From 52978c11e8c21c9a6016c82fa0af0960b66d280d Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Wed, 15 Nov 2017 17:01:30 -0800 Subject: [PATCH] mgrpc: decrease size of channel buffers Signed-off-by: Stephen J Day --- channel.go | 8 ++++++-- server.go | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/channel.go b/channel.go index 6aca367..d4ef6bb 100644 --- a/channel.go +++ b/channel.go @@ -12,6 +12,8 @@ import ( "github.com/pkg/errors" ) +const maxMessageSize = 8 << 10 // TODO(stevvooe): Cut these down, since they are pre-alloced. + type channel struct { conn net.Conn bw *bufio.Writer @@ -21,13 +23,15 @@ type channel struct { func newChannel(conn net.Conn) *channel { return &channel{ conn: conn, - bw: bufio.NewWriter(conn), - br: bufio.NewReader(conn), + bw: bufio.NewWriterSize(conn, maxMessageSize), + br: bufio.NewReaderSize(conn, maxMessageSize), } } func (ch *channel) recv(ctx context.Context, msg interface{}) error { defer log.G(ctx).WithField("msg", msg).Info("recv") + + // TODO(stevvooe): Use `bufio.Reader.Peek` here to remove this allocation. var p [maxMessageSize]byte n, err := readmsg(ch.br, p[:]) if err != nil { diff --git a/server.go b/server.go index 3691a36..9587578 100644 --- a/server.go +++ b/server.go @@ -46,8 +46,6 @@ func (s *Server) Serve(l net.Listener) error { return nil } -const maxMessageSize = 1 << 20 // TODO(stevvooe): Cut these down, since they are pre-alloced. - func (s *Server) handleConn(conn net.Conn) { defer conn.Close()