mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-03-21 17:39:25 +00:00
Utils/API: Replaced semaphore-mutex API
This commit is contained in:
parent
e07784a03a
commit
f22d6eaa9f
|
@ -38,7 +38,7 @@ type CustomRateLimit struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type bucket struct {
|
type bucket struct {
|
||||||
lock moreatomic.BusyMutex
|
lock moreatomic.CtxMutex
|
||||||
custom *CustomRateLimit
|
custom *CustomRateLimit
|
||||||
|
|
||||||
remaining uint64
|
remaining uint64
|
||||||
|
@ -48,6 +48,13 @@ type bucket struct {
|
||||||
lastReset time.Time // only for custom
|
lastReset time.Time // only for custom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newBucket() *bucket {
|
||||||
|
return &bucket{
|
||||||
|
lock: *moreatomic.NewCtxMutex(),
|
||||||
|
remaining: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewLimiter(prefix string) *Limiter {
|
func NewLimiter(prefix string) *Limiter {
|
||||||
return &Limiter{
|
return &Limiter{
|
||||||
Prefix: prefix,
|
Prefix: prefix,
|
||||||
|
@ -66,9 +73,7 @@ func (l *Limiter) getBucket(path string, store bool) *bucket {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
bc := &bucket{
|
bc := newBucket()
|
||||||
remaining: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, limit := range l.CustomLimits {
|
for _, limit := range l.CustomLimits {
|
||||||
if strings.Contains(path, limit.Contains) {
|
if strings.Contains(path, limit.Contains) {
|
||||||
|
@ -131,11 +136,7 @@ func (l *Limiter) Release(path string, headers http.Header) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer b.lock.Unlock()
|
||||||
// Try and lock the bucket, to prevent unlocking an unlocked lock:
|
|
||||||
b.lock.TryLock()
|
|
||||||
b.lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Check custom limiter
|
// Check custom limiter
|
||||||
if b.custom != nil {
|
if b.custom != nil {
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -7,6 +7,5 @@ require (
|
||||||
github.com/gorilla/websocket v1.4.2
|
github.com/gorilla/websocket v1.4.2
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
|
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
|
|
||||||
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
|
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -10,8 +10,6 @@ golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzht
|
||||||
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
|
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|
|
@ -2,36 +2,51 @@ package moreatomic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"golang.org/x/sync/semaphore"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BusyMutex struct {
|
type CtxMutex struct {
|
||||||
sema semaphore.Weighted
|
mut chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBusyMutex() *BusyMutex {
|
func NewCtxMutex() *CtxMutex {
|
||||||
return &BusyMutex{
|
return &CtxMutex{
|
||||||
sema: *semaphore.NewWeighted(1),
|
mut: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *BusyMutex) TryLock() bool {
|
// func (m *CtxMutex) TryLock() bool {
|
||||||
return m.sema.TryAcquire(1)
|
// select {
|
||||||
|
// case m.mut <- struct{}{}:
|
||||||
|
// return true
|
||||||
|
// default:
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *CtxMutex) IsBusy() bool {
|
||||||
|
// select {
|
||||||
|
// case m.mut <- struct{}{}:
|
||||||
|
// <-m.mut
|
||||||
|
// return false
|
||||||
|
// default:
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (m *CtxMutex) Lock(ctx context.Context) error {
|
||||||
|
select {
|
||||||
|
case m.mut <- struct{}{}:
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *BusyMutex) IsBusy() bool {
|
func (m *CtxMutex) Unlock() {
|
||||||
if !m.sema.TryAcquire(1) {
|
select {
|
||||||
return false
|
case <-m.mut:
|
||||||
|
// return
|
||||||
|
default:
|
||||||
|
panic("Unlock of already unlocked mutex.")
|
||||||
}
|
}
|
||||||
m.sema.Release(1)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BusyMutex) Lock(ctx context.Context) error {
|
|
||||||
return m.sema.Acquire(ctx, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BusyMutex) Unlock() {
|
|
||||||
m.sema.Release(1)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue