You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
694 B
Go
43 lines
694 B
Go
4 years ago
|
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
|
||
|
}
|
||
|
}
|