Skip to content

Commit

Permalink
bss: increase read limit for bfg conn to 2 MiB (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing authored Aug 2, 2024
1 parent 05d559f commit 9557ba7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
4 changes: 3 additions & 1 deletion api/auth/secp256k1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func TestProtocolHandshake(t *testing.T) {
}

clientURI := testServer.URL
conn, err := protocol.NewConn(clientURI, clientAuth)
conn, err := protocol.NewConn(clientURI, &protocol.ConnOptions{
Authenticator: clientAuth,
})
if err != nil {
t.Fatal(err)
}
Expand Down
36 changes: 28 additions & 8 deletions api/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ type Conn struct {
sync.RWMutex

serverURL string
auth Authenticator
opts ConnOptions
msgID uint64

wsc *websocket.Conn
Expand All @@ -372,8 +372,21 @@ type Conn struct {
calls map[string]chan *readResult
}

// ConnOptions are options available for a Conn.
type ConnOptions struct {
// ReadLimit is the maximum number of bytes to read from the connection.
// Defaults to defaultConnReadLimit.
ReadLimit int64

// Authenticator is the connection authenticator.
Authenticator Authenticator
}

// defaultConnReadLimit is the default connection read limit.
const defaultConnReadLimit = 512 * (1 << 10) // 512 KiB

// NewConn returns a client side connection object.
func NewConn(urlStr string, authenticator Authenticator) (*Conn, error) {
func NewConn(urlStr string, opts *ConnOptions) (*Conn, error) {
log.Tracef("NewConn: %v", urlStr)
defer log.Tracef("NewConn exit: %v", urlStr)

Expand All @@ -382,9 +395,16 @@ func NewConn(urlStr string, authenticator Authenticator) (*Conn, error) {
return nil, err
}

if opts == nil {
opts = new(ConnOptions)
}
if opts.ReadLimit <= 0 {
opts.ReadLimit = defaultConnReadLimit
}

ac := &Conn{
serverURL: u.String(),
auth: authenticator,
opts: *opts,
calls: make(map[string]chan *readResult),
msgID: 1,
}
Expand Down Expand Up @@ -414,7 +434,7 @@ func (ac *Conn) Connect(ctx context.Context) error {
if err != nil {
return fmt.Errorf("dial server: %w", err)
}
conn.SetReadLimit(512 * 1024) // XXX - default is 32KB
conn.SetReadLimit(ac.opts.ReadLimit)
defer func() {
if ac.wsc == nil {
conn.Close(websocket.StatusNormalClosure, "")
Expand All @@ -424,14 +444,14 @@ func (ac *Conn) Connect(ctx context.Context) error {
handshakeCtx, cancel := context.WithTimeout(ctx, WSHandshakeTimeout)
defer cancel()

if ac.auth != nil {
if auth := ac.opts.Authenticator; auth != nil {
log.Tracef("Connect: handshaking with %v", ac.serverURL)
if err := ac.auth.HandshakeClient(handshakeCtx, NewWSConn(conn)); err != nil {
if err := auth.HandshakeClient(handshakeCtx, NewWSConn(conn)); err != nil {
return HandshakeError(fmt.Sprintf("failed to handshake with server: %v", err))
}
}

// done as an API message and it should be done at the protocol
// done as an API message, and it should be done at the protocol
// level instead...
var msg Message
if err := NewWSConn(conn).ReadJSON(connectCtx, &msg); err != nil {
Expand All @@ -456,7 +476,7 @@ func (ac *Conn) Connect(ctx context.Context) error {
return nil
}

// wsConn returns the underlying webscket connection.
// wsConn returns the underlying websocket connection.
func (ac *Conn) wsConn() *websocket.Conn {
ac.RLock()
defer ac.RUnlock()
Expand Down
4 changes: 3 additions & 1 deletion service/bss/bss.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,9 @@ func (s *Server) connectBFG(ctx context.Context) error {
log.Tracef("connectBFG")
defer log.Tracef("connectBFG exit")

conn, err := protocol.NewConn(s.cfg.BFGURL, nil)
conn, err := protocol.NewConn(s.cfg.BFGURL, &protocol.ConnOptions{
ReadLimit: 2 * (1 << 20), // 2 MiB
})
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion service/popm/popm.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,9 @@ func (m *Miner) connectBFG(pctx context.Context) error {
return err
}

conn, err = protocol.NewConn(m.cfg.BFGWSURL, authenticator)
conn, err = protocol.NewConn(m.cfg.BFGWSURL, &protocol.ConnOptions{
Authenticator: authenticator,
})
if err != nil {
return err
}
Expand Down

0 comments on commit 9557ba7

Please sign in to comment.