Files
FastDeploy/fastdeploy/golang_router/internal/scheduler/common/counter.go
T
mouxin 0a92e96f20 [Feature] Add Golang-based Router for Request Scheduling and Load Balancing (#5882)
* [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>
2026-01-07 21:28:08 +08:00

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
}
}
}