wttr.in/cmd/peakHandling.go

75 lines
1.6 KiB
Go
Raw Normal View History

2020-05-30 16:14:17 +00:00
package main
import (
"log"
"net/http"
"sync"
"time"
"github.com/robfig/cron"
)
2022-11-27 14:51:41 +00:00
func (rp *RequestProcessor) startPeakHandling() {
2020-05-30 16:14:17 +00:00
c := cron.New()
// cronTime := fmt.Sprintf("%d,%d * * * *", 30-prefetchInterval/60, 60-prefetchInterval/60)
2022-11-27 14:51:41 +00:00
c.AddFunc(
"24 * * * *",
func() { rp.prefetchPeakRequests(&rp.peakRequest30) },
)
c.AddFunc(
"54 * * * *",
func() { rp.prefetchPeakRequests(&rp.peakRequest60) },
)
2020-05-30 16:14:17 +00:00
c.Start()
}
2022-11-27 14:51:41 +00:00
func (rp *RequestProcessor) savePeakRequest(cacheDigest string, r *http.Request) {
2020-05-30 16:14:17 +00:00
_, min, _ := time.Now().Clock()
if min == 30 {
2022-11-27 14:51:41 +00:00
rp.peakRequest30.Store(cacheDigest, *r)
2020-05-30 16:14:17 +00:00
} else if min == 0 {
2022-11-27 14:51:41 +00:00
rp.peakRequest60.Store(cacheDigest, *r)
2020-05-30 16:14:17 +00:00
}
}
2022-11-27 14:51:41 +00:00
func (rp *RequestProcessor) prefetchRequest(r *http.Request) {
rp.ProcessRequest(r)
2020-05-30 16:14:17 +00:00
}
func syncMapLen(sm *sync.Map) int {
count := 0
f := func(key, value interface{}) bool {
// Not really certain about this part, don't know for sure
// if this is a good check for an entry's existence
if key == "" {
return false
}
count++
return true
}
sm.Range(f)
return count
}
2022-11-27 14:51:41 +00:00
func (rp *RequestProcessor) prefetchPeakRequests(peakRequestMap *sync.Map) {
2020-05-30 16:14:17 +00:00
peakRequestLen := syncMapLen(peakRequestMap)
if peakRequestLen == 0 {
return
}
2022-11-20 16:54:42 +00:00
log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen)
2022-12-02 19:10:32 +00:00
sleepBetweenRequests := time.Duration(rp.config.Uplink.PrefetchInterval*1000/peakRequestLen) * time.Millisecond
2020-05-30 16:14:17 +00:00
peakRequestMap.Range(func(key interface{}, value interface{}) bool {
2020-06-01 12:18:03 +00:00
go func(r http.Request) {
2022-11-27 14:51:41 +00:00
rp.prefetchRequest(&r)
2020-06-01 12:18:03 +00:00
}(value.(http.Request))
2020-05-30 16:14:17 +00:00
peakRequestMap.Delete(key)
time.Sleep(sleepBetweenRequests)
return true
})
}