dynamic read buffers

This commit is contained in:
Derek Collison
2016-04-03 14:30:17 -07:00
parent ca4bde918b
commit 768f23b5b4
5 changed files with 58 additions and 11 deletions

View File

@@ -23,6 +23,13 @@ const (
msgHeadProto = "MSG "
)
// For controlling dynamic buffer sizes.
const (
startReadBufSize = 512 // For INFO JSON block
minReadBufSize = 128
maxReadBufSize = 65536
)
// Type of client
const (
// CLIENT is an end user.
@@ -164,9 +171,8 @@ func (c *client) readLoop() {
return
}
// read buffer
// FIXME(dlc) make dynamic
b := make([]byte, s.opts.BufSize)
// Start read buffer.
b := make([]byte, startReadBufSize)
for {
n, err := nc.Read(b)
@@ -226,6 +232,18 @@ func (c *client) readLoop() {
if nc == nil {
return
}
// Update buffer size as/if needed.
// Grow
if n == len(b) && len(b) < maxReadBufSize {
b = make([]byte, len(b)*2)
}
// Shrink, for now don't accelerate, ping/pong will eventually sort it out.
if n < len(b)/2 && len(b) > minReadBufSize {
b = make([]byte, len(b)/2)
}
}
}

View File

@@ -8,7 +8,7 @@ import (
const (
// VERSION is the current version for the server.
VERSION = "0.7.9"
VERSION = "0.8.0.beta"
// DEFAULT_PORT is the deault port for client connections.
DEFAULT_PORT = 4222

View File

@@ -537,10 +537,20 @@ func (c *client) parse(buf []byte) error {
case '\r':
c.drop = 1
case '\n':
if err := c.processInfo(buf[c.as : i-c.drop]); err != nil {
var arg []byte
if c.argBuf != nil {
arg = c.argBuf
} else {
arg = buf[c.as : i-c.drop]
}
if err := c.processInfo(arg); err != nil {
return err
}
c.drop, c.state = 0, OP_START
c.drop, c.as, c.state = 0, i+1, OP_START
default:
if c.argBuf != nil {
c.argBuf = append(c.argBuf, b)
}
}
case OP_PLUS:
switch b {
@@ -623,7 +633,7 @@ func (c *client) parse(buf []byte) error {
// Check for split buffer scenarios for any ARG state.
if (c.state == SUB_ARG || c.state == UNSUB_ARG || c.state == PUB_ARG ||
c.state == MSG_ARG || c.state == MINUS_ERR_ARG ||
c.state == CONNECT_ARG) && c.argBuf == nil {
c.state == CONNECT_ARG || c.state == INFO_ARG) && c.argBuf == nil {
c.argBuf = c.scratch[:0]
c.argBuf = append(c.argBuf, buf[c.as:i-c.drop]...)
// FIXME(dlc), check max control line len