Compare commits

..

3 Commits

Author SHA1 Message Date
Arne Maier 82790e260d Add log 2024-11-11 21:52:33 +01:00
Arne Maier 47ed047f7e Add throttle to context 2024-11-11 21:43:12 +01:00
Arne Maier ac5837e26a Correct context 2024-11-11 21:34:30 +01:00
2 changed files with 37 additions and 19 deletions

46
main.go
View File

@ -5,20 +5,37 @@ import (
"fmt"
"github.com/kordondev/meeting/throttling"
"net/http"
"slices"
"time"
)
const burstLimit = 2
func main() {
throttles := make(map[string]<-chan time.Time)
calledLastHour := make([]string, 0)
ctx := context.Background()
throttles := make(map[string]context.Context) // use sync map
http.HandleFunc("/", serveIndexFile)
http.HandleFunc("/input.txt", serveInputFile)
http.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) {
tryResult(w, r, throttles, calledLastHour)
cctxV, ok := throttles[r.RemoteAddr]
if !ok {
cctx, cancel := context.WithCancel(ctx)
throttle := throttling.CreateThrottle(cctx, burstLimit)
a := throttleWithCancel{
throttle: throttle,
cancel: cancel,
}
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)
})
err := http.ListenAndServe(":3333", nil)
@ -27,6 +44,11 @@ func main() {
}
}
type throttleWithCancel struct {
throttle <-chan time.Time
cancel context.CancelFunc
}
func serveIndexFile(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index-with-text.html")
}
@ -36,26 +58,18 @@ func serveInputFile(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/input.txt")
}
func tryResult(w http.ResponseWriter, r *http.Request, throttles map[string]<-chan time.Time, 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)
}
throttle, ok := throttles[clientIP]
if !ok {
fmt.Println("Creating new throttle")
throttle = throttling.CreateThrottle(r.Context(), burstLimit)
throttles[clientIP] = throttle
}
fmt.Println(clientIP, "called with:", clientResult)
throttle := ctx.Value("throttle").(throttleWithCancel).throttle
payload := throttling.Payload{
R: r,
W: w,
ClientResult: clientResult,
}
throttling.CallFunction(context.TODO(), &CheckResult{}, &payload, throttle)
throttling.CallFunction(ctx, &CheckResult{}, &payload, throttle)
}
type CheckResult struct {

View File

@ -24,9 +24,6 @@ type Payload struct {
// CallFunction allows burst rate limiting client calls with the
// payloads.
func CallFunction(ctx context.Context, client Client, payload *Payload, throttle <-chan time.Time) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
<-throttle // rate limit our client calls
client.Call(payload)
}
@ -43,11 +40,18 @@ func CreateThrottle(ctx context.Context, burstLimit int) <-chan time.Time {
for t := range ticker.C {
select {
case throttle <- t:
{
fmt.Println("Add bucket to throttle")
}
case <-ctx.Done():
{
fmt.Println("Ticker done")
return // exit goroutine when surrounding function returns
}
default:
{
fmt.Println("Dropping bucket")
}
}
}
}()