From 7d9e2b18d69878d5cb4e6e276ceedeeae5598911 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 24 May 2021 17:36:39 +1000 Subject: [PATCH] Extract chansync to github.com/anacrolix/chansync --- client.go | 9 ++++-- go.mod | 1 + go.sum | 2 ++ internal/chansync/broadcast-cond.go.go | 33 -------------------- internal/chansync/interfaces.go | 8 ----- internal/chansync/set-once.go | 42 -------------------------- peer-conn-msg-writer.go | 2 +- peerconn.go | 2 +- requesting.go | 2 +- 9 files changed, 13 insertions(+), 88 deletions(-) delete mode 100644 internal/chansync/broadcast-cond.go.go delete mode 100644 internal/chansync/interfaces.go delete mode 100644 internal/chansync/set-once.go diff --git a/client.go b/client.go index 1858b848..2920c263 100644 --- 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 88326bfe..b5e8dead 100644 --- 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 c12955bc..f5eb4a2b 100644 --- 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 index 6d96d3c4..00000000 --- a/internal/chansync/broadcast-cond.go.go +++ /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 index 751ba0ef..00000000 --- a/internal/chansync/interfaces.go +++ /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 index db0e6e89..00000000 --- a/internal/chansync/set-once.go +++ /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 - } -} diff --git a/peer-conn-msg-writer.go b/peer-conn-msg-writer.go index 809d85fc..40fc27ef 100644 --- a/peer-conn-msg-writer.go +++ b/peer-conn-msg-writer.go @@ -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" ) diff --git a/peerconn.go b/peerconn.go index 46756320..b35564ac 100644 --- a/peerconn.go +++ b/peerconn.go @@ -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" diff --git a/requesting.go b/requesting.go index 7313c84a..f622b043 100644 --- a/requesting.go +++ b/requesting.go @@ -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" ) -- 2.48.1