-
Notifications
You must be signed in to change notification settings - Fork 122
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8ffe64c
implement bufferpool
edwardmack 4c54701
implement byte pool for subscription setup
edwardmack dbe0bfb
Merge branch 'development' into ed/ws_subscriptions_buffered_pool
edwardmack 0ce8318
lint
edwardmack b994af0
Merge branch 'ed/ws_subscriptions_buffered_pool' of https://github.co…
edwardmack 1aeb73d
fix lint
edwardmack 9fa3365
address comments
edwardmack f82db74
Merge branch 'development' into ed/ws_subscriptions_buffered_pool
edwardmack 1d100b9
Merge branch 'development' into ed/ws_subscriptions_buffered_pool
edwardmack 8ef628d
refactor UnregisterFinalizedChannel to UnregisterFinalisedChannel
edwardmack 8234e2d
Merge branch 'development' into ed/ws_subscriptions_buffered_pool
edwardmack 5dc184d
lint
edwardmack d0eb594
Merge branch 'development' into ed/ws_subscriptions_buffered_pool
edwardmack 3b52ece
change error handling
edwardmack 06f4a73
refactor NumPooled to Len
edwardmack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() *BytePool { | ||
bp := NewBytePool(256) | ||
for i := 0; i < 256; i++ { | ||
_ = bp.Put(byte(i)) | ||
} | ||
return bp | ||
} | ||
|
||
// 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: | ||
return fmt.Errorf("pool is full") | ||
} | ||
} | ||
|
||
// NumPooled returns the number of items currently pooled. | ||
func (bp *BytePool) NumPooled() int { | ||
return len(bp.c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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 := NewBytePool256() | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit:
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.