initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

42
core/syncx/limit.go Normal file
View File

@@ -0,0 +1,42 @@
package syncx
import (
"errors"
"zero/core/lang"
)
var ErrReturn = errors.New("discarding limited token, resource pool is full, someone returned multiple times")
type Limit struct {
pool chan lang.PlaceholderType
}
func NewLimit(n int) Limit {
return Limit{
pool: make(chan lang.PlaceholderType, n),
}
}
func (l Limit) Borrow() {
l.pool <- lang.Placeholder
}
// Return returns the borrowed resource, returns error only if returned more than borrowed.
func (l Limit) Return() error {
select {
case <-l.pool:
return nil
default:
return ErrReturn
}
}
func (l Limit) TryBorrow() bool {
select {
case l.pool <- lang.Placeholder:
return true
default:
return false
}
}