Add log
This commit is contained in:
parent
47ed047f7e
commit
82790e260d
18
main.go
18
main.go
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/kordondev/meeting/throttling"
|
"github.com/kordondev/meeting/throttling"
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,8 +12,7 @@ const burstLimit = 2
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
throttles := make(map[string]context.Context)
|
throttles := make(map[string]context.Context) // use sync map
|
||||||
calledLastHour := make([]string, 0)
|
|
||||||
|
|
||||||
http.HandleFunc("/", serveIndexFile)
|
http.HandleFunc("/", serveIndexFile)
|
||||||
http.HandleFunc("/input.txt", serveInputFile)
|
http.HandleFunc("/input.txt", serveInputFile)
|
||||||
|
|
@ -30,9 +28,14 @@ func main() {
|
||||||
}
|
}
|
||||||
cctxV = context.WithValue(ctx, "throttle", a)
|
cctxV = context.WithValue(ctx, "throttle", a)
|
||||||
throttles[r.RemoteAddr] = cctxV
|
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)
|
err := http.ListenAndServe(":3333", nil)
|
||||||
|
|
@ -55,13 +58,10 @@ func serveInputFile(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "static/input.txt")
|
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
|
clientIP := r.RemoteAddr
|
||||||
clientResult := r.URL.Query().Get("result")
|
clientResult := r.URL.Query().Get("result")
|
||||||
fmt.Println(clientIP, clientResult)
|
fmt.Println(clientIP, "called with:", clientResult)
|
||||||
if !slices.Contains(calledLastHour, clientIP) {
|
|
||||||
calledLastHour = append(calledLastHour, clientIP)
|
|
||||||
}
|
|
||||||
throttle := ctx.Value("throttle").(throttleWithCancel).throttle
|
throttle := ctx.Value("throttle").(throttleWithCancel).throttle
|
||||||
payload := throttling.Payload{
|
payload := throttling.Payload{
|
||||||
R: r,
|
R: r,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue