client.go | 4 ++-- internal/string-limiter/string-limiter.go => internal/limiter/limiter.go | 14 ++++++++------ diff --git a/client.go b/client.go index 1379b6929a03c8f5f983f1f279c040b43908147c..ac90b05d7304275b43c8a4eb3602de8bf1945a09 100644 --- a/client.go +++ b/client.go @@ -24,7 +24,7 @@ "github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/slices" "github.com/anacrolix/missinggo/v2/pproffd" "github.com/anacrolix/sync" - "github.com/anacrolix/torrent/internal/string-limiter" + "github.com/anacrolix/torrent/internal/limiter" "github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/webtorrent" "github.com/davecgh/go-spew/spew" @@ -80,7 +80,7 @@ numHalfOpen int websocketTrackers websocketTrackers - activeAnnounceLimiter string_limiter.Instance + activeAnnounceLimiter limiter.Instance } type ipStr string diff --git a/internal/string-limiter/string-limiter.go b/internal/limiter/limiter.go rename from internal/string-limiter/string-limiter.go rename to internal/limiter/limiter.go index 8a8f91dcd0588ad0d47d20ea8d1095ec1f0a30b3..1fd29db4300d16901586996ac1ce673b7c9ae907 100644 --- a/internal/string-limiter/string-limiter.go +++ b/internal/limiter/limiter.go @@ -1,15 +1,17 @@ -package string_limiter +package limiter import "sync" -// Manages resources with a limited number of concurrent slots for use keyed by a string. +type Key = interface{} + +// Manages resources with a limited number of concurrent slots for use for each key. type Instance struct { SlotsPerKey int mu sync.Mutex // Limits concurrent use of a resource. Push into the channel to use a slot, and receive to free // up a slot. - active map[string]*activeValueType + active map[Key]*activeValueType } type activeValueType struct { @@ -19,7 +21,7 @@ } type ActiveValueRef struct { v *activeValueType - k string + k Key i *Instance } @@ -40,11 +42,11 @@ } // Get a reference to the values for a key. You should make sure to call Drop exactly once on the // returned value when done. -func (i *Instance) GetRef(key string) ActiveValueRef { +func (i *Instance) GetRef(key Key) ActiveValueRef { i.mu.Lock() defer i.mu.Unlock() if i.active == nil { - i.active = make(map[string]*activeValueType) + i.active = make(map[Key]*activeValueType) } v, ok := i.active[key] if !ok {