Skip to content

Commit 445d074

Browse files
authored
Merge pull request #373 from SiaFoundation/chris/dialer
Add support for custom dialer
2 parents f535624 + 58f33e5 commit 445d074

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: minor
3+
---
4+
5+
# Allow for passing a custom Dialer to the Syncer.

syncer/syncer.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func Subnet(addr, mask string) string {
8888
}
8989

9090
type config struct {
91+
Dialer Dialer
9192
MaxInboundPeers int
9293
MaxOutboundPeers int
9394
MaxInflightRPCs int
@@ -111,6 +112,7 @@ type config struct {
111112

112113
func defaultConfig() config {
113114
return config{
115+
Dialer: &net.Dialer{},
114116
MaxInboundPeers: 64,
115117
MaxOutboundPeers: 16,
116118
MaxInflightRPCs: 64,
@@ -136,6 +138,13 @@ func defaultConfig() config {
136138
// An Option modifies a Syncer's configuration.
137139
type Option func(*config)
138140

141+
// WithDialer sets the Dialer used to dial network connections.
142+
func WithDialer(d Dialer) Option {
143+
return func(c *config) {
144+
c.Dialer = d
145+
}
146+
}
147+
139148
// WithMaxInboundPeers sets the maximum number of inbound connections. The
140149
// default is 8.
141150
func WithMaxInboundPeers(n int) Option {
@@ -237,8 +246,15 @@ func WithLogger(l *zap.Logger) Option {
237246
return func(c *config) { c.Logger = l }
238247
}
239248

249+
// A Dialer is responsible for dialing the Syncer's outgoing network
250+
// connections.
251+
type Dialer interface {
252+
DialContext(ctx context.Context, network, address string) (net.Conn, error)
253+
}
254+
240255
// A Syncer synchronizes blockchain data with peers.
241256
type Syncer struct {
257+
d Dialer
242258
l net.Listener
243259
cm ChainManager
244260
pm PeerStore
@@ -763,7 +779,7 @@ func (s *Syncer) Connect(ctx context.Context, addr string) (*Peer, error) {
763779
}
764780
defer done()
765781

766-
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", addr)
782+
conn, err := s.d.DialContext(ctx, "tcp", addr)
767783
if err != nil {
768784
return nil, err
769785
}
@@ -834,6 +850,7 @@ func New(l net.Listener, cm ChainManager, pm PeerStore, header gateway.Header, o
834850
opt(&config)
835851
}
836852
s := &Syncer{
853+
d: config.Dialer,
837854
l: l,
838855
cm: cm,
839856
pm: pm,
@@ -884,6 +901,8 @@ func RetrieveCheckpoint(ctx context.Context, peers []string, index types.ChainIn
884901
}
885902
}
886903

904+
// NOTE: we don't use the syncer's dialer here since this
905+
// operation is only performed once before creating the syncer.
887906
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", addr)
888907
if err != nil {
889908
sendResult(resp{err: err})

0 commit comments

Comments
 (0)