Add throttle to context
This commit is contained in:
parent
ac5837e26a
commit
47ed047f7e
23
main.go
23
main.go
|
|
@ -13,22 +13,26 @@ const burstLimit = 2
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
throttles := make(map[string]<-chan time.Time)
|
throttles := make(map[string]context.Context)
|
||||||
calledLastHour := make([]string, 0)
|
calledLastHour := make([]string, 0)
|
||||||
|
|
||||||
http.HandleFunc("/", serveIndexFile)
|
http.HandleFunc("/", serveIndexFile)
|
||||||
http.HandleFunc("/input.txt", serveInputFile)
|
http.HandleFunc("/input.txt", serveInputFile)
|
||||||
http.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
cctxV, ok := throttles[r.RemoteAddr]
|
||||||
throttle := throttling.CreateThrottle(ctx, burstLimit)
|
if !ok {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
throttle := throttling.CreateThrottle(cctx, burstLimit)
|
||||||
a := throttleWithCancel{
|
a := throttleWithCancel{
|
||||||
throttle: throttle,
|
throttle: throttle,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
con := context.WithValue(ctx, "throttle", a)
|
cctxV = context.WithValue(ctx, "throttle", a)
|
||||||
|
throttles[r.RemoteAddr] = cctxV
|
||||||
|
}
|
||||||
|
|
||||||
tryResult(ctx, w, r, throttles, calledLastHour)
|
tryResult(cctxV, w, r, calledLastHour)
|
||||||
})
|
})
|
||||||
|
|
||||||
err := http.ListenAndServe(":3333", nil)
|
err := http.ListenAndServe(":3333", nil)
|
||||||
|
|
@ -51,19 +55,14 @@ 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, throttles map[string]<-chan time.Time, calledLastHour []string) {
|
func tryResult(ctx context.Context, w http.ResponseWriter, r *http.Request, calledLastHour []string) {
|
||||||
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, clientResult)
|
||||||
if !slices.Contains(calledLastHour, clientIP) {
|
if !slices.Contains(calledLastHour, clientIP) {
|
||||||
calledLastHour = append(calledLastHour, clientIP)
|
calledLastHour = append(calledLastHour, clientIP)
|
||||||
}
|
}
|
||||||
throttle, ok := throttles[clientIP]
|
throttle := ctx.Value("throttle").(throttleWithCancel).throttle
|
||||||
if !ok {
|
|
||||||
fmt.Println("Creating new throttle")
|
|
||||||
throttle = throttling.CreateThrottle(ctx, burstLimit)
|
|
||||||
throttles[clientIP] = throttle
|
|
||||||
}
|
|
||||||
payload := throttling.Payload{
|
payload := throttling.Payload{
|
||||||
R: r,
|
R: r,
|
||||||
W: w,
|
W: w,
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,6 @@ type Payload struct {
|
||||||
// CallFunction allows burst rate limiting client calls with the
|
// CallFunction allows burst rate limiting client calls with the
|
||||||
// payloads.
|
// payloads.
|
||||||
func CallFunction(ctx context.Context, client Client, payload *Payload, throttle <-chan time.Time) {
|
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
|
<-throttle // rate limit our client calls
|
||||||
client.Call(payload)
|
client.Call(payload)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue