summaryrefslogtreecommitdiff
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2025-07-06 15:04:08 +0300
committerKyren223 <Kyren223@proton.me>2025-07-06 15:04:08 +0300
commit21b4d14a2f6eb88ea3e05646794b802ac6d5a90c (patch)
tree70045d5e9cddef2b6c4b1a5381d85c593032d7be /internal/server/server.go
parentf80cb4a0fe26f5ebbb9658da671771dc79d416d6 (diff)
Fixed issues with timeout() function on the server and added automatic
log rotation every midnight (if the server is running at that time)
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 41eab92..35cd0f3 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -235,6 +235,8 @@ func (server *server) handleConnection(conn net.Conn) {
go func() {
defer writerWg.Done()
localCtx := context.WithoutCancel(ctx)
+ // Local context to not be effected by parent cancellation
+ // will still have a time limit upper bound, from timeout()
for request := range framer.Out {
processPacket(localCtx, sess, request)
@@ -295,8 +297,8 @@ func processPacket(ctx context.Context, sess *session.Session, pkt packet.Packet
func processRequest(ctx context.Context, sess *session.Session, request packet.Payload) packet.Payload {
slog.InfoContext(ctx, "processing request",
- ctxkeys.RequestType.String(),
- request.Type(), ctxkeys.Request.String(), request,
+ ctxkeys.PayloadType.String(),
+ request.Type(), ctxkeys.Payload.String(), request,
)
if !sess.IsTosAccepted() {
@@ -408,12 +410,14 @@ func timeout[T packet.Payload](
ctx context.Context, sess *session.Session, request T,
) packet.Payload {
// TODO: Remove the channel and just wait directly?
+ // No - We need to use a channel so timeout works properly
responseChan := make(chan packet.Payload)
- // FIXME: currently just ignoring the given context
+ // TODO: Check if this is now fixed after the rewrite:
+ // currently just ignoring the given context
// this fixes the issue where the client disconnects so the server
// doesn't bother and cancels the request
- ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
+ ctx, cancel := context.WithTimeout(ctx, timeoutDuration) // no longer ignoring
defer cancel()
go func() {
@@ -424,7 +428,10 @@ func timeout[T packet.Payload](
case response := <-responseChan:
return response
case <-ctx.Done():
- log.Println(sess.Addr(), "timeout of", request.Type(), "request")
+ slog.WarnContext(ctx, "request timeout",
+ ctxkeys.Payload.String(), request,
+ ctxkeys.PayloadType.String(), request.Type(),
+ )
return &packet.Error{Error: "request timeout"}
}
}