Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: add flag --min-output-buffer-timeout with default 25ms #1133

Merged
merged 2 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func nsqdFlagSet(opts *nsqd.Options) *flag.FlagSet {
flagSet.Int64("max-rdy-count", opts.MaxRdyCount, "maximum RDY count for a client")
flagSet.Int64("max-output-buffer-size", opts.MaxOutputBufferSize, "maximum client configurable size (in bytes) for a client output buffer")
flagSet.Duration("max-output-buffer-timeout", opts.MaxOutputBufferTimeout, "maximum client configurable duration of time between flushing to a client")
flagSet.Duration("min-output-buffer-timeout", opts.MinOutputBufferTimeout, "minimum client configurable duration of time between flushing to a client")
flagSet.Duration("output-buffer-timeout", opts.OutputBufferTimeout, "default duration of time between flushing data to clients")

// statsd integration options
Expand Down
57 changes: 23 additions & 34 deletions nsqd/client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,7 @@ func (c *clientV2) Identify(data identifyDataV2) error {
return err
}

err = c.SetOutputBufferSize(data.OutputBufferSize)
if err != nil {
return err
}

err = c.SetOutputBufferTimeout(data.OutputBufferTimeout)
err = c.SetOutputBuffer(data.OutputBufferSize, data.OutputBufferTimeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -413,49 +408,43 @@ func (c *clientV2) SetHeartbeatInterval(desiredInterval int) error {
return nil
}

func (c *clientV2) SetOutputBufferSize(desiredSize int) error {
var size int
func (c *clientV2) SetOutputBuffer(desiredSize int, desiredTimeout int) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()

switch {
case desiredTimeout == -1:
c.OutputBufferTimeout = 0
case desiredTimeout == 0:
// do nothing (use default)
case true &&
desiredTimeout >= int(c.ctx.nsqd.getOpts().MinOutputBufferTimeout/time.Millisecond) &&
desiredTimeout <= int(c.ctx.nsqd.getOpts().MaxOutputBufferTimeout/time.Millisecond):

c.OutputBufferTimeout = time.Duration(desiredTimeout) * time.Millisecond
default:
return fmt.Errorf("output buffer timeout (%d) is invalid", desiredTimeout)
}

switch {
case desiredSize == -1:
// effectively no buffer (every write will go directly to the wrapped net.Conn)
size = 1
c.OutputBufferSize = 1
c.OutputBufferTimeout = 0
case desiredSize == 0:
// do nothing (use default)
case desiredSize >= 64 && desiredSize <= int(c.ctx.nsqd.getOpts().MaxOutputBufferSize):
size = desiredSize
c.OutputBufferSize = desiredSize
default:
return fmt.Errorf("output buffer size (%d) is invalid", desiredSize)
}

if size > 0 {
c.writeLock.Lock()
defer c.writeLock.Unlock()
c.OutputBufferSize = size
if desiredSize != 0 {
err := c.Writer.Flush()
if err != nil {
return err
}
c.Writer = bufio.NewWriterSize(c.Conn, size)
}

return nil
}

func (c *clientV2) SetOutputBufferTimeout(desiredTimeout int) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()

switch {
case desiredTimeout == -1:
c.OutputBufferTimeout = 0
case desiredTimeout == 0:
// do nothing (use default)
case desiredTimeout >= 1 &&
desiredTimeout <= int(c.ctx.nsqd.getOpts().MaxOutputBufferTimeout/time.Millisecond):
c.OutputBufferTimeout = time.Duration(desiredTimeout) * time.Millisecond
default:
return fmt.Errorf("output buffer timeout (%d) is invalid", desiredTimeout)
c.Writer = bufio.NewWriterSize(c.Conn, c.OutputBufferSize)
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion nsqd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Options struct {
MaxRdyCount int64 `flag:"max-rdy-count"`
MaxOutputBufferSize int64 `flag:"max-output-buffer-size"`
MaxOutputBufferTimeout time.Duration `flag:"max-output-buffer-timeout"`
MinOutputBufferTimeout time.Duration `flag:"min-output-buffer-timeout"`
OutputBufferTimeout time.Duration `flag:"output-buffer-timeout"`

// statsd integration
Expand Down Expand Up @@ -130,7 +131,8 @@ func NewOptions() *Options {
MaxHeartbeatInterval: 60 * time.Second,
MaxRdyCount: 2500,
MaxOutputBufferSize: 64 * 1024,
MaxOutputBufferTimeout: 1 * time.Second,
MaxOutputBufferTimeout: 30 * time.Second,
MinOutputBufferTimeout: 25 * time.Millisecond,
OutputBufferTimeout: 250 * time.Millisecond,

StatsdPrefix: "nsq.%s",
Expand Down