Statistik: Impression-Erfassung asynchron (nicht im Request-Pfad)
Der Seitenaufruf wartete bisher synchron auf den DB-Upsert. Mit synchronous=FULL (WAL) macht SQLite pro Commit ein fsync -> spuerbare Latenz pro Seitenaufruf. Die Erfassung laeuft jetzt in einer Goroutine, die Auslieferung blockiert nicht mehr darauf. Der GeoIP-Lookup war nie das Problem (mmap, einmal beim Start geoeffnet). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -92,14 +92,22 @@ func lookupASN(ip net.IP) string {
|
|||||||
// und vermerkt das autonome System. Die IP wird zuerst maskiert; sowohl die
|
// und vermerkt das autonome System. Die IP wird zuerst maskiert; sowohl die
|
||||||
// Speicherung als auch die ASN-Auflösung arbeiten nur auf dem anonymisierten
|
// Speicherung als auch die ASN-Auflösung arbeiten nur auf dem anonymisierten
|
||||||
// Netz. Gezielt nur auf den HTML-Seiten-Routen, nicht auf API oder Assets.
|
// Netz. Gezielt nur auf den HTML-Seiten-Routen, nicht auf API oder Assets.
|
||||||
|
//
|
||||||
|
// Die Erfassung läuft asynchron: der Seitenaufruf darf nicht auf den DB-Schreib
|
||||||
|
// warten (SQLite serialisiert Schreiber, synchronous=FULL macht pro Commit ein
|
||||||
|
// fsync). r ist nach Rückkehr des Handlers nicht mehr sicher nutzbar -> wir
|
||||||
|
// kopieren die zwei Werte vorher heraus.
|
||||||
func trackImpression(next http.Handler) http.Handler {
|
func trackImpression(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
netw := maskIP(clientIP(r.RemoteAddr))
|
remote, path := r.RemoteAddr, r.URL.Path
|
||||||
anon := ""
|
go func() {
|
||||||
if netw != nil {
|
netw := maskIP(clientIP(remote))
|
||||||
anon = netw.String()
|
anon := ""
|
||||||
}
|
if netw != nil {
|
||||||
recordImpression(r.URL.Path, anon, lookupASN(netw))
|
anon = netw.String()
|
||||||
|
}
|
||||||
|
recordImpression(path, anon, lookupASN(netw))
|
||||||
|
}()
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user