add context TODO

This commit is contained in:
2025-08-01 12:56:09 +03:00
parent 6ed5a7f9e0
commit 83912b6c28
3 changed files with 18 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package gateway
import (
"context"
"encoding/json"
"io"
"log/slog"
@@ -13,6 +14,8 @@ import (
)
func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() // TODO
w.Header().Set("Content-Type", "application/json")
sessionUUID := r.Header.Get("X-Session-UUID")
if sessionUUID == "" {
@@ -64,7 +67,11 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrParseErrorS))
return
}
resp := gs.Route(sessionUUID, r, &single)
resp := gs.Route(ctx, sessionUUID, r, &single)
if resp == nil {
w.Write([]byte(""))
return
}
rpc.WriteResponse(w, resp)
return
}
@@ -76,7 +83,7 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
wg.Add(1)
go func(req rpc.RPCRequest) {
defer wg.Done()
res := gs.Route(sessionUUID, r, &req)
res := gs.Route(ctx, sessionUUID, r, &req)
if res != nil {
responses <- *res
}
@@ -91,10 +98,12 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
}
if len(result) > 0 {
json.NewEncoder(w).Encode(result)
} else {
w.Write([]byte("[]"))
}
}
func (gs *GatewayServer) Route(sid string, r *http.Request, req *rpc.RPCRequest) (resp *rpc.RPCResponse) {
func (gs *GatewayServer) Route(ctx context.Context, sid string, r *http.Request, req *rpc.RPCRequest) (resp *rpc.RPCResponse) {
defer utils.CatchPanicWithFallback(func(rec any) {
gs.x.SLog.Error("panic caught in handler", slog.Any("error", rec))
resp = rpc.NewError(rpc.ErrInternalError, "Internal server error (panic)", req.ID)
@@ -110,10 +119,10 @@ func (gs *GatewayServer) Route(sid string, r *http.Request, req *rpc.RPCRequest)
return rpc.NewError(rpc.ErrContextVersion, rpc.ErrContextVersionS, req.ID)
}
resp = server.Handle(sid, r, req)
// checks if request is notification
if req.ID == nil {
go server.Handle(ctx, sid, r, req)
return nil
}
return resp
return server.Handle(ctx, sid, r, req)
}

View File

@@ -1,13 +1,14 @@
package sv1
import (
"context"
"log/slog"
"net/http"
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
)
func (h *HandlerV1) Handle(sid string, r *http.Request, req *rpc.RPCRequest) *rpc.RPCResponse {
func (h *HandlerV1) Handle(_ context.Context, sid string, r *http.Request, req *rpc.RPCRequest) *rpc.RPCResponse {
if req.Method == "" {
h.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrMethodNotFoundS), slog.String("requested-method", req.Method))
return rpc.NewError(rpc.ErrMethodIsMissing, rpc.ErrMethodIsMissingS, req.ID)

View File

@@ -1,5 +1,7 @@
package sv1
// TODO: make a lua state pool using sync.Pool
import (
"fmt"
"io"