Fixed issues with timeout() function on the server and added automatic

log rotation every midnight (if the server is running at that time)
This commit is contained in:
2025-07-06 15:04:08 +03:00
parent f80cb4a0fe
commit 21b4d14a2f
2 changed files with 25 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import (
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/kyren223/eko/internal/server"
"github.com/kyren223/eko/internal/server/api"
@@ -61,8 +62,8 @@ func setupLogging() {
rotator := &lumberjack.Logger{
Filename: filepath.Join(logDir, "server.log"),
MaxSize: 1, // megabytes TODO: switch this to a more reasonable size (100?)
MaxAge: 28, // days
MaxSize: 100, // megabytes
MaxAge: 28, // days
}
level := slog.LevelDebug
@@ -78,4 +79,14 @@ func setupLogging() {
logger := slog.New(handler)
slog.SetDefault(logger)
slog.SetLogLoggerLevel(level) // TODO: remove me after fully migrating to slog
go func() {
for {
now := time.Now()
next := now.Truncate(24 * time.Hour).Add(24 * time.Hour)
time.Sleep(time.Until(next)) // sleep until next midnight
rotator.Rotate()
}
}()
}

View File

@@ -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"}
}
}