From b2fd53f4ce4ee4d9191b0a4889e747cc2bf07441 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 21 Dec 2020 09:39:02 +1100 Subject: [PATCH] Generalize internal/string-limiter Key type --- client.go | 4 ++-- .../string-limiter.go => limiter/limiter.go} | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) rename internal/{string-limiter/string-limiter.go => limiter/limiter.go} (85%) diff --git a/client.go b/client.go index 1379b692..ac90b05d 100644 --- a/client.go +++ b/client.go @@ -24,7 +24,7 @@ import ( "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 @@ type Client struct { 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 similarity index 85% rename from internal/string-limiter/string-limiter.go rename to internal/limiter/limiter.go index 8a8f91dc..1fd29db4 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 activeValueType struct { type ActiveValueRef struct { v *activeValueType - k string + k Key i *Instance } @@ -40,11 +42,11 @@ func (me ActiveValueRef) Drop() { // 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 { -- 2.48.1