]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Extract chansync to github.com/anacrolix/chansync
authorMatt Joiner <anacrolix@gmail.com>
Mon, 24 May 2021 07:36:39 +0000 (17:36 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 7 Jun 2021 03:01:40 +0000 (13:01 +1000)
client.go
go.mod
go.sum
internal/chansync/broadcast-cond.go.go [deleted file]
internal/chansync/interfaces.go [deleted file]
internal/chansync/set-once.go [deleted file]
peer-conn-msg-writer.go
peerconn.go
requesting.go

index 1858b84816a5e064cd6c9d5579b1e0c564d008c6..2920c2630da4784c8cd7e8841201524882d2a548 100644 (file)
--- a/client.go
+++ b/client.go
@@ -32,8 +32,9 @@ import (
        "golang.org/x/time/rate"
        "golang.org/x/xerrors"
 
+       "github.com/anacrolix/chansync"
+
        "github.com/anacrolix/torrent/bencode"
-       "github.com/anacrolix/torrent/internal/chansync"
        "github.com/anacrolix/torrent/internal/limiter"
        "github.com/anacrolix/torrent/iplist"
        "github.com/anacrolix/torrent/metainfo"
@@ -260,7 +261,7 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
                                if err != nil {
                                        panic(err)
                                }
-                               cl.dhtServers = append(cl.dhtServers, anacrolixDhtServerWrapper{ds})
+                               cl.dhtServers = append(cl.dhtServers, AnacrolixDhtServerWrapper{ds})
                                cl.onClose = append(cl.onClose, func() { ds.Close() })
                        }
                }
@@ -314,6 +315,10 @@ func (cl *Client) AddDialer(d Dialer) {
        }
 }
 
