-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
112 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
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
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
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,81 @@ | ||
package v1 | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type token struct { | ||
rps uint32 | ||
lastUse time.Time | ||
} | ||
|
||
type TokensBucket struct { | ||
maxRPS uint32 | ||
mux sync.Mutex | ||
tokens map[string]*token | ||
unusedTokenTime time.Duration | ||
checkTokenTime time.Duration | ||
cancel atomic.Bool | ||
} | ||
|
||
func NewTokensBucket(maxRPS uint32, unusedTokenTime, checkTokenTime time.Duration) *TokensBucket { | ||
bucket := &TokensBucket{ | ||
maxRPS: maxRPS, | ||
tokens: map[string]*token{}, | ||
unusedTokenTime: unusedTokenTime, | ||
checkTokenTime: checkTokenTime, | ||
} | ||
|
||
go bucket.deleteUnusedToken() | ||
runtime.SetFinalizer(bucket, destructBasket) | ||
return bucket | ||
} | ||
|
||
func (m *TokensBucket) Obtain(id string) { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
|
||
if _, ok := m.tokens[id]; !ok { | ||
m.tokens[id] = &token{ | ||
lastUse: time.Now(), | ||
rps: 1, | ||
} | ||
return | ||
} | ||
|
||
sleepTime := time.Second - time.Since(m.tokens[id].lastUse) | ||
if sleepTime < 0 { | ||
m.tokens[id].lastUse = time.Now() | ||
m.tokens[id].rps = 0 | ||
} else if m.tokens[id].rps >= m.maxRPS { | ||
time.Sleep(sleepTime) | ||
m.tokens[id].lastUse = time.Now() | ||
m.tokens[id].rps = 0 | ||
} | ||
m.tokens[id].rps++ | ||
} | ||
|
||
func destructBasket(m *TokensBucket) { | ||
m.cancel.Store(true) | ||
} | ||
|
||
func (m *TokensBucket) deleteUnusedToken() { | ||
for { | ||
if m.cancel.Load() { | ||
return | ||
} | ||
m.mux.Lock() | ||
|
||
for id, token := range m.tokens { | ||
if time.Since(token.lastUse) >= m.unusedTokenTime { | ||
delete(m.tokens, id) | ||
} | ||
} | ||
m.mux.Unlock() | ||
|
||
time.Sleep(m.checkTokenTime) | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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