]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Generalize internal/string-limiter Key type
authorMatt Joiner <anacrolix@gmail.com>
Sun, 20 Dec 2020 22:39:02 +0000 (09:39 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 20 Dec 2020 22:39:02 +0000 (09:39 +1100)
client.go
internal/limiter/limiter.go [moved from internal/string-limiter/string-limiter.go with 85% similarity]

index 1379b6929a03c8f5f983f1f279c040b43908147c..ac90b05d7304275b43c8a4eb3602de8bf1945a09 100644 (file)
--- 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
similarity index 85%
rename from internal/string-limiter/string-limiter.go
rename to internal/limiter/limiter.go
index 8a8f91dcd0588ad0d47d20ea8d1095ec1f0a30b3..1fd29db4300d16901586996ac1ce673b7c9ae907 100644 (file)
@@ -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 {