From 96481da0a468198b3f49da68f1549a0e6df08ed4 Mon Sep 17 00:00:00 2001 From: adman Date: Wed, 29 Apr 2026 23:38:24 +0200 Subject: [PATCH] refactor: add context propagation to connection and event systems --- client/client.go | 11 ++++++++--- client/status.go | 5 +++-- event/event_bus.go | 6 +++++- proto/connection.go | 7 +++++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index 918d83e..4a84591 100644 --- a/client/client.go +++ b/client/client.go @@ -1,6 +1,7 @@ package client import ( + "context" "net" "github.com/admin-else/strom/proto" @@ -22,13 +23,17 @@ func Connect(addr string) (ret *proto.Conn, err error) { return } +func ConnectVersionLess(addr string) (ret *proto.Conn, err error) { + return ConnectVersionLessCtx(context.Background(), addr) +} + // ConnectVersionLess connects to the given address without setting the version you should set it // when using this function. // You must close the connection when you're done with it -func ConnectVersionLess(addr string) (ret *proto.Conn, err error) { - ret = proto.NewConn() +func ConnectVersionLessCtx(ctx context.Context, addr string) (ret *proto.Conn, err error) { + ret = proto.NewConnCtx(ctx) ret.Actor = proto_base.Client - ret.Conn, err = net.Dial("tcp", addr) + ret.Conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", addr) if err != nil { return } diff --git a/client/status.go b/client/status.go index 1c04ee4..195a996 100644 --- a/client/status.go +++ b/client/status.go @@ -1,6 +1,7 @@ package client import ( + "context" "encoding/json" "time" @@ -39,7 +40,7 @@ func (s *StatusClient) OnPong(p *v1_21_8.StatusToClientPacketPing) (err error) { // StatusRaw returns a StatusClient that is not connected to a server. // So ignore the resource leak warning. And maybe attach a warn ignore comment. // It does not resolve SRV records. -func StatusRaw(addr string) (s *StatusClient, err error) { +func StatusRaw(ctx context.Context, addr string) (s *StatusClient, err error) { c, err := ConnectVersionLess(addr) if err != nil { return @@ -72,7 +73,7 @@ func StatusRaw(addr string) (s *StatusClient, err error) { // StatusNoDns is like Status but does not resolve SRV records. func StatusNoDns(addr string) (status server.StatusResponse, err error) { - s, err := StatusRaw(addr) + s, err := StatusRaw(context.Background(), addr) if err != nil { return } diff --git a/event/event_bus.go b/event/event_bus.go index 91c7cd4..21d7012 100644 --- a/event/event_bus.go +++ b/event/event_bus.go @@ -236,8 +236,12 @@ func (l *Loop) StartLoop() (err error) { } func NewLoop() (l *Loop) { + return NewLoopCtx(context.Background()) +} + +func NewLoopCtx(ctx context.Context) (l *Loop) { l = &Loop{} - l.Ctx, l.Cancel = context.WithCancel(context.Background()) + l.Ctx, l.Cancel = context.WithCancel(ctx) l.HandlerFunctions = make(map[reflect.Type][]reflect.Value) return } diff --git a/proto/connection.go b/proto/connection.go index 3a6fc70..5f56cef 100644 --- a/proto/connection.go +++ b/proto/connection.go @@ -269,8 +269,11 @@ func (c *Conn) Receive() (packet proto_base.EncodeDecodeAble, err error) { } return } - func NewConn() (ret *Conn) { + return NewConnCtx(context.Background()) +} + +func NewConnCtx(ctx context.Context) (ret *Conn) { ret = &Conn{} ret.SetState(proto_base.Handshaking) ret.SetCompressionThreshold(-1) @@ -278,7 +281,7 @@ func NewConn() (ret *Conn) { if err != nil { panic(err) } - ret.Loop = event.NewLoop() + ret.Loop = event.NewLoopCtx(ctx) ret.Log = slog.Default() return }