From 82790e260d70e452a249edfecb402afce0cf3d4d Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Mon, 11 Nov 2024 21:52:33 +0100 Subject: [PATCH] Add log --- main.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 205d633..e2b0d69 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/kordondev/meeting/throttling" "net/http" - "slices" "time" ) @@ -13,8 +12,7 @@ const burstLimit = 2 func main() { ctx := context.Background() - throttles := make(map[string]context.Context) - calledLastHour := make([]string, 0) + throttles := make(map[string]context.Context) // use sync map http.HandleFunc("/", serveIndexFile) http.HandleFunc("/input.txt", serveInputFile) @@ -30,9 +28,14 @@ func main() { } cctxV = context.WithValue(ctx, "throttle", a) throttles[r.RemoteAddr] = cctxV + go func() { + time.Sleep(time.Minute * 20) + delete(throttles, r.RemoteAddr) + cancel() + }() } - tryResult(cctxV, w, r, calledLastHour) + tryResult(cctxV, w, r) }) err := http.ListenAndServe(":3333", nil) @@ -55,13 +58,10 @@ func serveInputFile(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static/input.txt") } -func tryResult(ctx context.Context, w http.ResponseWriter, r *http.Request, calledLastHour []string) { +func tryResult(ctx context.Context, w http.ResponseWriter, r *http.Request) { clientIP := r.RemoteAddr clientResult := r.URL.Query().Get("result") - fmt.Println(clientIP, clientResult) - if !slices.Contains(calledLastHour, clientIP) { - calledLastHour = append(calledLastHour, clientIP) - } + fmt.Println(clientIP, "called with:", clientResult) throttle := ctx.Value("throttle").(throttleWithCancel).throttle payload := throttling.Payload{ R: r,