refactor: add context propagation to connection and event systems
This commit is contained in:
+8
-3
@@ -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
|
||||
}
|
||||
|
||||
+3
-2
@@ -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
|
||||
}
|
||||
|
||||
+5
-1
@@ -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
|
||||
}
|
||||
|
||||
+5
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user