shutdown tcp server gracefully with router

This commit is contained in:
Joel Wetzell
2025-11-22 12:03:26 -06:00
parent 51e656313c
commit 1791aa17b1

View File

@@ -1,10 +1,11 @@
package showbridge package showbridge
import ( import (
"context"
"fmt" "fmt"
"log/slog" "log/slog"
"net" "net"
"sync"
"time"
"github.com/jwetzell/showbridge-go/internal/framing" "github.com/jwetzell/showbridge-go/internal/framing"
) )
@@ -14,6 +15,8 @@ type TCPServer struct {
Port uint16 Port uint16
framingMethod string framingMethod string
router *Router router *Router
quit chan interface{}
wg sync.WaitGroup
} }
func init() { func init() {
@@ -43,7 +46,7 @@ func init() {
return nil, fmt.Errorf("tcp framing method must be a string") return nil, fmt.Errorf("tcp framing method must be a string")
} }
return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), config: config}, nil return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), config: config, quit: make(chan interface{})}, nil
}, },
}) })
} }
@@ -60,9 +63,9 @@ func (ts *TCPServer) RegisterRouter(router *Router) {
ts.router = router ts.router = router
} }
func (ts *TCPServer) HandleClient(ctx context.Context, client net.Conn) { func (ts *TCPServer) handleClient(client net.Conn) {
slog.Debug("connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String()) slog.Debug("connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
defer client.Close()
var framer framing.Framer var framer framing.Framer
switch ts.framingMethod { switch ts.framingMethod {
@@ -77,14 +80,20 @@ func (ts *TCPServer) HandleClient(ctx context.Context, client net.Conn) {
} }
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
ClientRead:
for { for {
select { select {
case <-ctx.Done(): case <-ts.quit:
return return
default: default:
client.SetDeadline(time.Now().Add(time.Millisecond * 200))
byteCount, err := client.Read(buffer) byteCount, err := client.Read(buffer)
if err != nil { if err != nil {
//NOTE(jwetzell) we hit deadline
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue ClientRead
}
if err.Error() == "EOF" { if err.Error() == "EOF" {
slog.Debug("connection closed", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String()) slog.Debug("connection closed", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
} }
@@ -103,35 +112,44 @@ func (ts *TCPServer) HandleClient(ctx context.Context, client net.Conn) {
} }
} }
} }
} }
} }
func (ts TCPServer) Run() error { func (ts *TCPServer) Run() error {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", ts.Port)) listener, err := net.Listen("tcp", fmt.Sprintf(":%d", ts.Port))
if err != nil { if err != nil {
return err return err
} }
ts.wg.Add(1)
// TODO(jwetzell): shutdown with router.Context properly
go func() { go func() {
<-ts.router.Context.Done() <-ts.router.Context.Done()
slog.Debug("router context done in module", "id", ts.config.Id) close(ts.quit)
listener.Close() listener.Close()
slog.Debug("router context done in module", "id", ts.config.Id)
}() }()
AcceptLoop:
for { for {
select { conn, err := listener.Accept()
case <-ts.router.Context.Done():
return nil
default:
client, err := listener.Accept()
if err != nil { if err != nil {
return err select {
case <-ts.quit:
break AcceptLoop
default:
slog.Debug("net.tcp.server problem with listener", "error", err)
} }
go ts.HandleClient(ts.router.Context, client) } else {
ts.wg.Add(1)
go func() {
ts.handleClient(conn)
ts.wg.Done()
}()
} }
} }
ts.wg.Done()
ts.wg.Wait()
return nil
} }
func (ts *TCPServer) Output(payload any) error { func (ts *TCPServer) Output(payload any) error {