@@ -123,6 +123,12 @@ type asyncCB func()
123
123
// Option is a function on the options for a connection.
124
124
type Option func (* Options ) error
125
125
126
+ // CustomDialer can be used to specify any dialer, not necessarily
127
+ // a *net.Dialer.
128
+ type CustomDialer interface {
129
+ Dial (network , address string ) (net.Conn , error )
130
+ }
131
+
126
132
// Options can be used to create a customized connection.
127
133
type Options struct {
128
134
@@ -225,9 +231,14 @@ type Options struct {
225
231
// Token sets the token to be used when connecting to a server.
226
232
Token string
227
233
228
- // Dialer allows a custom Dialer when forming connections.
234
+ // Dialer allows a custom net.Dialer when forming connections.
235
+ // DEPRECATED: should use CustomDialer instead.
229
236
Dialer * net.Dialer
230
237
238
+ // CustomDialer allows to specify a custom dialer (not necessarily
239
+ // a *net.Dialer).
240
+ CustomDialer CustomDialer
241
+
231
242
// UseOldRequestStyle forces the old method of Requests that utilize
232
243
// a new Inbox and a new Subscription for each request.
233
244
UseOldRequestStyle bool
@@ -586,13 +597,24 @@ func Token(token string) Option {
586
597
587
598
// Dialer is an Option to set the dialer which will be used when
588
599
// attempting to establish a connection.
600
+ // DEPRECATED: Should use CustomDialer instead.
589
601
func Dialer (dialer * net.Dialer ) Option {
590
602
return func (o * Options ) error {
591
603
o .Dialer = dialer
592
604
return nil
593
605
}
594
606
}
595
607
608
+ // SetCustomDialer is an Option to set a custom dialer which will be
609
+ // used when attempting to establish a connection. If both Dialer
610
+ // and CustomDialer are specified, CustomDialer takes precedence.
611
+ func SetCustomDialer (dialer CustomDialer ) Option {
612
+ return func (o * Options ) error {
613
+ o .CustomDialer = dialer
614
+ return nil
615
+ }
616
+ }
617
+
596
618
// UseOldRequestyStyle is an Option to force usage of the old Request style.
597
619
func UseOldRequestStyle () Option {
598
620
return func (o * Options ) error {
@@ -877,7 +899,13 @@ func (nc *Conn) createConn() (err error) {
877
899
cur .lastAttempt = time .Now ()
878
900
}
879
901
880
- dialer := nc .Opts .Dialer
902
+ // CustomDialer takes precedence. If not set, use Opts.Dialer which
903
+ // is set to a default *net.Dialer (in Connect()) if not explicitly
904
+ // set by the user.
905
+ dialer := nc .Opts .CustomDialer
906
+ if dialer == nil {
907
+ dialer = nc .Opts .Dialer
908
+ }
881
909
nc .conn , err = dialer .Dial ("tcp" , nc .url .Host )
882
910
if err != nil {
883
911
return err
0 commit comments