From 17bc04b6ef33ad41afff444f9abc5858fdc43208 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 1 Apr 2021 15:36:55 +0300 Subject: [PATCH] make DialWorkerFunc, NewDialSync private they work with private data types, so there is no point in having them public --- dial_sync.go | 8 ++++---- dial_sync_test.go | 18 ++++++++---------- swarm.go | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/dial_sync.go b/dial_sync.go index 24781dd..3179016 100644 --- a/dial_sync.go +++ b/dial_sync.go @@ -13,10 +13,10 @@ import ( var errDialCanceled = errors.New("dial was aborted internally, likely due to https://git.io/Je2wW") // DialWorerFunc is used by DialSync to spawn a new dial worker -type DialWorkerFunc func(context.Context, peer.ID, <-chan dialRequest) error +type dialWorkerFunc func(context.Context, peer.ID, <-chan dialRequest) error -// NewDialSync constructs a new DialSync -func NewDialSync(worker DialWorkerFunc) *DialSync { +// newDialSync constructs a new DialSync +func newDialSync(worker dialWorkerFunc) *DialSync { return &DialSync{ dials: make(map[peer.ID]*activeDial), dialWorker: worker, @@ -28,7 +28,7 @@ func NewDialSync(worker DialWorkerFunc) *DialSync { type DialSync struct { dials map[peer.ID]*activeDial dialsLk sync.Mutex - dialWorker DialWorkerFunc + dialWorker dialWorkerFunc } type activeDial struct { diff --git a/dial_sync_test.go b/dial_sync_test.go index e5a7da6..59ace9a 100644 --- a/dial_sync_test.go +++ b/dial_sync_test.go @@ -10,7 +10,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ) -func getMockDialFunc() (DialWorkerFunc, func(), context.Context, <-chan struct{}) { +func getMockDialFunc() (dialWorkerFunc, func(), context.Context, <-chan struct{}) { dfcalls := make(chan struct{}, 512) // buffer it large enough that we won't care dialctx, cancel := context.WithCancel(context.Background()) ch := make(chan struct{}) @@ -48,7 +48,7 @@ func getMockDialFunc() (DialWorkerFunc, func(), context.Context, <-chan struct{} func TestBasicDialSync(t *testing.T) { df, done, _, callsch := getMockDialFunc() - dsync := NewDialSync(df) + dsync := newDialSync(df) p := peer.ID("testpeer") @@ -86,7 +86,7 @@ func TestBasicDialSync(t *testing.T) { func TestDialSyncCancel(t *testing.T) { df, done, _, dcall := getMockDialFunc() - dsync := NewDialSync(df) + dsync := newDialSync(df) p := peer.ID("testpeer") @@ -137,7 +137,7 @@ func TestDialSyncCancel(t *testing.T) { func TestDialSyncAllCancel(t *testing.T) { df, done, dctx, _ := getMockDialFunc() - dsync := NewDialSync(df) + dsync := newDialSync(df) p := peer.ID("testpeer") @@ -211,7 +211,7 @@ func TestFailFirst(t *testing.T) { return nil } - ds := NewDialSync(f) + ds := newDialSync(f) p := peer.ID("testing") @@ -234,7 +234,7 @@ func TestFailFirst(t *testing.T) { } func TestStressActiveDial(t *testing.T) { - ds := NewDialSync(func(ctx context.Context, p peer.ID, reqch <-chan dialRequest) error { + ds := newDialSync(func(ctx context.Context, p peer.ID, reqch <-chan dialRequest) error { go func() { for { select { @@ -279,16 +279,14 @@ func TestDialSelf(t *testing.T) { s := NewSwarm(ctx, self, nil, nil) defer s.Close() - ds := NewDialSync(s.dialWorker) - // this should fail - _, err := ds.DialLock(ctx, self) + _, err := s.dsync.DialLock(ctx, self) if err != ErrDialToSelf { t.Fatal("expected error from self dial") } // do it twice to make sure we get a new active dial object that fails again - _, err = ds.DialLock(ctx, self) + _, err = s.dsync.DialLock(ctx, self) if err != ErrDialToSelf { t.Fatal("expected error from self dial") } diff --git a/swarm.go b/swarm.go index c57c563..95b164d 100644 --- a/swarm.go +++ b/swarm.go @@ -121,7 +121,7 @@ func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc } } - s.dsync = NewDialSync(s.dialWorker) + s.dsync = newDialSync(s.dialWorker) s.limiter = newDialLimiter(s.dialAddr, s.IsFdConsumingAddr) s.proc = goprocessctx.WithContext(ctx) s.ctx = goprocessctx.OnClosingContext(s.proc) -- GitLab