@@ -61,6 +61,7 @@ const (
6161 DefaultMaxPingOut = 2
6262 DefaultMaxChanLen = 64 * 1024 // 64k
6363 DefaultReconnectBufSize = 8 * 1024 * 1024 // 8MB
64+ DefaultWriteBufSize = defaultBufSize
6465 RequestChanLen = 8
6566 DefaultDrainTimeout = 30 * time .Second
6667 DefaultFlusherTimeout = time .Minute
@@ -574,6 +575,14 @@ type Options struct {
574575 // IgnoreDiscoveredServers will disable adding advertised server URLs
575576 // from INFO messages to the server pool.
576577 IgnoreDiscoveredServers bool
578+
579+ // WriteBufferSize is an advanced option that sets the flush threshold
580+ // of the write buffer used to batch outgoing data before writing to
581+ // the underlying connection. In most cases, the default value should
582+ // not be changed. A smaller buffer reduces the amount of data that
583+ // can be lost on blocked writes but may significantly reduce throughput.
584+ // Defaults to 32768 bytes (32KB).
585+ WriteBufferSize int
577586}
578587
579588const (
@@ -1172,6 +1181,19 @@ func ReconnectBufSize(size int) Option {
11721181 }
11731182}
11741183
1184+ // WriteBufferSize is an advanced option that sets the flush threshold
1185+ // of the write buffer used to batch outgoing data before writing to
1186+ // the underlying connection. In most cases, the default value should
1187+ // not be changed. A smaller buffer reduces the amount of data that
1188+ // can be lost on blocked writes but may significantly reduce throughput.
1189+ // Defaults to 32768 bytes (32KB).
1190+ func WriteBufferSize (size int ) Option {
1191+ return func (o * Options ) error {
1192+ o .WriteBufferSize = size
1193+ return nil
1194+ }
1195+ }
1196+
11751197// Timeout is an Option to set the timeout for Dial on a connection.
11761198// Defaults to 2s.
11771199func Timeout (t time.Duration ) Option {
@@ -1741,6 +1763,10 @@ func (o Options) Connect() (*Conn, error) {
17411763 if nc .Opts .ReconnectBufSize == 0 {
17421764 nc .Opts .ReconnectBufSize = DefaultReconnectBufSize
17431765 }
1766+ // Default WriteBufferSize
1767+ if nc .Opts .WriteBufferSize <= 0 {
1768+ nc .Opts .WriteBufferSize = DefaultWriteBufSize
1769+ }
17441770 // Ensure that Timeout is not 0
17451771 if nc .Opts .Timeout == 0 {
17461772 nc .Opts .Timeout = DefaultTimeout
@@ -2080,7 +2106,7 @@ func (nc *Conn) newReaderWriter() {
20802106 off : - 1 ,
20812107 }
20822108 nc .bw = & natsWriter {
2083- limit : defaultBufSize ,
2109+ limit : nc . Opts . WriteBufferSize ,
20842110 plimit : nc .Opts .ReconnectBufSize ,
20852111 }
20862112}
0 commit comments