Skip to content

Commit a9b3ccc

Browse files
authored
chore: replace unbuffered channels with buffered channels (#1668)
* 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
1 parent 670d418 commit a9b3ccc

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

dot/rpc/subscription/listeners_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ func TestBlockFinalizedListener_Listen(t *testing.T) {
121121
}
122122

123123
func TestExtrinsicSubmitListener_Listen(t *testing.T) {
124-
notifyImportedChan := make(chan *types.Block)
125-
notifyFinalizedChan := make(chan *types.FinalisationInfo)
124+
notifyImportedChan := make(chan *types.Block, 100)
125+
notifyFinalizedChan := make(chan *types.FinalisationInfo, 100)
126126

127127
mockConnection := &MockWSConnAPI{}
128128
esl := ExtrinsicSubmitListener{

dot/rpc/subscription/websocket.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ var errCannotReadFromWebsocket = errors.New("cannot read message from websocket"
4343
var errCannotUnmarshalMessage = errors.New("cannot unmarshal webasocket message data")
4444
var logger = log.New("pkg", "rpc/subscription")
4545

46+
// DEFAULT_BUFFER_SIZE buffer size for channels
47+
const DEFAULT_BUFFER_SIZE = 100
48+
4649
// WSConn struct to hold WebSocket Connection references
4750
type WSConn struct {
4851
Wsconn *websocket.Conn
@@ -228,7 +231,7 @@ func (c *WSConn) unsubscribeStorageListener(reqID float64, l Listener, _ interfa
228231

229232
func (c *WSConn) initBlockListener(reqID float64, _ interface{}) (Listener, error) {
230233
bl := &BlockListener{
231-
Channel: make(chan *types.Block),
234+
Channel: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
232235
wsconn: c,
233236
}
234237

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

261264
func (c *WSConn) initBlockFinalizedListener(reqID float64, _ interface{}) (Listener, error) {
262265
bfl := &BlockFinalizedListener{
263-
channel: make(chan *types.FinalisationInfo),
266+
channel: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
264267
wsconn: c,
265268
}
266269

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

300303
// listen for built blocks
301304
esl := &ExtrinsicSubmitListener{
302-
importedChan: make(chan *types.Block),
305+
importedChan: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
303306
wsconn: c,
304307
extrinsic: types.Extrinsic(extBytes),
305-
finalisedChan: make(chan *types.FinalisationInfo),
308+
finalisedChan: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
306309
}
307310

308311
if c.BlockAPI == nil {

0 commit comments

Comments
 (0)