refactor: add context propagation to connection and event systems
This commit is contained in:
+8
-3
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/admin-else/strom/proto"
|
"github.com/admin-else/strom/proto"
|
||||||
@@ -22,13 +23,17 @@ func Connect(addr string) (ret *proto.Conn, err error) {
|
|||||||
return
|
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
|
// ConnectVersionLess connects to the given address without setting the version you should set it
|
||||||
// when using this function.
|
// when using this function.
|
||||||
// You must close the connection when you're done with it
|
// You must close the connection when you're done with it
|
||||||
func ConnectVersionLess(addr string) (ret *proto.Conn, err error) {
|
func ConnectVersionLessCtx(ctx context.Context, addr string) (ret *proto.Conn, err error) {
|
||||||
ret = proto.NewConn()
|
ret = proto.NewConnCtx(ctx)
|
||||||
ret.Actor = proto_base.Client
|
ret.Actor = proto_base.Client
|
||||||
ret.Conn, err = net.Dial("tcp", addr)
|
ret.Conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -1,6 +1,7 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"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.
|
// StatusRaw returns a StatusClient that is not connected to a server.
|
||||||
// So ignore the resource leak warning. And maybe attach a warn ignore comment.
|
// So ignore the resource leak warning. And maybe attach a warn ignore comment.
|
||||||
// It does not resolve SRV records.
|
// 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)
|
c, err := ConnectVersionLess(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -72,7 +73,7 @@ func StatusRaw(addr string) (s *StatusClient, err error) {
|
|||||||
|
|
||||||
// StatusNoDns is like Status but does not resolve SRV records.
|
// StatusNoDns is like Status but does not resolve SRV records.
|
||||||
func StatusNoDns(addr string) (status server.StatusResponse, err error) {
|
func StatusNoDns(addr string) (status server.StatusResponse, err error) {
|
||||||
s, err := StatusRaw(addr)
|
s, err := StatusRaw(context.Background(), addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -236,8 +236,12 @@ func (l *Loop) StartLoop() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewLoop() (l *Loop) {
|
func NewLoop() (l *Loop) {
|
||||||
|
return NewLoopCtx(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLoopCtx(ctx context.Context) (l *Loop) {
|
||||||
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)
|
l.HandlerFunctions = make(map[reflect.Type][]reflect.Value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -269,8 +269,11 @@ func (c *Conn) Receive() (packet proto_base.EncodeDecodeAble, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn() (ret *Conn) {
|
func NewConn() (ret *Conn) {
|
||||||
|
return NewConnCtx(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConnCtx(ctx context.Context) (ret *Conn) {
|
||||||
ret = &Conn{}
|
ret = &Conn{}
|
||||||
ret.SetState(proto_base.Handshaking)
|
ret.SetState(proto_base.Handshaking)
|
||||||
ret.SetCompressionThreshold(-1)
|
ret.SetCompressionThreshold(-1)
|
||||||
@@ -278,7 +281,7 @@ func NewConn() (ret *Conn) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
ret.Loop = event.NewLoop()
|
ret.Loop = event.NewLoopCtx(ctx)
|
||||||
ret.Log = slog.Default()
|
ret.Log = slog.Default()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user