Skip to content

Commit

Permalink
chore: replace unbuffered channels with buffered channels (#1668)
Browse files Browse the repository at this point in the history
* add buffer to channels

* add buffers to channels in tests

* remove buffers from channels that shouldn't be buffered

* added DEFAULT_BUFFERS_SIZE const

* lint

* addres comments
  • Loading branch information
edwardmack authored Jul 9, 2021
1 parent 670d418 commit a9b3ccc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func TestBlockFinalizedListener_Listen(t *testing.T) {
}

func TestExtrinsicSubmitListener_Listen(t *testing.T) {
notifyImportedChan := make(chan *types.Block)
notifyFinalizedChan := make(chan *types.FinalisationInfo)
notifyImportedChan := make(chan *types.Block, 100)
notifyFinalizedChan := make(chan *types.FinalisationInfo, 100)

mockConnection := &MockWSConnAPI{}
esl := ExtrinsicSubmitListener{
Expand Down
11 changes: 7 additions & 4 deletions dot/rpc/subscription/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var errCannotReadFromWebsocket = errors.New("cannot read message from websocket"
var errCannotUnmarshalMessage = errors.New("cannot unmarshal webasocket message data")
var logger = log.New("pkg", "rpc/subscription")

// DEFAULT_BUFFER_SIZE buffer size for channels
const DEFAULT_BUFFER_SIZE = 100

// WSConn struct to hold WebSocket Connection references
type WSConn struct {
Wsconn *websocket.Conn
Expand Down Expand Up @@ -228,7 +231,7 @@ func (c *WSConn) unsubscribeStorageListener(reqID float64, l Listener, _ interfa

func (c *WSConn) initBlockListener(reqID float64, _ interface{}) (Listener, error) {
bl := &BlockListener{
Channel: make(chan *types.Block),
Channel: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
wsconn: c,
}

Expand Down Expand Up @@ -260,7 +263,7 @@ func (c *WSConn) initBlockListener(reqID float64, _ interface{}) (Listener, erro

func (c *WSConn) initBlockFinalizedListener(reqID float64, _ interface{}) (Listener, error) {
bfl := &BlockFinalizedListener{
channel: make(chan *types.FinalisationInfo),
channel: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
wsconn: c,
}

Expand Down Expand Up @@ -299,10 +302,10 @@ func (c *WSConn) initExtrinsicWatch(reqID float64, params interface{}) (Listener

// listen for built blocks
esl := &ExtrinsicSubmitListener{
importedChan: make(chan *types.Block),
importedChan: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
wsconn: c,
extrinsic: types.Extrinsic(extBytes),
finalisedChan: make(chan *types.FinalisationInfo),
finalisedChan: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
}

if c.BlockAPI == nil {
Expand Down

0 comments on commit a9b3ccc

Please sign in to comment.