pull all non-request scoped values out of context

This commit is contained in:
Joel Wetzell
2026-05-06 21:33:16 -05:00
parent eb25c73f3a
commit 833bd529d6
98 changed files with 328 additions and 584 deletions

View File

@@ -3,7 +3,6 @@ package module
import (
"context"
"database/sql"
"errors"
"fmt"
"log/slog"
@@ -59,13 +58,8 @@ func (t *DbSqlite) Type() string {
return t.config.Type
}
func (t *DbSqlite) Start(ctx context.Context) error {
func (t *DbSqlite) Start(ctx context.Context, router common.RouteIO) error {
t.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("db.sqlite unable to get router from context")
}
t.router = router
t.ctx = ctx

View File

@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"github.com/google/jsonschema-go/jsonschema"
@@ -99,10 +98,6 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if hs.router != nil {
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
senderAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
if err == nil {
inputContext = context.WithValue(inputContext, common.SenderContextKey, senderAddr)
}
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
if !responseWriter.done {
if aRouteFound {
@@ -160,13 +155,8 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func (hs *HTTPServer) Start(ctx context.Context) error {
func (hs *HTTPServer) Start(ctx context.Context, router common.RouteIO) error {
hs.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("http.server unable to get router from context")
}
hs.router = router
moduleContext, cancel := context.WithCancel(ctx)
hs.ctx = moduleContext

View File

@@ -4,7 +4,6 @@ package module
import (
"context"
"errors"
"fmt"
"log/slog"
@@ -60,14 +59,9 @@ func (mi *MIDIInput) Type() string {
return mi.config.Type
}
func (mi *MIDIInput) Start(ctx context.Context) error {
func (mi *MIDIInput) Start(ctx context.Context, router common.RouteIO) error {
mi.logger.Debug("running")
defer midi.CloseDriver()
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("midi.input unable to get router from context")
}
mi.router = router
moduleContext, cancel := context.WithCancel(ctx)
mi.ctx = moduleContext

View File

@@ -61,14 +61,9 @@ func (mo *MIDIOutput) Type() string {
return mo.config.Type
}
func (mo *MIDIOutput) Start(ctx context.Context) error {
func (mo *MIDIOutput) Start(ctx context.Context, router common.RouteIO) error {
mo.logger.Debug("running")
defer midi.CloseDriver()
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("midi.output unable to get router from context")
}
mo.router = router
moduleContext, cancel := context.WithCancel(ctx)
mo.ctx = moduleContext

View File

@@ -80,13 +80,8 @@ func (mc *MQTTClient) Type() string {
return mc.config.Type
}
func (mc *MQTTClient) Start(ctx context.Context) error {
func (mc *MQTTClient) Start(ctx context.Context, router common.RouteIO) error {
mc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("mqtt.client unable to get router from context")
}
mc.router = router
moduleContext, cancel := context.WithCancel(ctx)
mc.ctx = moduleContext

View File

@@ -68,14 +68,8 @@ func (nc *NATSClient) Type() string {
return nc.config.Type
}
func (nc *NATSClient) Start(ctx context.Context) error {
func (nc *NATSClient) Start(ctx context.Context, router common.RouteIO) error {
nc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("nats.client unable to get router from context")
}
nc.router = router
moduleContext, cancel := context.WithCancel(ctx)
nc.ctx = moduleContext

View File

@@ -86,14 +86,8 @@ func (ns *NATSServer) Type() string {
return ns.config.Type
}
func (ns *NATSServer) Start(ctx context.Context) error {
func (ns *NATSServer) Start(ctx context.Context, router common.RouteIO) error {
ns.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("nats.server unable to get router from context")
}
ns.router = router
moduleContext, cancel := context.WithCancel(ctx)
ns.ctx = moduleContext

View File

@@ -2,7 +2,6 @@ package module
import (
"context"
"errors"
"log/slog"
"net"
"time"
@@ -40,13 +39,8 @@ func (pc *PSNClient) Type() string {
return pc.config.Type
}
func (pc *PSNClient) Start(ctx context.Context) error {
func (pc *PSNClient) Start(ctx context.Context, router common.RouteIO) error {
pc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("psn.client unable to get router from context")
}
pc.router = router
moduleContext, cancel := context.WithCancel(ctx)
pc.ctx = moduleContext

View File

@@ -73,15 +73,9 @@ func (rc *RedisClient) Printf(ctx context.Context, format string, v ...interface
rc.logger.Debug(msg)
}
func (rc *RedisClient) Start(ctx context.Context) error {
func (rc *RedisClient) Start(ctx context.Context, router common.RouteIO) error {
redis.SetLogger(rc)
rc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("redis.client unable to get router from context")
}
rc.router = router
moduleContext, cancel := context.WithCancel(ctx)
rc.ctx = moduleContext

View File

@@ -99,14 +99,8 @@ func (sc *SerialClient) SetupPort() error {
return nil
}
func (sc *SerialClient) Start(ctx context.Context) error {
func (sc *SerialClient) Start(ctx context.Context, router common.RouteIO) error {
sc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("serial.client unable to get router from context")
}
sc.router = router
moduleContext, cancel := context.WithCancel(ctx)
sc.ctx = moduleContext

View File

@@ -131,13 +131,8 @@ func (scs *SIPCallServer) Type() string {
return scs.config.Type
}
func (scs *SIPCallServer) Start(ctx context.Context) error {
func (scs *SIPCallServer) Start(ctx context.Context, router common.RouteIO) error {
scs.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("sip.call.server unable to get router from context")
}
scs.router = router
moduleContext, cancel := context.WithCancel(ctx)
scs.ctx = moduleContext

View File

@@ -150,13 +150,8 @@ func (sds *SIPDTMFServer) Type() string {
return sds.config.Type
}
func (sds *SIPDTMFServer) Start(ctx context.Context) error {
func (sds *SIPDTMFServer) Start(ctx context.Context, router common.RouteIO) error {
sds.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("sip.dtmf.server unable to get router from context")
}
sds.router = router
moduleContext, cancel := context.WithCancel(ctx)
sds.ctx = moduleContext

View File

@@ -91,13 +91,8 @@ func (tc *TCPClient) Type() string {
return tc.config.Type
}
func (tc *TCPClient) Start(ctx context.Context) error {
func (tc *TCPClient) Start(ctx context.Context, router common.RouteIO) error {
tc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.client unable to get router from context")
}
tc.router = router
moduleContext, cancel := context.WithCancel(ctx)
tc.ctx = moduleContext

View File

@@ -166,10 +166,9 @@ ClientRead:
messages := ts.Framer.Decode(buffer[0:byteCount])
for _, message := range messages {
if ts.router != nil {
senderAddr, ok := client.RemoteAddr().(*net.TCPAddr)
_, ok := client.RemoteAddr().(*net.TCPAddr)
if ok {
senderCtx := context.WithValue(ts.ctx, common.SenderContextKey, senderAddr)
ts.router.HandleInput(senderCtx, ts.Id(), message)
ts.router.HandleInput(ts.ctx, ts.Id(), message)
} else {
ts.router.HandleInput(ts.ctx, ts.Id(), message)
}
@@ -183,13 +182,8 @@ ClientRead:
}
}
func (ts *TCPServer) Start(ctx context.Context) error {
func (ts *TCPServer) Start(ctx context.Context, router common.RouteIO) error {
ts.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.server unable to get router from context")
}
ts.router = router
moduleContext, cancel := context.WithCancel(ctx)
ts.ctx = moduleContext

View File

@@ -73,7 +73,7 @@ func TestBadDbSqlite(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("db.sqlite expected to fail")

View File

@@ -73,7 +73,7 @@ func TestBadHTTPServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("http.server expected to fail")

View File

@@ -73,7 +73,7 @@ func TestBadMIDIInput(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("midi.input expected to fail")

View File

@@ -73,7 +73,7 @@ func TestBadMIDIOutput(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("midi.output expected to fail")

View File

@@ -116,7 +116,7 @@ func TestBadMQTTClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("mqtt.client expected to fail")

View File

@@ -94,7 +94,7 @@ func TestBadNATSClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("nats.client expected to fail")

View File

@@ -71,7 +71,7 @@ func TestBadNATSServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("nats.server expected to fail")

View File

@@ -59,7 +59,7 @@ func TestBadPSNClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("psn.client expected to fail")

View File

@@ -94,7 +94,7 @@ func TestBadRedisClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("redis.client expected to fail")

View File

@@ -103,7 +103,7 @@ func TestBadSerialClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("serial.client expected to fail")

View File

@@ -88,7 +88,7 @@ func TestBadSIPCallServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("sip.call.server expected to fail")

View File

@@ -107,7 +107,7 @@ func TestBadSIPDTMFServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("sip.dtmf.server expected to fail")

View File

@@ -95,7 +95,7 @@ func TestBadTCPClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("net.tcp.client expected to fail")

View File

@@ -120,7 +120,7 @@ func TestBadTCPServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("net.tcp.server expected to fail")

View File

@@ -75,7 +75,7 @@ func TestBadTimeInterval(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("time.interval expected to fail")

View File

@@ -75,7 +75,7 @@ func TestBadTimeTimer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("time.timer expected to fail")

View File

@@ -95,7 +95,7 @@ func TestBadUDPClient(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("net.udp.client expected to fail")

View File

@@ -102,7 +102,7 @@ func TestBadUDPMulticast(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("net.udp.multicast expected to fail")

View File

@@ -99,7 +99,7 @@ func TestBadUDPServer(t *testing.T) {
return
}
err = moduleInstance.Start(t.Context())
err = moduleInstance.Start(t.Context(), nil)
if err == nil {
t.Fatalf("net.udp.server expected to fail")

View File

@@ -2,7 +2,6 @@ package module
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
@@ -58,13 +57,8 @@ func (i *TimeInterval) Type() string {
return i.config.Type
}
func (i *TimeInterval) Start(ctx context.Context) error {
func (i *TimeInterval) Start(ctx context.Context, router common.RouteIO) error {
i.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("time.interval unable to get router from context")
}
i.router = router
moduleContext, cancel := context.WithCancel(ctx)
i.ctx = moduleContext

View File

@@ -2,7 +2,6 @@ package module
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
@@ -59,13 +58,8 @@ func (t *TimeTimer) Type() string {
return t.config.Type
}
func (t *TimeTimer) Start(ctx context.Context) error {
func (t *TimeTimer) Start(ctx context.Context, router common.RouteIO) error {
t.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.tcp.client unable to get router from context")
}
t.router = router
moduleContext, cancel := context.WithCancel(ctx)
t.ctx = moduleContext

View File

@@ -79,13 +79,8 @@ func (uc *UDPClient) SetupConn() error {
return err
}
func (uc *UDPClient) Start(ctx context.Context) error {
func (uc *UDPClient) Start(ctx context.Context, router common.RouteIO) error {
uc.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.client unable to get router from context")
}
uc.router = router
moduleContext, cancel := context.WithCancel(ctx)
uc.ctx = moduleContext

View File

@@ -73,13 +73,8 @@ func (um *UDPMulticast) Type() string {
return um.config.Type
}
func (um *UDPMulticast) Start(ctx context.Context) error {
func (um *UDPMulticast) Start(ctx context.Context, router common.RouteIO) error {
um.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.multicast unable to get router from context")
}
um.router = router
moduleContext, cancel := context.WithCancel(ctx)
um.ctx = moduleContext

View File

@@ -95,13 +95,8 @@ func (us *UDPServer) Type() string {
return us.config.Type
}
func (us *UDPServer) Start(ctx context.Context) error {
func (us *UDPServer) Start(ctx context.Context, router common.RouteIO) error {
us.logger.Debug("running")
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
if !ok {
return errors.New("net.udp.server unable to get router from context")
}
us.router = router
moduleContext, cancel := context.WithCancel(ctx)
us.ctx = moduleContext
@@ -124,7 +119,7 @@ func (us *UDPServer) Start(ctx context.Context) error {
default:
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
numBytes, senderAddr, err := listener.ReadFromUDP(buffer)
numBytes, _, err := listener.ReadFromUDP(buffer)
if err != nil {
//NOTE(jwetzell) we hit deadline
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
@@ -134,8 +129,7 @@ func (us *UDPServer) Start(ctx context.Context) error {
}
message := buffer[:numBytes]
if us.router != nil {
senderCtx := context.WithValue(us.ctx, common.SenderContextKey, senderAddr)
us.router.HandleInput(senderCtx, us.Id(), message)
us.router.HandleInput(us.ctx, us.Id(), message)
} else {
us.logger.Error("input received but no router is configured")
}