forked from ChainSafe/gossamer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(lib/common) implement byte pool to improve websocket subscripti…
…on efficiency (ChainSafe#1693) * implement bufferpool * implement byte pool for subscription setup * lint * fix lint * address comments * refactor UnregisterFinalizedChannel to UnregisterFinalisedChannel * lint * change error handling * refactor NumPooled to Len
- Loading branch information
1 parent
281eef2
commit 0639451
Showing
12 changed files
with
168 additions
and
45 deletions.
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") | ||
} | ||
} | ||
|
||
// Len returns the number of items currently pooled. | ||
func (bp *BytePool) Len() 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.Len()) | ||
|
||
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.Len()) | ||
|
||
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.Len()) | ||
|
||
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