mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-06-20 07:32:40 +02: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")
|
||||
|
|
|
@ -3,6 +3,7 @@ package handler
|
|||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -29,7 +30,7 @@ func TestHandlerPerformance(t *testing.T) {
|
|||
AllowedCorsDomains: []string{"raw.codeberg.org", "fonts.codeberg.org", "design.codeberg.org"},
|
||||
PagesBranches: []string{"pages"},
|
||||
}
|
||||
testHandler := Handler(serverCfg, giteaClient, cache.NewInMemoryCache(), cache.NewInMemoryCache())
|
||||
testHandler := Handler(serverCfg, giteaClient, cache.NewInMemoryCache(), cache.NewInMemoryCache(), &sync.Map{})
|
||||
|
||||
testCase := func(uri string, status int) {
|
||||
t.Run(uri, func(t *testing.T) {
|
||||
|
|
|
@ -7,7 +7,9 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pires/go-proxyproto"
|
||||
|
@ -135,8 +137,41 @@ func Serve(ctx *cli.Context) error {
|
|||
StartProfilingServer(ctx.String("profiling-address"))
|
||||
}
|
||||
|
||||
mostActiveIpMap := &sync.Map{}
|
||||
if cfg.Server.LogMostActiveIps {
|
||||
go func() {
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
for range ticker.C {
|
||||
type kv struct {
|
||||
Key string
|
||||
Value uint
|
||||
}
|
||||
|
||||
var kvArray []kv
|
||||
mostActiveIpMap.Range(func(k, v any) bool {
|
||||
kvArray = append(kvArray, kv{k.(string), v.(uint)})
|
||||
return true
|
||||
})
|
||||
mostActiveIpMap.Clear()
|
||||
|
||||
sort.Slice(kvArray, func(i, j int) bool {
|
||||
return kvArray[i].Value > kvArray[j].Value
|
||||
})
|
||||
|
||||
builder := strings.Builder{}
|
||||
var item kv
|
||||
for i := uint(0); i < cfg.Server.MostActiveIpCount; i++ {
|
||||
item = kvArray[i]
|
||||
builder.WriteString(fmt.Sprintf("\n%s, %d", item.Key, item.Value))
|
||||
}
|
||||
|
||||
log.Log().Msg(fmt.Sprintf("%d most active IPs:%s", cfg.Server.MostActiveIpCount, builder.String()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Create ssl handler based on settings
|
||||
sslHandler := handler.Handler(cfg.Server, giteaClient, canonicalDomainCache, redirectsCache)
|
||||
sslHandler := handler.Handler(cfg.Server, giteaClient, canonicalDomainCache, redirectsCache, mostActiveIpMap)
|
||||
|
||||
// Start the ssl listener
|
||||
log.Info().Msgf("Start SSL server using TCP listener on %s", listener.Addr())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue