From 21b4d14a2f6eb88ea3e05646794b802ac6d5a90c Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 6 Jul 2025 15:04:08 +0300 Subject: Fixed issues with timeout() function on the server and added automatic log rotation every midnight (if the server is running at that time) --- internal/server/server.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'internal/server') 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"} } } -- cgit v1.3.1