Skip to content

Commit

Permalink
fix: increase keep alive for connection
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru authored and dborovcanin committed Apr 30, 2024
1 parent cf60205 commit c5a9fc2
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions internal/server/coap/coap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
"time"

"github.com/absmach/magistrala/internal/server"
gocoap "github.com/plgd-dev/go-coap/v3"
"github.com/plgd-dev/go-coap/v3/mux"
"github.com/plgd-dev/go-coap/v3/net"
"github.com/plgd-dev/go-coap/v3/options"
"github.com/plgd-dev/go-coap/v3/udp"
udpClient "github.com/plgd-dev/go-coap/v3/udp/client"
)

const (
stopWaitTime = 5 * time.Second
)
// const (
// stopWaitTime = 5 * time.Second
// )

type Server struct {
server.BaseServer
Expand Down Expand Up @@ -44,26 +47,51 @@ func (s *Server) Start() error {
errCh := make(chan error)
s.Logger.Info(fmt.Sprintf("%s service started using http, exposed port %s", s.Name, s.Address))
s.Logger.Info(fmt.Sprintf("%s service %s server listening at %s without TLS", s.Name, s.Protocol, s.Address))
l, err := net.NewListenUDP("udp", s.Address)
if err != nil {
return err
}
defer l.Close()

s.Logger.Info(fmt.Sprintf("CoAP proxy server started at %s without DTLS", s.Address))

dummyInactiveFunc := func(cc *udpClient.Conn) {
// This function intentionally left blank.
}
cs := udp.NewServer(
options.WithMux(mux.HandlerFunc(s.handler)),
options.WithKeepAlive(10, 10*time.Minute, dummyInactiveFunc),
)

go func() {
errCh <- gocoap.ListenAndServe("udp", s.Address, s.handler)
errCh <- cs.Serve(l)
}()

select {
case <-s.Ctx.Done():
return s.Stop()
s.Logger.Info(fmt.Sprintf("CoAP proxy server at %s without DTLS exiting ...", s.Address))
l.Close()
case err := <-errCh:
s.Logger.Error(fmt.Sprintf("CoAP proxy server at %s without DTLS exiting with errors: %s", s.Address, err.Error()))
return err
}
return nil
// select {
// case <-s.Ctx.Done():
// return s.Stop()
// case err := <-errCh:
// return err
// }
}

func (s *Server) Stop() error {
defer s.Cancel()
c := make(chan bool)
defer close(c)
select {
case <-c:
case <-time.After(stopWaitTime):
}
s.Logger.Info(fmt.Sprintf("%s service shutdown of http at %s", s.Name, s.Address))
// defer s.Cancel()
// c := make(chan bool)
// defer close(c)
// select {
// case <-c:
// case <-time.After(stopWaitTime):
// }
// s.Logger.Info(fmt.Sprintf("%s service shutdown of http at %s", s.Name, s.Address))
return nil
}

0 comments on commit c5a9fc2

Please sign in to comment.