2018-07-04 10:51:47 +00:00
|
|
|
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"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
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-06-16 20:19:45 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
// newDialSync constructs a new DialSync
|
|
|
|
func newDialSync(worker dialWorkerFunc) *DialSync {
|
2018-07-04 10:51:47 +00:00
|
|
|
return &DialSync{
|
2021-10-19 13:43:41 +00:00
|
|
|
dials: make(map[peer.ID]*activeDial),
|
|
|
|
dialWorker: worker,
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type activeDial struct {
|
2021-10-19 13:43:41 +00:00
|
|
|
id peer.ID
|
|
|
|
refCnt int
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
reqch chan dialRequest
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
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)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
ad.ds.dialsLk.Unlock()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
func (ad *activeDial) dial(ctx context.Context, p peer.ID) (*Conn, error) {
|
|
|
|
dialCtx := ad.ctx
|
2018-07-04 10:51:47 +00:00
|
|
|
|
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()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
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-06-16 20:19:45 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
func (ds *DialSync) getActiveDial(p peer.ID) (*activeDial, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
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.
|
2018-07-04 10:51:47 +00:00
|
|
|
adctx, cancel := context.WithCancel(context.Background())
|
|
|
|
actd = &activeDial{
|
|
|
|
id: p,
|
2021-10-19 13:43:41 +00:00
|
|
|
ctx: adctx,
|
2018-07-04 10:51:47 +00:00
|
|
|
cancel: cancel,
|
2021-10-19 13:43:41 +00:00
|
|
|
reqch: make(chan dialRequest),
|
2018-07-04 10:51:47 +00:00
|
|
|
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
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase ref count before dropping dialsLk
|
2021-10-19 13:43:41 +00:00
|
|
|
actd.refCnt++
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
return actd, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
defer ad.decref()
|
|
|
|
return ad.dial(ctx, p)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|