Skip to content

Commit

Permalink
Merge pull request #76 from mkhanali/add_buffer_size_option
Browse files Browse the repository at this point in the history
Add buffer size option to connection
  • Loading branch information
worg authored Apr 28, 2020
2 parents 75e99cc + cd5b9c8 commit 8baf124
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func Connect(conn io.ReadWriteCloser, opts ...func(*Conn) error) (*Conn, error)
return nil, err
}

if options.ReadBufferSize > 0 {
reader = frame.NewReaderSize(conn, options.ReadBufferSize)
}

if options.WriteBufferSize > 0 {
writer = frame.NewWriterSize(conn, options.ReadBufferSize)
}

readChannelCapacity := 20
writeChannelCapacity := 20

Expand Down
25 changes: 25 additions & 0 deletions conn_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type connOptions struct {
AcceptVersions []string
Header *frame.Header
ReadChannelCapacity, WriteChannelCapacity int
ReadBufferSize, WriteBufferSize int
}

func newConnOptions(conn *Conn, opts []func(*Conn) error) (*connOptions, error) {
Expand Down Expand Up @@ -156,6 +157,16 @@ var ConnOpt struct {
// same time. A high number may affect memory usage while a too low number may lock the
// system up. Default is set to 20.
WriteChannelCapacity func(capacity int) func(*Conn) error

// ReadBufferSize specifies number of bytes that can be used to read the message
// A high number may affect memory usage while a too low number may lock the
// system up. Default is set to 4096.
ReadBufferSize func(size int) func(*Conn) error

// WriteBufferSize specifies number of bytes that can be used to write the message
// A high number may affect memory usage while a too low number may lock the
// system up. Default is set to 4096.
WriteBufferSize func(size int) func(*Conn) error
}

func init() {
Expand Down Expand Up @@ -244,4 +255,18 @@ func init() {
return nil
}
}

ConnOpt.ReadBufferSize = func(size int) func(*Conn) error {
return func(c *Conn) error {
c.options.ReadBufferSize = size
return nil
}
}

ConnOpt.WriteBufferSize = func(size int) func(*Conn) error {
return func(c *Conn) error {
c.options.WriteBufferSize = size
return nil
}
}
}

0 comments on commit 8baf124

Please sign in to comment.