+func (cl *Client) Listeners() []Listener {
+       return cl.listeners
+}
+
 // Registers a Listener, and starts Accepting on it. You must Close Listeners provided this way
 // yourself.
 func (cl *Client) AddListener(l Listener) {
diff --git a/go.mod b/go.mod
index 88326bfea5a71bba9810f49181248b0b9e394675..b5e8dead74008ed945be77450b627356cb34aec0 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ require (
        crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508
        github.com/RoaringBitmap/roaring v0.6.0 // indirect
        github.com/alexflint/go-arg v1.3.0
+       github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92 // indirect
        github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c // indirect
        github.com/anacrolix/dht/v2 v2.9.1
        github.com/anacrolix/envpprof v1.1.1
diff --git a/go.sum b/go.sum
index c12955bc83396437adeededfd84e2828daad306d..f5eb4a2ba54e140b7c48dae89bea989864b54cef 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -56,6 +56,8 @@ github.com/alexflint/go-arg v1.3.0 h1:UfldqSdFWeLtoOuVRosqofU4nmhI1pYEbT4ZFS34Bd
 github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
 github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
 github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
+github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92 h1:WGk37RyXPWcIALJxTkTNrXN3yLQp7hSFa3x5GkrK/Rs=
+github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=
 github.com/anacrolix/confluence v1.7.1-0.20210221224747-9cb14aa2c53a/go.mod h1:T0JHvSaf9UfoiUdCtCOUuRroHm/tauUJTbLc6/vd5YA=
 github.com/anacrolix/confluence v1.7.1-0.20210221225853-90405640e928/go.mod h1:NoLcfoRet+kYttjLXJRmh4qBVrylJsfIItik5GGj21A=
 github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c h1:HfbeiZS/0hwdotwtQhllrd3PagmuLgCN9O8CHJgzPGQ=
diff --git a/internal/chansync/broadcast-cond.go.go b/internal/chansync/broadcast-cond.go.go
deleted file mode 100644 (file)
index 6d96d3c..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-package chansync
-
-import (
-       "github.com/anacrolix/sync"
-)
-
-// Can be used as zero-value. Due to the caller needing to bring their own synchronization, an
-// eqiuvalent to "sync".Cond.Signal is not provided. BroadcastCond is intended to be selected on
-// with other channels.
-type BroadcastCond struct {
-       mu sync.Mutex
-       ch chan struct{}
-}
-
-func (me *BroadcastCond) Broadcast() {
-       me.mu.Lock()
-       defer me.mu.Unlock()
-       if me.ch != nil {
-               close(me.ch)
-               me.ch = nil
-       }
-}
-
-// Should be called before releasing locks on resources that might trigger subsequent Broadcasts.
-// The channel is closed when the condition changes.
-func (me *BroadcastCond) Signaled() Signaled {
-       me.mu.Lock()
-       defer me.mu.Unlock()
-       if me.ch == nil {
-               me.ch = make(chan struct{})
-       }
-       return me.ch
-}
diff --git a/internal/chansync/interfaces.go b/internal/chansync/interfaces.go
deleted file mode 100644 (file)
index 751ba0e..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-package chansync
-
-// Here we'll strongly-type channels to assist correct usage, if possible.
-
-type (
-       Signaled <-chan struct{}
-       Done     <-chan struct{}
-)
diff --git a/internal/chansync/set-once.go b/internal/chansync/set-once.go
deleted file mode 100644 (file)
index db0e6e8..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-package chansync
-
-import "sync"
-
-// SetOnce is a boolean value that can only be flipped from false to true.
-type SetOnce struct {
-       ch        chan struct{}
-       initOnce  sync.Once
-       closeOnce sync.Once
-}
-
-// Returns a channel that is closed when the event is flagged.
-func (me *SetOnce) Done() Done {
-       me.init()
-       return me.ch
-}
-
-func (me *SetOnce) init() {
-       me.initOnce.Do(func() {
-               me.ch = make(chan struct{})
-       })
-}
-
-// Set only returns true the first time it is called.
-func (me *SetOnce) Set() (first bool) {
-       me.closeOnce.Do(func() {
-               me.init()
-               first = true
-               close(me.ch)
-       })
-       return
-}
-
-func (me *SetOnce) IsSet() bool {
-       me.init()
-       select {
-       case <-me.ch:
-               return true
-       default:
-               return false
-       }
-}
index 809d85fcada6ee2f42ecc7ac13ad8e7e6fe6d1af..40fc27ef122e83a70b49a861fb26fc94a4c1b6a8 100644 (file)
@@ -5,10 +5,10 @@ import (
        "io"
        "time"
 
+       "github.com/anacrolix/chansync"
        "github.com/anacrolix/log"
        "github.com/anacrolix/sync"
 
-       "github.com/anacrolix/torrent/internal/chansync"
        pp "github.com/anacrolix/torrent/peer_protocol"
 )
 
index 46756320a2ad52fb9e48c29974d209583e96fa06..b35564ac83aa47d817904327c9bdb763136ff530 100644 (file)
@@ -19,8 +19,8 @@ import (
        "github.com/anacrolix/missinggo/v2/prioritybitmap"
        "github.com/anacrolix/multiless"
 
+       "github.com/anacrolix/chansync"
        "github.com/anacrolix/torrent/bencode"
-       "github.com/anacrolix/torrent/internal/chansync"
        "github.com/anacrolix/torrent/metainfo"
        "github.com/anacrolix/torrent/mse"
        pp "github.com/anacrolix/torrent/peer_protocol"
index 7313c84a3fa1d6fff04aed1ad43364a7efd2a9d7..f622b0430692987b235a0e0b8fa6bf4eb6c741be 100644 (file)
@@ -6,7 +6,7 @@ import (
 
        "github.com/anacrolix/missinggo/v2/bitmap"
 
-       "github.com/anacrolix/torrent/internal/chansync"
+       "github.com/anacrolix/chansync"
        request_strategy "github.com/anacrolix/torrent/request-strategy"
        "github.com/anacrolix/torrent/types"
 )