-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: barrier hash lru pubsub sem pie 🐾
- Loading branch information
0 parents
commit ba9b72a
Showing
26 changed files
with
2,897 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.PHONY: all help test doc | ||
|
||
all: help | ||
|
||
help: ## Show this help | ||
@scripts/help.sh | ||
|
||
test: ## Test potential bugs and race conditions | ||
@scripts/test.sh | ||
|
||
doc: ## Generate docs | ||
@scripts/doc.sh |
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,14 @@ | ||
|
||
A collection of idiomatic patterns for Go language. | ||
|
||
## Patterns | ||
|
||
| Pattern | Description | Status | | ||
|:-------:|:----------- |:------:| | ||
| [Barrier](/docs/barrier.md) | Prevents a process from proceeding until all N processes reach to the barrier | ✔ | | ||
| [JumpHash](/docs/jumphash.md) | Provides a jump consistent hash implementation | ✔ | | ||
| [LRU](/docs/lru.md) | Implements a LRU cache | ✔ | | ||
| [Publish/Subscribe](/docs/pubsub.md) | Passes information to a collection of recipients who subscribed to a topic | ✔ | | ||
| [RingHash](/docs/ringhash.md) | Provides a ring hash implementation | ✔ | | ||
| [Semaphore](/docs/semaphore.md) | Allows controlling access to a common resource | ✔ | | ||
| [Singleton](/docs/singleton.md) | Restricts instantiation of a type to one object | ✔ | |
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,115 @@ | ||
|
||
|
||
# barrier | ||
`import "github.com/andy2046/gopie/pkg/barrier"` | ||
|
||
* [Overview](#pkg-overview) | ||
* [Index](#pkg-index) | ||
|
||
## <a name="pkg-overview">Overview</a> | ||
Package barrier provides a barrier implementation. | ||
|
||
|
||
|
||
|
||
## <a name="pkg-index">Index</a> | ||
* [Variables](#pkg-variables) | ||
* [type Barrier](#Barrier) | ||
* [func New(n int) *Barrier](#New) | ||
* [func (b *Barrier) Await(ctx context.Context) error](#Barrier.Await) | ||
* [func (b *Barrier) IsBroken() bool](#Barrier.IsBroken) | ||
* [func (b *Barrier) N() int](#Barrier.N) | ||
* [func (b *Barrier) NWaiting() int](#Barrier.NWaiting) | ||
* [func (b *Barrier) Reset()](#Barrier.Reset) | ||
|
||
|
||
#### <a name="pkg-files">Package files</a> | ||
[barrier.go](/src/github.com/andy2046/gopie/pkg/barrier/barrier.go) | ||
|
||
|
||
|
||
## <a name="pkg-variables">Variables</a> | ||
``` go | ||
var ( | ||
// ErrBroken when barrier is broken. | ||
ErrBroken = errors.New("barrier is broken") | ||
) | ||
``` | ||
|
||
|
||
|
||
## <a name="Barrier">type</a> [Barrier](/src/target/barrier.go?s=185:360#L11) | ||
``` go | ||
type Barrier struct { | ||
// contains filtered or unexported fields | ||
} | ||
``` | ||
Barrier is a synchronizer that allows members to wait for each other. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### <a name="New">func</a> [New](/src/target/barrier.go?s=484:508#L27) | ||
``` go | ||
func New(n int) *Barrier | ||
``` | ||
New returns a new barrier. | ||
|
||
|
||
|
||
|
||
|
||
### <a name="Barrier.Await">func</a> (\*Barrier) [Await](/src/target/barrier.go?s=763:813#L40) | ||
``` go | ||
func (b *Barrier) Await(ctx context.Context) error | ||
``` | ||
Await waits until all members have called await on the barrier. | ||
|
||
|
||
|
||
|
||
### <a name="Barrier.IsBroken">func</a> (\*Barrier) [IsBroken](/src/target/barrier.go?s=2069:2102#L123) | ||
``` go | ||
func (b *Barrier) IsBroken() bool | ||
``` | ||
IsBroken returns true if the barrier is broken. | ||
|
||
|
||
|
||
|
||
### <a name="Barrier.N">func</a> (\*Barrier) [N](/src/target/barrier.go?s=1809:1834#L111) | ||
``` go | ||
func (b *Barrier) N() int | ||
``` | ||
N returns the number of members for the barrier. | ||
|
||
|
||
|
||
|
||
### <a name="Barrier.NWaiting">func</a> (\*Barrier) [NWaiting](/src/target/barrier.go?s=1928:1960#L116) | ||
``` go | ||
func (b *Barrier) NWaiting() int | ||
``` | ||
NWaiting returns the number of members currently waiting at the barrier. | ||
|
||
|
||
|
||
|
||
### <a name="Barrier.Reset">func</a> (\*Barrier) [Reset](/src/target/barrier.go?s=1676:1701#L104) | ||
``` go | ||
func (b *Barrier) Reset() | ||
``` | ||
Reset resets the barrier to initial state. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
- - - | ||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) |
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,96 @@ | ||
|
||
|
||
# jumphash | ||
`import "github.com/andy2046/gopie/pkg/jumphash"` | ||
|
||
* [Overview](#pkg-overview) | ||
* [Index](#pkg-index) | ||
|
||
## <a name="pkg-overview">Overview</a> | ||
Package jumphash provides a jump consistent hash implementation. | ||
|
||
|
||
|
||
|
||
## <a name="pkg-index">Index</a> | ||
* [func Hash(key uint64, buckets int) int](#Hash) | ||
* [func HashString(key string, buckets int) int](#HashString) | ||
* [type Hasher](#Hasher) | ||
* [func New(n int) *Hasher](#New) | ||
* [func (h *Hasher) Hash(key string) int](#Hasher.Hash) | ||
* [func (h *Hasher) N() int](#Hasher.N) | ||
|
||
|
||
#### <a name="pkg-files">Package files</a> | ||
[jumphash.go](/src/github.com/andy2046/gopie/pkg/jumphash/jumphash.go) | ||
|
||
|
||
|
||
|
||
|
||
## <a name="Hash">func</a> [Hash](/src/target/jumphash.go?s=427:465#L16) | ||
``` go | ||
func Hash(key uint64, buckets int) int | ||
``` | ||
Hash takes a key and the number of buckets, returns an integer in the range [0, buckets). | ||
If the number of buckets is not greater than 1 then 1 is used. | ||
|
||
|
||
|
||
## <a name="HashString">func</a> [HashString](/src/target/jumphash.go?s=775:819#L33) | ||
``` go | ||
func HashString(key string, buckets int) int | ||
``` | ||
HashString takes string as key instead of integer and uses CRC-64 to generate key. | ||
|
||
|
||
|
||
|
||
## <a name="Hasher">type</a> [Hasher](/src/target/jumphash.go?s=1029:1060#L43) | ||
``` go | ||
type Hasher struct { | ||
// contains filtered or unexported fields | ||
} | ||
``` | ||
Hasher represents a jump consistent Hasher using a string as key. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### <a name="New">func</a> [New](/src/target/jumphash.go?s=1106:1129#L48) | ||
``` go | ||
func New(n int) *Hasher | ||
``` | ||
New returns a new instance of of Hasher. | ||
|
||
|
||
|
||
|
||
|
||
### <a name="Hasher.Hash">func</a> (\*Hasher) [Hash](/src/target/jumphash.go?s=1391:1428#L61) | ||
``` go | ||
func (h *Hasher) Hash(key string) int | ||
``` | ||
Hash returns the integer hash for the given key. | ||
|
||
|
||
|
||
|
||
### <a name="Hasher.N">func</a> (\*Hasher) [N](/src/target/jumphash.go?s=1292:1316#L56) | ||
``` go | ||
func (h *Hasher) N() int | ||
``` | ||
N returns the number of buckets the hasher can assign to. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
- - - | ||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) |
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,124 @@ | ||
|
||
|
||
# lru | ||
`import "github.com/andy2046/gopie/pkg/lru"` | ||
|
||
* [Overview](#pkg-overview) | ||
* [Index](#pkg-index) | ||
|
||
## <a name="pkg-overview">Overview</a> | ||
Package lru implements a LRU cache. | ||
|
||
|
||
|
||
|
||
## <a name="pkg-index">Index</a> | ||
* [type Cache](#Cache) | ||
* [func New(maxEntries int) *Cache](#New) | ||
* [func (c *Cache) Add(key interface{}, value interface{})](#Cache.Add) | ||
* [func (c *Cache) Clear()](#Cache.Clear) | ||
* [func (c *Cache) Get(key interface{}) (value interface{}, ok bool)](#Cache.Get) | ||
* [func (c *Cache) Len() int](#Cache.Len) | ||
* [func (c *Cache) Remove(key interface{})](#Cache.Remove) | ||
* [func (c *Cache) RemoveOldest()](#Cache.RemoveOldest) | ||
|
||
|
||
#### <a name="pkg-files">Package files</a> | ||
[lru.go](/src/github.com/andy2046/gopie/pkg/lru/lru.go) | ||
|
||
|
||
|
||
|
||
|
||
|
||
## <a name="Cache">type</a> [Cache](/src/target/lru.go?s=115:480#L10) | ||
``` go | ||
type Cache struct { | ||
// MaxEntries is the maximum number of cache entries | ||
// before an item is purged. Zero means no limit. | ||
MaxEntries int | ||
|
||
// OnPurged specificies a function to be executed | ||
// when an entry is purged from the cache. | ||
OnPurged func(key interface{}, value interface{}) | ||
// contains filtered or unexported fields | ||
} | ||
``` | ||
Cache is a LRU cache. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### <a name="New">func</a> [New](/src/target/lru.go?s=618:649#L30) | ||
``` go | ||
func New(maxEntries int) *Cache | ||
``` | ||
New creates a new cache, if maxEntries is zero, the cache has no limit. | ||
|
||
|
||
|
||
|
||
|
||
### <a name="Cache.Add">func</a> (\*Cache) [Add](/src/target/lru.go?s=881:936#L42) | ||
``` go | ||
func (c *Cache) Add(key interface{}, value interface{}) | ||
``` | ||
Add adds value to the cache. | ||
|
||
|
||
|
||
|
||
### <a name="Cache.Clear">func</a> (\*Cache) [Clear](/src/target/lru.go?s=2529:2552#L131) | ||
``` go | ||
func (c *Cache) Clear() | ||
``` | ||
Clear purges all items from the cache. | ||
|
||
|
||
|
||
|
||
### <a name="Cache.Get">func</a> (\*Cache) [Get](/src/target/lru.go?s=1355:1420#L63) | ||
``` go | ||
func (c *Cache) Get(key interface{}) (value interface{}, ok bool) | ||
``` | ||
Get looks up value by key from the cache. | ||
|
||
|
||
|
||
|
||
### <a name="Cache.Len">func</a> (\*Cache) [Len](/src/target/lru.go?s=2365:2390#L120) | ||
``` go | ||
func (c *Cache) Len() int | ||
``` | ||
Len returns the number of items in the cache. | ||
|
||
|
||
|
||
|
||
### <a name="Cache.Remove">func</a> (\*Cache) [Remove](/src/target/lru.go?s=1656:1695#L78) | ||
``` go | ||
func (c *Cache) Remove(key interface{}) | ||
``` | ||
Remove removes the provided key from the cache. | ||
|
||
|
||
|
||
|
||
### <a name="Cache.RemoveOldest">func</a> (\*Cache) [RemoveOldest](/src/target/lru.go?s=1887:1917#L91) | ||
``` go | ||
func (c *Cache) RemoveOldest() | ||
``` | ||
RemoveOldest removes the oldest item from the cache. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
- - - | ||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) |
Oops, something went wrong.