Skip to content

Add write_timeout policy configuration option#7499

Merged
neilalexander merged 2 commits intomainfrom
neil/writetimeout
Oct 31, 2025
Merged

Add write_timeout policy configuration option#7499
neilalexander merged 2 commits intomainfrom
neil/writetimeout

Conversation

@neilalexander
Copy link
Copy Markdown
Member

This adds a new write_timeout configuration option which controls the slow consumer behaviour more precisely with what happens when the write_deadline is reached. This option can be set for clients, routes, gateways or leafnodes independently. Three values are supported:

  • default: functionality as it stands today, retry for route/gateway/leafnode, close for client
  • retry: when hitting the write_deadline, keep retrying writes until either we succeed or the ping interval hits
  • close: when hitting the write_deadline, close the connection immediately

Signed-off-by: Neil Twigg neil@nats.io

… leafnodes

Signed-off-by: Neil Twigg <neil@nats.io>
@neilalexander neilalexander marked this pull request as ready for review October 31, 2025 10:55
@neilalexander neilalexander requested a review from a team as a code owner October 31, 2025 10:55
@neilalexander neilalexander requested a review from a team October 31, 2025 10:56
Copy link
Copy Markdown
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

server/client.go Outdated
fsp int32 // Flush signals that are pending per producer from readLoop's pcd.
sg *sync.Cond // To signal writeLoop that there is data to flush.
wdl time.Duration // Snapshot of write deadline.
wtp WriteTimeoutPolicy // What do we do on a write timeout?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we store this once in the server struct?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier versions of this code did have them in the server struct but the switch was slower than having it per-client, although I have noticed that I can make the WriteTimeoutPolicy a uint8 instead and modify the placement in the outbound struct, and then it actually takes up no more extra space due to existing alignment, so I'll do that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main branch:

  // client.go:330 | Size: 112
  type outbound struct {
    nb  net.Buffers    ■ ■ ■ ■ ■ ■ ■ ■
                       ■ ■ ■ ■ ■ ■ ■ ■
                       ■ ■ ■ ■ ■ ■ ■ ■
    wnb net.Buffers    ■ ■ ■ ■ ■ ■ ■ ■
                       ■ ■ ■ ■ ■ ■ ■ ■
                       ■ ■ ■ ■ ■ ■ ■ ■
    pb  int64          ■ ■ ■ ■ ■ ■ ■ ■
    fsp int32          ■ ■ ■ ■ □ □ □ □
    sg  *sync.Cond     ■ ■ ■ ■ ■ ■ ■ ■
    wdl time.Duration  ■ ■ ■ ■ ■ ■ ■ ■
    mp  int64          ■ ■ ■ ■ ■ ■ ■ ■
    lft time.Duration  ■ ■ ■ ■ ■ ■ ■ ■
    stc chan struct{}  ■ ■ ■ ■ ■ ■ ■ ■
    cw  *s2.Writer     ■ ■ ■ ■ ■ ■ ■ ■
  }

After this fix, so same amount of space consumed overall:

  // client.go:350 | Size: 112
  type outbound struct {
    nb  net.Buffers         ■ ■ ■ ■ ■ ■ ■ ■
                            ■ ■ ■ ■ ■ ■ ■ ■
                            ■ ■ ■ ■ ■ ■ ■ ■
    wnb net.Buffers         ■ ■ ■ ■ ■ ■ ■ ■
                            ■ ■ ■ ■ ■ ■ ■ ■
                            ■ ■ ■ ■ ■ ■ ■ ■
    pb  int64               ■ ■ ■ ■ ■ ■ ■ ■
    fsp int32               ■ ■ ■ ■
    wtp WriteTimeoutPolicy          ■ □ □ □
    sg  *sync.Cond          ■ ■ ■ ■ ■ ■ ■ ■
    wdl time.Duration       ■ ■ ■ ■ ■ ■ ■ ■
    mp  int64               ■ ■ ■ ■ ■ ■ ■ ■
    lft time.Duration       ■ ■ ■ ■ ■ ■ ■ ■
    stc chan struct{}       ■ ■ ■ ■ ■ ■ ■ ■
    cw  *s2.Writer          ■ ■ ■ ■ ■ ■ ■ ■
  }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks.

Signed-off-by: Neil Twigg <neil@nats.io>
@neilalexander neilalexander merged commit e2661b5 into main Oct 31, 2025
69 of 70 checks passed
@neilalexander neilalexander deleted the neil/writetimeout branch October 31, 2025 13:30
neilalexander added a commit that referenced this pull request Nov 5, 2025
Includes the following:

- #7499
- #7503
- #7508
- #7510
- #7509
- #7512
- #7516
- #7515

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit that referenced this pull request Nov 5, 2025
Includes the following:

- #7416
- #7425
- #7486
- #7495
- #7482
- #7496
- #7499
- #7503
- #7508 (excluding weak
pointer/cache-related changes that apply only to 2.12.x)
- #7510
- #7509
- #7512
- #7516
- #7515

Signed-off-by: Neil Twigg <neil@nats.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants