This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package swarm | ||
|
||
import ( | ||
"github.com/libp2p/go-libp2p-swarm/dial" | ||
) | ||
|
||
type Option func(*Swarm) error | ||
|
||
// WithDialPipeline injects a custom dial pipeline in this swarm. | ||
// | ||
// To modify behaviour from the default pipeline, use NewDefaultPipeline and the accessor methods on *dial.Pipeline: | ||
// | ||
// factory := func(s *swarm.Swarm) *dial.Pipeline { | ||
// pipeline := s.NewDefaultPipeline() | ||
// | ||
// var planner dial.Planner | ||
// pipeline.SetPlanner(planner) | ||
// | ||
// prep := pipeline.Preparer() | ||
// seq := prep.(*dial.PreparerSeq) | ||
// | ||
// var newBackoff Preparer | ||
// // replace the backokff preparer. | ||
// seq.Replace("backoff", newBackoff) | ||
// | ||
// return pipeline | ||
// } | ||
// | ||
// s := swarm.NewSwarm(ctx, pid, peerstore, swarm.WithDialPipeline(factory)) | ||
func WithDialPipeline(factory func(swarm *Swarm) *dial.Pipeline) Option { | ||
return func(s *Swarm) error { | ||
s.pipeline = factory(s) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package swarm | ||
|
||
import ( | ||
addrutil "github.com/libp2p/go-addr-util" | ||
inet "github.com/libp2p/go-libp2p-net" | ||
dial "github.com/libp2p/go-libp2p-swarm/dial" | ||
transport "github.com/libp2p/go-libp2p-transport" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func (s *Swarm) NewDefaultPipeline() *dial.Pipeline { | ||
p := dial.NewPipeline(s.ctx, s, func(tc transport.Conn) (conn inet.Conn, e error) { | ||
return s.addConn(tc, inet.DirOutbound) | ||
}) | ||
|
||
// preparers. | ||
seq := new(dial.PreparerSeq) | ||
seq.AddLast("validator", dial.NewValidator(s.LocalPeer())) | ||
seq.AddLast("timeout", dial.NewRequestTimeout()) | ||
seq.AddLast("dedup", dial.NewDedup()) | ||
seq.AddLast("backoff", dial.NewBackoff()) | ||
p.SetPreparer(seq) | ||
|
||
// dial address filters. | ||
var filters []dial.AddrFilterFn | ||
|
||
// do we have a transport for dialing this address? | ||
filters = append(filters, func(addr ma.Multiaddr) bool { | ||
t := s.TransportForDialing(addr) | ||
return t != nil && t.CanDial(addr) | ||
}) | ||
|
||
// is the address blocked? | ||
filters = append(filters, (dial.AddrFilterFn)(addrutil.FilterNeg(s.Filters.AddrBlocked))) | ||
|
||
// address resolver. | ||
p.SetAddressResolver(dial.NewPeerstoreAddressResolver(s, true, filters...)) | ||
|
||
// throttler. | ||
p.SetThrottler(dial.NewDefaultThrottler()) | ||
|
||
// planner. | ||
p.SetPlanner(dial.NewImmediatePlanner()) | ||
|
||
// executor. | ||
p.SetExecutor(dial.NewExecutor(s.TransportForDialing, dial.SetJobTimeout)) | ||
|
||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters