mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2026-04-23 00:17:25 +08:00
0a92e96f20
* [Feature] add golang router * [Feature] add golang router * [Feature] add golang router * [Feature] add golang router * [Feature] add golang router * [Feature] Add Golang-based Router for Request Scheduling and Load Balancing * [Feature] Add Golang-based Router for Request Scheduling and Load Balancing * [Feature] Add Golang-based Router for Request Scheduling and Load Balancing * [Feature] Add Golang-based Router for Request Scheduling and Load Balancing --------- Co-authored-by: mouxin <mouxin@baidu.com>
56 lines
787 B
Go
56 lines
787 B
Go
package common
|
|
|
|
import (
|
|
"sync/atomic"
|
|
)
|
|
|
|
type Counter struct {
|
|
count atomic.Uint64
|
|
}
|
|
|
|
func (c *Counter) Inc() {
|
|
c.count.Add(1)
|
|
}
|
|
|
|
func (c *Counter) Dec() {
|
|
c.count.Add(^uint64(0))
|
|
}
|
|
|
|
func (c *Counter) Get() uint64 {
|
|
return c.count.Load()
|
|
}
|
|
|
|
// TokenCounter records the number of tokens currently being processed by each P instance
|
|
type TokenCounter struct {
|
|
tokens atomic.Uint64
|
|
}
|
|
|
|
func (c *TokenCounter) Add(n uint64) {
|
|
c.tokens.Add(n)
|
|
}
|
|
|
|
func (c *TokenCounter) Get() uint64 {
|
|
return c.tokens.Load()
|
|
}
|
|
|
|
func (c *TokenCounter) Sub(n uint64) {
|
|
if n == 0 {
|
|
return
|
|
}
|
|
for {
|
|
old := c.tokens.Load()
|
|
if old == 0 {
|
|
return
|
|
}
|
|
var newVal uint64
|
|
if old <= n {
|
|
newVal = 0
|
|
} else {
|
|
newVal = old - n
|
|
}
|
|
if c.tokens.CompareAndSwap(old, newVal) {
|
|
return
|
|
}
|
|
}
|
|
}
|