Utils: Swapped go-csync with /x/sync/semaphore

This commit is contained in:
diamondburned (Forefront) 2020-05-06 00:32:21 -07:00
parent 450a112878
commit e07784a03a
4 changed files with 27 additions and 24 deletions

View File

@ -9,8 +9,8 @@ import (
"sync/atomic"
"time"
"github.com/diamondburned/arikawa/utils/moreatomic"
"github.com/pkg/errors"
"github.com/sasha-s/go-csync"
)
// ExtraDelay because Discord is trash. I've seen this in both litcord and
@ -38,7 +38,7 @@ type CustomRateLimit struct {
}
type bucket struct {
lock csync.Mutex
lock moreatomic.BusyMutex
custom *CustomRateLimit
remaining uint64
@ -87,8 +87,7 @@ func (l *Limiter) getBucket(path string, store bool) *bucket {
func (l *Limiter) Acquire(ctx context.Context, path string) error {
b := l.getBucket(path, true)
// Acquire lock with a timeout
if err := b.lock.CLock(ctx); err != nil {
if err := b.lock.Lock(ctx); err != nil {
return err
}

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/gorilla/schema v1.1.0
github.com/gorilla/websocket v1.4.2
github.com/pkg/errors v0.9.1
github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa
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
)

4
go.sum
View File

@ -4,14 +4,14 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa h1:xiD6U6h+QMkAwI195dFwdku2N+enlCy9XwFTnEXaCQo=
github.com/sasha-s/go-csync v0.0.0-20160729053059-3bc6c8bdb3fa/go.mod h1:KKzWrLiWu6EpzxZBPmPisPgq6oL+do2yLa0C0BTx5fA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzhtallWRSm4Q0d09pL6XbQtU=
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/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-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -1,33 +1,37 @@
package moreatomic
import "sync"
import (
"context"
"golang.org/x/sync/semaphore"
)
type BusyMutex struct {
busy Bool
mut sync.Mutex
sema semaphore.Weighted
}
func NewBusyMutex() *BusyMutex {
return &BusyMutex{
sema: *semaphore.NewWeighted(1),
}
}
func (m *BusyMutex) TryLock() bool {
if m.busy.Get() {
return false
}
m.mut.Lock()
m.busy.Set(true)
return true
return m.sema.TryAcquire(1)
}
func (m *BusyMutex) IsBusy() bool {
return m.busy.Get()
if !m.sema.TryAcquire(1) {
return false
}
m.sema.Release(1)
return true
}
func (m *BusyMutex) Lock() {
m.mut.Lock()
m.busy.Set(true)
func (m *BusyMutex) Lock(ctx context.Context) error {
return m.sema.Acquire(ctx, 1)
}
func (m *BusyMutex) Unlock() {
m.busy.Set(false)
m.mut.Unlock()
m.sema.Release(1)
}