mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-06-20 12:21:35 +00:00
feat: use a in memory map to count requests for ips and log the n most active each hour
This commit is contained in:
parent
023ea17492
commit
b22d3665a9
7 changed files with 74 additions and 20 deletions
|
@ -3,6 +3,7 @@ package handler
|
|||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
|
@ -24,14 +25,21 @@ func Handler(
|
|||
cfg config.ServerConfig,
|
||||
giteaClient *gitea.Client,
|
||||
canonicalDomainCache, redirectsCache cache.ICache,
|
||||
mostActiveIpMap *sync.Map,
|
||||
) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
ctx := context.New(w, req)
|
||||
log := log.With().Str("ReqId", ctx.ReqId).Strs("Handler", []string{req.Host, req.RequestURI}).Logger()
|
||||
|
||||
if cfg.LogEveryRequest {
|
||||
log.Log().Str("IP", req.RemoteAddr).Msg("new request")
|
||||
if cfg.LogMostActiveIps {
|
||||
success := false
|
||||
for !success {
|
||||
value, _ := mostActiveIpMap.LoadOrStore(req.RemoteAddr, uint(0))
|
||||
count := value.(uint)
|
||||
success = mostActiveIpMap.CompareAndSwap(req.RemoteAddr, count, count+1)
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("\n----------------------------------------------------------")
|
||||
|
||||
ctx.RespWriter.Header().Set("Server", "pages-server")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue