Skip to content

Commit fe3b8f2

Browse files
committed
BufSize option, bump for go1.5.1, bump version
1 parent f26e2e4 commit fe3b8f2

9 files changed

+23
-11
lines changed

Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.5
1+
FROM golang:1.5.1
22

33
MAINTAINER Derek Collison <derek@apcera.com>
44

@@ -10,4 +10,3 @@ RUN CGO_ENABLED=0 go install -v -a -tags netgo -installsuffix netgo -ldflags "-s
1010
EXPOSE 4222 8222
1111
ENTRYPOINT ["gnatsd"]
1212
CMD ["--help"]
13-

gnatsd.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func main() {
5353
flag.IntVar(&opts.ProfPort, "profile", 0, "Profiling HTTP port")
5454
flag.StringVar(&opts.RoutesStr, "routes", "", "Routes to actively solicit a connection.")
5555

56+
// Not public per se, will be replaced with dynamic system, but can be used to lower memory footprint when
57+
// lots of connections present.
58+
flag.IntVar(&opts.BufSize, "bs", 0, "Read/Write buffer size per client connection.")
59+
5660
flag.Usage = server.Usage
5761

5862
flag.Parse()

server/client.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
)
1818

1919
const (
20-
// The size of the bufio reader/writer on top of the socket.
21-
defaultBufSize = 32768
2220
// Scratch buffer size for the processMsg() calls.
2321
msgScratchSize = 512
2422
msgHeadProto = "MSG "
@@ -94,7 +92,7 @@ func init() {
9492
func (c *client) initClient() {
9593
s := c.srv
9694
c.cid = atomic.AddUint64(&s.gcid, 1)
97-
c.bw = bufio.NewWriterSize(c.nc, defaultBufSize)
95+
c.bw = bufio.NewWriterSize(c.nc, s.opts.BufSize)
9896
c.subs = hashmap.New()
9997

10098
// This is a scratch buffer used for processMsg()
@@ -123,8 +121,8 @@ func (c *client) initClient() {
123121
// No clue why, but this stalls and kills performance on Mac (Mavericks).
124122
//
125123
// if ip, ok := c.nc.(*net.TCPConn); ok {
126-
// ip.SetReadBuffer(defaultBufSize)
127-
// ip.SetWriteBuffer(2 * defaultBufSize)
124+
// ip.SetReadBuffer(s.opts.BufSize)
125+
// ip.SetWriteBuffer(2 * s.opts.BufSize)
128126
// }
129127

130128
// Set the Ping timer
@@ -139,13 +137,14 @@ func (c *client) readLoop() {
139137
// We check for that after the loop, but want to avoid a nil dereference
140138
c.mu.Lock()
141139
nc := c.nc
140+
s := c.srv
142141
c.mu.Unlock()
143142

144143
if nc == nil {
145144
return
146145
}
147146

148-
b := make([]byte, defaultBufSize)
147+
b := make([]byte, s.opts.BufSize)
149148

150149
for {
151150
n, err := nc.Read(b)

server/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var defaultServerOptions = Options{
4646

4747
func rawSetup(serverOptions Options) (*Server, *client, *bufio.Reader, string) {
4848
cli, srv := net.Pipe()
49-
cr := bufio.NewReaderSize(cli, defaultBufSize)
49+
cr := bufio.NewReaderSize(cli, DEFAULT_BUF_SIZE)
5050
s := New(&serverOptions)
5151
if serverOptions.Authorization != "" {
5252
s.SetAuthMethod(&mockAuth{})

server/const.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
const (
1010
// VERSION is the current version for the server.
11-
VERSION = "0.6.6"
11+
VERSION = "0.6.8"
1212

1313
// DEFAULT_PORT is the deault port for client connections.
1414
DEFAULT_PORT = 4222
@@ -82,4 +82,8 @@ const (
8282

8383
// MAX_PUB_ARGS Maximum possible number of arguments from PUB proto.
8484
MAX_PUB_ARGS = 3
85+
86+
// Default Buffer size for reads and writes per connection. Will be replaced by dynamic
87+
// system in the long run.
88+
DEFAULT_BUF_SIZE = 32768
8589
)

server/opts.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Options struct {
4747
RemoteSyslog string `json:"-"`
4848
Routes []*url.URL `json:"-"`
4949
RoutesStr string `json:"-"`
50+
BufSize int `json:"-"`
5051
}
5152

5253
type authorization struct {
@@ -366,4 +367,7 @@ func processOptions(opts *Options) {
366367
if opts.MaxPending == 0 {
367368
opts.MaxPending = MAX_PENDING_SIZE
368369
}
370+
if opts.BufSize == 0 {
371+
opts.BufSize = DEFAULT_BUF_SIZE
372+
}
369373
}

server/opts_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestDefaultOptions(t *testing.T) {
2222
MaxPayload: MAX_PAYLOAD_SIZE,
2323
MaxPending: MAX_PENDING_SIZE,
2424
ClusterAuthTimeout: float64(AUTH_TIMEOUT) / float64(time.Second),
25+
BufSize: DEFAULT_BUF_SIZE,
2526
}
2627

2728
opts := &Options{}

server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ func (s *Server) AcceptLoop() {
278278
Noticef("Listening for client connections on %s", hp)
279279
l, e := net.Listen("tcp", hp)
280280
if e != nil {
281+
fmt.Printf("could not listen on port for %s, %v\n", hp, e)
281282
Fatalf("Error listening on port: %s, %q", hp, e)
282283
return
283284
}

test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func RunServerWithAuth(opts *server.Options, auth server.Auth) *server.Server {
7878
for time.Now().Before(end) {
7979
addr := s.Addr()
8080
if addr == nil {
81-
time.Sleep(10 * time.Millisecond)
81+
time.Sleep(50 * time.Millisecond)
8282
// Retry. We might take a little while to open a connection.
8383
continue
8484
}

0 commit comments

Comments
 (0)