Skip to content

Commit c7215a2

Browse files
committed
coap: allow to set dialer option
1 parent 51615c9 commit c7215a2

1 file changed

Lines changed: 44 additions & 21 deletions

File tree

net/coap/client.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ type dialOptions struct {
432432
KeepAlive *keepalive.KeepAlive
433433
errors func(err error)
434434
maxMessageSize int
435-
timeout time.Duration
435+
dialer *net.Dialer
436436
}
437437

438438
type DialOptionFunc func(dialOptions) dialOptions
@@ -477,6 +477,13 @@ func WithMaxMessageSize(maxMessageSize int) DialOptionFunc {
477477
}
478478
}
479479

480+
func WithDialer(dialer *net.Dialer) DialOptionFunc {
481+
return func(c dialOptions) dialOptions {
482+
c.dialer = dialer
483+
return c
484+
}
485+
}
486+
480487
func DialUDP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientCloseHandler, error) {
481488
h := NewOnCloseHandler()
482489
var cfg dialOptions
@@ -493,11 +500,15 @@ func DialUDP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
493500
if cfg.maxMessageSize > 0 {
494501
dopts = append(dopts, udp.WithMaxMessageSize(cfg.maxMessageSize))
495502
}
496-
deadline, ok := ctx.Deadline()
497-
if ok {
498-
dopts = append(dopts, udp.WithDialer(&net.Dialer{
499-
Timeout: deadline.Sub(time.Now()),
500-
}))
503+
if cfg.dialer != nil {
504+
dopts = append(dopts, udp.WithDialer(cfg.dialer))
505+
} else {
506+
deadline, ok := ctx.Deadline()
507+
if ok {
508+
dopts = append(dopts, udp.WithDialer(&net.Dialer{
509+
Timeout: deadline.Sub(time.Now()),
510+
}))
511+
}
501512
}
502513
c, err := udp.Dial(addr, dopts...)
503514
if err != nil {
@@ -531,11 +542,15 @@ func DialTCP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
531542
if cfg.maxMessageSize > 0 {
532543
dopts = append(dopts, tcp.WithMaxMessageSize(cfg.maxMessageSize))
533544
}
534-
deadline, ok := ctx.Deadline()
535-
if ok {
536-
dopts = append(dopts, tcp.WithDialer(&net.Dialer{
537-
Timeout: deadline.Sub(time.Now()),
538-
}))
545+
if cfg.dialer != nil {
546+
dopts = append(dopts, tcp.WithDialer(cfg.dialer))
547+
} else {
548+
deadline, ok := ctx.Deadline()
549+
if ok {
550+
dopts = append(dopts, tcp.WithDialer(&net.Dialer{
551+
Timeout: deadline.Sub(time.Now()),
552+
}))
553+
}
539554
}
540555
c, err := tcp.Dial(addr, dopts...)
541556
if err != nil {
@@ -606,11 +621,15 @@ func DialTCPSecure(ctx context.Context, addr string, tlsCfg *tls.Config, opts ..
606621
if cfg.maxMessageSize > 0 {
607622
dopts = append(dopts, tcp.WithMaxMessageSize(cfg.maxMessageSize))
608623
}
609-
deadline, ok := ctx.Deadline()
610-
if ok {
611-
dopts = append(dopts, tcp.WithDialer(&net.Dialer{
612-
Timeout: deadline.Sub(time.Now()),
613-
}))
624+
if cfg.dialer != nil {
625+
dopts = append(dopts, tcp.WithDialer(cfg.dialer))
626+
} else {
627+
deadline, ok := ctx.Deadline()
628+
if ok {
629+
dopts = append(dopts, tcp.WithDialer(&net.Dialer{
630+
Timeout: deadline.Sub(time.Now()),
631+
}))
632+
}
614633
}
615634
c, err := tcp.Dial(addr, dopts...)
616635
if err != nil {
@@ -645,11 +664,15 @@ func DialUDPSecure(ctx context.Context, addr string, dtlsCfg *piondtls.Config, o
645664
if cfg.maxMessageSize > 0 {
646665
dopts = append(dopts, dtls.WithMaxMessageSize(cfg.maxMessageSize))
647666
}
648-
deadline, ok := ctx.Deadline()
649-
if ok {
650-
dopts = append(dopts, dtls.WithDialer(&net.Dialer{
651-
Timeout: deadline.Sub(time.Now()),
652-
}))
667+
if cfg.dialer != nil {
668+
dopts = append(dopts, dtls.WithDialer(cfg.dialer))
669+
} else {
670+
deadline, ok := ctx.Deadline()
671+
if ok {
672+
dopts = append(dopts, dtls.WithDialer(&net.Dialer{
673+
Timeout: deadline.Sub(time.Now()),
674+
}))
675+
}
653676
}
654677
c, err := dtls.Dial(addr, dtlsCfg, dopts...)
655678
if err != nil {

0 commit comments

Comments
 (0)