perf: serve long-range studies from a decoded closed-day cache
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4143bf599e
commit
4ca9c7bf0c
16 files changed
+846
-83
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -58,6 +60,28 @@ var (
|
||||
// NormPath normalises a path for signatures: query dropped, lowercase, uuids
|
||||
// and long hex runs become {id}, digit runs become {n}.
|
||||
func NormPath(p string) string {
|
||||
if v, ok := normMemo.Load(p); ok {
|
||||
return v.(string)
|
||||
}
|
||||
n := normPath(p)
|
||||
if normMemoN.Add(1) > normMemoMax { // bounded: paths come from attackers
|
||||
normMemo.Clear()
|
||||
normMemoN.Store(1)
|
||||
}
|
||||
normMemo.Store(p, n)
|
||||
return n
|
||||
}
|
||||
|
||||
// Paths repeat heavily in a study and the three regular expressions are the
|
||||
// costliest step of a signature, so results are memoised.
|
||||
const normMemoMax = 50000
|
||||
|
||||
var (
|
||||
normMemo sync.Map
|
||||
normMemoN atomic.Int64
|
||||
)
|
||||
|
||||
func normPath(p string) string {
|
||||
if i := strings.IndexAny(p, "?#"); i >= 0 {
|
||||
p = p[:i]
|
||||
}
|
||||
|
||||
+52
-13
@@ -3,8 +3,10 @@ package intel
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
@@ -158,20 +160,57 @@ func Sessions(events []Ev, gap time.Duration) []Session {
|
||||
for _, e := range events {
|
||||
byIP[e.IP] = append(byIP[e.IP], e)
|
||||
}
|
||||
var out []Session
|
||||
for ip, evs := range byIP {
|
||||
sort.SliceStable(evs, func(i, j int) bool { return evs[i].T.Before(evs[j].T) })
|
||||
priorFail := false
|
||||
start := 0
|
||||
for i := 1; i <= len(evs); i++ {
|
||||
if i < len(evs) && evs[i].T.Sub(evs[i-1].T) <= gap {
|
||||
continue
|
||||
return SessionsByIP(byIP, gap)
|
||||
}
|
||||
|
||||
// SessionsByIP is Sessions for events already grouped by address (the lists
|
||||
// are sorted in place by time), which spares the second copy of a long study.
|
||||
func SessionsByIP(byIP map[string][]Ev, gap time.Duration) []Session {
|
||||
ips := make([]string, 0, len(byIP))
|
||||
for ip := range byIP {
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
return SessionsLazy(ips, func(ip string) []Ev { return byIP[ip] }, gap)
|
||||
}
|
||||
|
||||
// SessionsLazy builds the sessions of the listed addresses, asking load for the
|
||||
// events of one address at a time so a long study never holds them all.
|
||||
func SessionsLazy(ips []string, load func(ip string) []Ev, gap time.Duration) []Session {
|
||||
if gap <= 0 {
|
||||
gap = DefaultGap
|
||||
}
|
||||
// addresses are independent of each other: build their sessions in parallel
|
||||
workers := min(runtime.GOMAXPROCS(0), 8, 1+len(ips)/256)
|
||||
parts := make([][]Session, workers)
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
var out []Session
|
||||
for i := w; i < len(ips); i += workers {
|
||||
ip := ips[i]
|
||||
evs := load(ip)
|
||||
sort.SliceStable(evs, func(i, j int) bool { return evs[i].T.Before(evs[j].T) })
|
||||
priorFail := false
|
||||
start := 0
|
||||
for i := 1; i <= len(evs); i++ {
|
||||
if i < len(evs) && evs[i].T.Sub(evs[i-1].T) <= gap {
|
||||
continue
|
||||
}
|
||||
s, pf := buildSession(ip, evs[start:i], priorFail)
|
||||
priorFail = pf
|
||||
out = append(out, s)
|
||||
start = i
|
||||
}
|
||||
}
|
||||
s, pf := buildSession(ip, evs[start:i], priorFail)
|
||||
priorFail = pf
|
||||
out = append(out, s)
|
||||
start = i
|
||||
}
|
||||
parts[w] = out
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
var out []Session
|
||||
for _, p := range parts {
|
||||
out = append(out, p...)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if !out[i].Start.Equal(out[j].Start) {
|
||||
|
||||
Reference in new issue
Block a user