-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(lib/common) implement byte pool to improve websocket subscription efficiency #1693
Changes from 6 commits
8ffe64c
4c54701
dbe0bfb
0ce8318
b994af0
1aeb73d
9fa3365
f82db74
1d100b9
8ef628d
8234e2d
5dc184d
d0eb594
3b52ece
06f4a73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||||||
// This file is part of gossamer. | ||||||
// | ||||||
// The gossamer library is free software: you can redistribute it and/or modify | ||||||
// it under the terms of the GNU Lesser General Public License as published by | ||||||
// the Free Software Foundation, either version 3 of the License, or | ||||||
// (at your option) any later version. | ||||||
// | ||||||
// The gossamer library is distributed in the hope that it will be useful, | ||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
// GNU Lesser General Public License for more details. | ||||||
// | ||||||
// You should have received a copy of the GNU Lesser General Public License | ||||||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||||||
|
||||||
package common | ||||||
|
||||||
import "fmt" | ||||||
|
||||||
// BytePool struct to hold byte objects that will be contained in pool | ||||||
type BytePool struct { | ||||||
c chan byte | ||||||
} | ||||||
|
||||||
// NewBytePool256 creates and initialises pool with 256 entries | ||||||
func NewBytePool256() (bp *BytePool, err error) { | ||||||
bp = NewBytePool(256) | ||||||
for i := 0; i < 256; i++ { | ||||||
err = bp.Put(byte(i)) | ||||||
} | ||||||
return | ||||||
} | ||||||
|
||||||
// NewBytePool creates a new empty byte pool with capacity of size | ||||||
func NewBytePool(size int) (bp *BytePool) { | ||||||
return &BytePool{ | ||||||
c: make(chan byte, size), | ||||||
} | ||||||
} | ||||||
|
||||||
// Get gets a Buffer from the BytePool, or creates a new one if none are | ||||||
// available in the pool. | ||||||
func (bp *BytePool) Get() (b byte, err error) { | ||||||
select { | ||||||
case b = <-bp.c: | ||||||
default: | ||||||
err = fmt.Errorf("all slots used") | ||||||
} | ||||||
return | ||||||
} | ||||||
|
||||||
// Put returns the given Buffer to the BytePool. | ||||||
func (bp *BytePool) Put(b byte) error { | ||||||
select { | ||||||
case bp.c <- b: | ||||||
return nil | ||||||
default: // Discard the buffer if the pool is full. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment removed. |
||||||
return fmt.Errorf("pool is full") | ||||||
} | ||||||
} | ||||||
|
||||||
// NumPooled returns the number of items currently pooled. | ||||||
func (bp *BytePool) NumPooled() int { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||||||
return len(bp.c) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package common | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBytePool(t *testing.T) { | ||
bp := NewBytePool(5) | ||
require.Equal(t, 0, bp.NumPooled()) | ||
|
||
for i := 0; i < 5; i++ { | ||
err := bp.Put(generateID()) | ||
require.NoError(t, err) | ||
} | ||
err := bp.Put(generateID()) | ||
require.EqualError(t, err, "pool is full") | ||
require.Equal(t, 5, bp.NumPooled()) | ||
|
||
for i := 0; i < 5; i++ { | ||
_, err := bp.Get() // nolint | ||
require.NoError(t, err) | ||
} | ||
_, err = bp.Get() | ||
require.EqualError(t, err, "all slots used") | ||
} | ||
|
||
func TestBytePool256(t *testing.T) { | ||
bp, err := NewBytePool256() | ||
require.NoError(t, err) | ||
require.Equal(t, 256, bp.NumPooled()) | ||
|
||
for i := 0; i < 256; i++ { | ||
_, err := bp.Get() // nolint | ||
require.NoError(t, err) | ||
} | ||
_, err = bp.Get() | ||
require.EqualError(t, err, "all slots used") | ||
} | ||
|
||
func generateID() byte { | ||
// skipcq: GSC-G404 | ||
id := rand.Intn(256) //nolint | ||
return byte(id) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.