status-go/vendor/github.com/libp2p/go-libp2p-swarm/dial_sync.go

123 lines
2.7 KiB
Go
Raw Normal View History

package swarm
import (
"context"
"sync"
2021-10-19 13:43:41 +00:00
"github.com/libp2p/go-libp2p-core/network"
2019-06-09 07:24:20 +00:00
"github.com/libp2p/go-libp2p-core/peer"
)
2021-10-19 13:43:41 +00:00
// DialWorerFunc is used by DialSync to spawn a new dial worker
type dialWorkerFunc func(context.Context, peer.ID, <-chan dialRequest) error
2021-10-19 13:43:41 +00:00
// newDialSync constructs a new DialSync
func newDialSync(worker dialWorkerFunc) *DialSync {
return &DialSync{
2021-10-19 13:43:41 +00:00
dials: make(map[peer.ID]*activeDial),
dialWorker: worker,
}
}
// DialSync is a dial synchronization helper that ensures that at most one dial
// to any given peer is active at any given time.
type DialSync struct {
2021-10-19 13:43:41 +00:00
dials map[peer.ID]*activeDial
dialsLk sync.Mutex
dialWorker dialWorkerFunc
}
type activeDial struct {
2021-10-19 13:43:41 +00:00
id peer.ID
refCnt int
ctx context.Context
cancel func()
2021-10-19 13:43:41 +00:00
reqch chan dialRequest
ds *DialSync
}
2021-10-19 13:43:41 +00:00
func (ad *activeDial) decref() {
ad.ds.dialsLk.Lock()
ad.refCnt--
if ad.refCnt == 0 {
ad.cancel()
close(ad.reqch)
delete(ad.ds.dials, ad.id)
}
2021-10-19 13:43:41 +00:00
ad.ds.dialsLk.Unlock()
}
2021-10-19 13:43:41 +00:00
func (ad *activeDial) dial(ctx context.Context, p peer.ID) (*Conn, error) {
dialCtx := ad.ctx
2021-10-19 13:43:41 +00:00
if forceDirect, reason := network.GetForceDirectDial(ctx); forceDirect {
dialCtx = network.WithForceDirectDial(dialCtx, reason)
}
if simConnect, reason := network.GetSimultaneousConnect(ctx); simConnect {
dialCtx = network.WithSimultaneousConnect(dialCtx, reason)
}
resch := make(chan dialResponse, 1)
select {
case ad.reqch <- dialRequest{ctx: dialCtx, resch: resch}:
case <-ctx.Done():
return nil, ctx.Err()
}
2021-10-19 13:43:41 +00:00
select {
case res := <-resch:
return res.conn, res.err
case <-ctx.Done():
return nil, ctx.Err()
}
}
2021-10-19 13:43:41 +00:00
func (ds *DialSync) getActiveDial(p peer.ID) (*activeDial, error) {
ds.dialsLk.Lock()
defer ds.dialsLk.Unlock()
actd, ok := ds.dials[p]
if !ok {
2021-10-19 13:43:41 +00:00
// This code intentionally uses the background context. Otherwise, if the first call
// to Dial is canceled, subsequent dial calls will also be canceled.
// XXX: this also breaks direct connection logic. We will need to pipe the
// information through some other way.
adctx, cancel := context.WithCancel(context.Background())
actd = &activeDial{
id: p,
2021-10-19 13:43:41 +00:00
ctx: adctx,
cancel: cancel,
2021-10-19 13:43:41 +00:00
reqch: make(chan dialRequest),
ds: ds,
}
2021-10-19 13:43:41 +00:00
err := ds.dialWorker(adctx, p, actd.reqch)
if err != nil {
cancel()
return nil, err
}
ds.dials[p] = actd
}
// increase ref count before dropping dialsLk
2021-10-19 13:43:41 +00:00
actd.refCnt++
2021-10-19 13:43:41 +00:00
return actd, nil
}
// DialLock initiates a dial to the given peer if there are none in progress
// then waits for the dial to that peer to complete.
func (ds *DialSync) DialLock(ctx context.Context, p peer.ID) (*Conn, error) {
2021-10-19 13:43:41 +00:00
ad, err := ds.getActiveDial(p)
if err != nil {
return nil, err
}
2021-10-19 13:43:41 +00:00
defer ad.decref()
return ad.dial(ctx, p)
}