Skip to content

Commit abcfb85

Browse files
committed
coap: update keepalive
1 parent 7f259d3 commit abcfb85

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/panjf2000/ants/v2 v2.4.3
1919
github.com/patrickmn/go-cache v2.1.0+incompatible
2020
github.com/pion/dtls/v2 v2.0.1-0.20200503085337-8e86b3a7d585
21-
github.com/plgd-dev/go-coap/v2 v2.1.4-0.20201201213140-b8c428d8fccf
21+
github.com/plgd-dev/go-coap/v2 v2.3.1-0.20210203214927-8dcfd2da7e6f
2222
github.com/stretchr/testify v1.5.1
2323
github.com/ugorji/go/codec v1.1.7
2424
github.com/valyala/fasthttp v1.12.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
100100
github.com/plgd-dev/go-coap/v2 v2.0.4-0.20200819112225-8eb712b901bc/go.mod h1:+tCi9Q78H/orWRtpVWyBgrr4vKFo2zYtbbxUllerBp4=
101101
github.com/plgd-dev/go-coap/v2 v2.1.4-0.20201201213140-b8c428d8fccf h1:am6M4meMUVVNItVjtrCZSOVUwHNz95MkkmUn8TjS4Js=
102102
github.com/plgd-dev/go-coap/v2 v2.1.4-0.20201201213140-b8c428d8fccf/go.mod h1:DccQmYY6swDlNlOCQOAX+SXTI9laSfGytskmeeNWmms=
103+
github.com/plgd-dev/go-coap/v2 v2.3.1-0.20210203214927-8dcfd2da7e6f h1:cCOPIuKfBHRM0BYtYjXws+98uWH4Zo16SHY05+E4deA=
104+
github.com/plgd-dev/go-coap/v2 v2.3.1-0.20210203214927-8dcfd2da7e6f/go.mod h1:0lg7sgOTxlHtfyGhPiak214vQ7CuBRD99LbdBCpDRW8=
103105
github.com/plgd-dev/kit v0.0.0-20200819113605-d5fcf3e94f63/go.mod h1:Yl9zisyXfPdtP9hTWlJqjJYXmgU/jtSDKttz9/CeD90=
104106
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
105107
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

net/coap/client.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
uuid "github.com/gofrs/uuid"
1818
piondtls "github.com/pion/dtls/v2"
1919
"github.com/plgd-dev/go-coap/v2/dtls"
20-
"github.com/plgd-dev/go-coap/v2/net/keepalive"
20+
"github.com/plgd-dev/go-coap/v2/net/monitor/inactivity"
2121
"github.com/plgd-dev/go-coap/v2/tcp"
2222
"github.com/plgd-dev/go-coap/v2/udp"
2323

@@ -429,7 +429,7 @@ func newClientCloseHandler(conn ClientConn, onClose *OnCloseHandler) *ClientClos
429429
type dialOptions struct {
430430
DisableTCPSignalMessageCSM bool
431431
DisablePeerTCPSignalMessageCSMs bool
432-
KeepAlive *keepalive.KeepAlive
432+
KeepaliveTimeout time.Duration
433433
errors func(err error)
434434
maxMessageSize int
435435
dialer *net.Dialer
@@ -458,7 +458,7 @@ func WithDialDisablePeerTCPSignalMessageCSMs() DialOptionFunc {
458458
// while attempting to make 3 pings during that period.
459459
func WithKeepAlive(connectionTimeout time.Duration) DialOptionFunc {
460460
return func(c dialOptions) dialOptions {
461-
c.KeepAlive = keepalive.New(keepalive.WithConfig(keepalive.MakeConfig(connectionTimeout)))
461+
c.KeepaliveTimeout = connectionTimeout
462462
return c
463463
}
464464
}
@@ -491,8 +491,11 @@ func DialUDP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
491491
cfg = o(cfg)
492492
}
493493
dopts := make([]udp.DialOption, 0, 4)
494-
if cfg.KeepAlive != nil {
495-
dopts = append(dopts, udp.WithKeepAlive(cfg.KeepAlive))
494+
if cfg.KeepaliveTimeout != 0 {
495+
dopts = append(dopts, udp.WithKeepAlive(3, cfg.KeepaliveTimeout/3, func(cc inactivity.ClientConn) {
496+
cc.Close()
497+
cfg.errors(fmt.Errorf("keep alive was reached fail limit:: closing connection"))
498+
}))
496499
}
497500
if cfg.errors != nil {
498501
dopts = append(dopts, udp.WithErrors(cfg.errors))
@@ -527,8 +530,11 @@ func DialTCP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
527530
cfg = o(cfg)
528531
}
529532
dopts := make([]tcp.DialOption, 0, 4)
530-
if cfg.KeepAlive != nil {
531-
dopts = append(dopts, tcp.WithKeepAlive(cfg.KeepAlive))
533+
if cfg.KeepaliveTimeout != 0 {
534+
dopts = append(dopts, tcp.WithKeepAlive(3, cfg.KeepaliveTimeout/3, func(cc inactivity.ClientConn) {
535+
cc.Close()
536+
cfg.errors(fmt.Errorf("keep alive was reached fail limit:: closing connection"))
537+
}))
532538
}
533539
if cfg.DisablePeerTCPSignalMessageCSMs {
534540
dopts = append(dopts, tcp.WithDisablePeerTCPSignalMessageCSMs())
@@ -606,8 +612,11 @@ func DialTCPSecure(ctx context.Context, addr string, tlsCfg *tls.Config, opts ..
606612
}
607613
dopts := make([]tcp.DialOption, 0, 4)
608614
dopts = append(dopts, tcp.WithTLS(tlsCfg))
609-
if cfg.KeepAlive != nil {
610-
dopts = append(dopts, tcp.WithKeepAlive(cfg.KeepAlive))
615+
if cfg.KeepaliveTimeout != 0 {
616+
dopts = append(dopts, tcp.WithKeepAlive(3, cfg.KeepaliveTimeout/3, func(cc inactivity.ClientConn) {
617+
cc.Close()
618+
cfg.errors(fmt.Errorf("keep alive was reached fail limit:: closing connection"))
619+
}))
611620
}
612621
if cfg.DisablePeerTCPSignalMessageCSMs {
613622
dopts = append(dopts, tcp.WithDisablePeerTCPSignalMessageCSMs())
@@ -655,8 +664,11 @@ func DialUDPSecure(ctx context.Context, addr string, dtlsCfg *piondtls.Config, o
655664
cfg = o(cfg)
656665
}
657666
dopts := make([]dtls.DialOption, 0, 4)
658-
if cfg.KeepAlive != nil {
659-
dopts = append(dopts, dtls.WithKeepAlive(cfg.KeepAlive))
667+
if cfg.KeepaliveTimeout != 0 {
668+
dopts = append(dopts, dtls.WithKeepAlive(3, cfg.KeepaliveTimeout/3, func(cc inactivity.ClientConn) {
669+
cc.Close()
670+
cfg.errors(fmt.Errorf("keep alive was reached fail limit:: closing connection"))
671+
}))
660672
}
661673
if cfg.errors != nil {
662674
dopts = append(dopts, dtls.WithErrors(cfg.errors))

0 commit comments

Comments
 (0)