Skip to content

Commit 9adcf8f

Browse files
only check for self dials once
1 parent e5b2a35 commit 9adcf8f

File tree

4 files changed

+10
-47
lines changed

4 files changed

+10
-47
lines changed

p2p/net/swarm/dial_sync.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/libp2p/go-libp2p-core/peer"
99
)
1010

11-
// DialWorerFunc is used by DialSync to spawn a new dial worker
12-
type dialWorkerFunc func(peer.ID, <-chan dialRequest) error
11+
// dialWorkerFunc is used by DialSync to spawn a new dial worker
12+
type dialWorkerFunc func(peer.ID, <-chan dialRequest)
1313

1414
// newDialSync constructs a new DialSync
1515
func newDialSync(worker dialWorkerFunc) *DialSync {
@@ -93,12 +93,7 @@ func (ds *DialSync) getActiveDial(p peer.ID) (*activeDial, error) {
9393
reqch: make(chan dialRequest),
9494
ds: ds,
9595
}
96-
97-
if err := ds.dialWorker(p, actd.reqch); err != nil {
98-
cancel()
99-
return nil, err
100-
}
101-
96+
ds.dialWorker(p, actd.reqch)
10297
ds.dials[p] = actd
10398
}
10499

p2p/net/swarm/dial_sync_test.go

+4-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func getMockDialFunc() (dialWorkerFunc, func(), context.Context, <-chan struct{}
1515
dfcalls := make(chan struct{}, 512) // buffer it large enough that we won't care
1616
dialctx, cancel := context.WithCancel(context.Background())
1717
ch := make(chan struct{})
18-
f := func(p peer.ID, reqch <-chan dialRequest) error {
18+
f := func(p peer.ID, reqch <-chan dialRequest) {
1919
defer cancel()
2020
dfcalls <- struct{}{}
2121
go func() {
@@ -24,7 +24,6 @@ func getMockDialFunc() (dialWorkerFunc, func(), context.Context, <-chan struct{}
2424
req.resch <- dialResponse{conn: new(Conn)}
2525
}
2626
}()
27-
return nil
2827
}
2928

3029
var once sync.Once
@@ -162,7 +161,7 @@ func TestDialSyncAllCancel(t *testing.T) {
162161

163162
func TestFailFirst(t *testing.T) {
164163
var count int32
165-
f := func(p peer.ID, reqch <-chan dialRequest) error {
164+
f := func(p peer.ID, reqch <-chan dialRequest) {
166165
go func() {
167166
for {
168167
req, ok := <-reqch
@@ -178,33 +177,29 @@ func TestFailFirst(t *testing.T) {
178177
atomic.AddInt32(&count, 1)
179178
}
180179
}()
181-
return nil
182180
}
183181

184182
ds := newDialSync(f)
185-
186183
p := peer.ID("testing")
187184

188185
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
189186
defer cancel()
190187

191-
_, err := ds.Dial(ctx, p)
192-
if err == nil {
188+
if _, err := ds.Dial(ctx, p); err == nil {
193189
t.Fatal("expected gophers to have eaten the modem")
194190
}
195191

196192
c, err := ds.Dial(ctx, p)
197193
if err != nil {
198194
t.Fatal(err)
199195
}
200-
201196
if c == nil {
202197
t.Fatal("should have gotten a 'real' conn back")
203198
}
204199
}
205200

206201
func TestStressActiveDial(t *testing.T) {
207-
ds := newDialSync(func(p peer.ID, reqch <-chan dialRequest) error {
202+
ds := newDialSync(func(p peer.ID, reqch <-chan dialRequest) {
208203
go func() {
209204
for {
210205
req, ok := <-reqch
@@ -214,7 +209,6 @@ func TestStressActiveDial(t *testing.T) {
214209
req.resch <- dialResponse{}
215210
}
216211
}()
217-
return nil
218212
})
219213

220214
wg := sync.WaitGroup{}
@@ -235,24 +229,3 @@ func TestStressActiveDial(t *testing.T) {
235229

236230
wg.Wait()
237231
}
238-
239-
func TestDialSelf(t *testing.T) {
240-
ctx, cancel := context.WithCancel(context.Background())
241-
defer cancel()
242-
243-
self := peer.ID("ABC")
244-
s := NewSwarm(ctx, self, nil, nil)
245-
defer s.Close()
246-
247-
// this should fail
248-
_, err := s.dsync.Dial(ctx, self)
249-
if err != ErrDialToSelf {
250-
t.Fatal("expected error from self dial")
251-
}
252-
253-
// do it twice to make sure we get a new active dial object that fails again
254-
_, err = s.dsync.Dial(ctx, self)
255-
if err != ErrDialToSelf {
256-
t.Fatal("expected error from self dial")
257-
}
258-
}

p2p/net/swarm/dial_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ func TestDialSimultaneousJoin(t *testing.T) {
672672
}
673673
}
674674

675-
func TestDialSelf2(t *testing.T) {
675+
func TestDialSelf(t *testing.T) {
676676
ctx, cancel := context.WithCancel(context.Background())
677677
defer cancel()
678678

p2p/net/swarm/swarm_dial.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,9 @@ type dialResponse struct {
294294
err error
295295
}
296296

297-
// startDialWorker starts an active dial goroutine that synchronizes and executes concurrent dials
298-
func (s *Swarm) startDialWorker(p peer.ID, reqch <-chan dialRequest) error {
299-
if p == s.local {
300-
return ErrDialToSelf
301-
}
302-
297+
// startDialWorker starts an active dial goroutine that synchronizes and executes concurrent dials to a single peer
298+
func (s *Swarm) startDialWorker(p peer.ID, reqch <-chan dialRequest) {
303299
go s.dialWorkerLoop(p, reqch)
304-
return nil
305300
}
306301

307302
func (s *Swarm) dialWorkerLoop(p peer.ID, reqch <-chan dialRequest) {

0 commit comments

Comments
 (0)