avoid allocs for guest alert etags

use phrase file modtime instead of instance start time for routeAPIPhrases to avoid cache churn
This commit is contained in:
Azareal 2020-03-07 12:59:06 +10:00
parent 42550269cc
commit f3bdfffbed
5 changed files with 20 additions and 12 deletions

View File

@ -14,8 +14,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
qgen "github.com/Azareal/Gosora/query_gen"
meta "github.com/Azareal/Gosora/common/meta" meta "github.com/Azareal/Gosora/common/meta"
qgen "github.com/Azareal/Gosora/query_gen"
) )
var SoftwareVersion = Version{Major: 0, Minor: 3, Patch: 0, Tag: "dev"} var SoftwareVersion = Version{Major: 0, Minor: 3, Patch: 0, Tag: "dev"}
@ -35,6 +35,8 @@ const Terabyte int = Gigabyte * 1024
const Petabyte int = Terabyte * 1024 const Petabyte int = Terabyte * 1024
var StartTime time.Time var StartTime time.Time
var GzipStartEtag string
var StartEtag string
var TmplPtrMap = make(map[string]interface{}) var TmplPtrMap = make(map[string]interface{})
// Anti-spam token with rotated key // Anti-spam token with rotated key
@ -176,4 +178,4 @@ func eachall(stmt *sql.Stmt, f func(r *sql.Rows) error) error {
} }
} }
return rows.Err() return rows.Err()
} }

View File

@ -263,7 +263,7 @@ func (list SFileList) Init() error {
return err return err
} }
path = strings.TrimPrefix(path, "public/") path = strings.TrimPrefix(path, "public/")
var ext = filepath.Ext("/public/" + path) ext := filepath.Ext("/public/" + path)
mimetype := mime.TypeByExtension(ext) mimetype := mime.TypeByExtension(ext)
// Get a checksum for CSPs and cache busting // Get a checksum for CSPs and cache busting

View File

@ -18,6 +18,7 @@ import (
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
// TODO: Add a phrase store? // TODO: Add a phrase store?
@ -39,6 +40,7 @@ type LevelPhrases struct {
type LanguagePack struct { type LanguagePack struct {
Name string Name string
IsoCode string IsoCode string
ModTime time.Time
//LastUpdated string //LastUpdated string
// Should we use a sync map or a struct for these? It would be nice, if we could keep all the phrases consistent. // Should we use a sync map or a struct for these? It would be nice, if we could keep all the phrases consistent.
@ -90,6 +92,7 @@ func InitPhrases(lang string) error {
if err != nil { if err != nil {
return err return err
} }
langPack.ModTime = f.ModTime()
langPack.ErrorsBytes = make(map[string][]byte) langPack.ErrorsBytes = make(map[string][]byte)
for name, phrase := range langPack.Errors { for name, phrase := range langPack.Errors {

View File

@ -57,6 +57,9 @@ func afterDBInit() (err error) {
} }
log.Print("Exitted storeInit") log.Print("Exitted storeInit")
c.GzipStartEtag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "-ng\""
c.StartEtag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "-n\""
var uids []int var uids []int
tc := c.Topics.GetCache() tc := c.Topics.GetCache()
if tc != nil { if tc != nil {

View File

@ -75,9 +75,9 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
var etag string var etag string
_, ok := w.(c.GzipResponseWriter) _, ok := w.(c.GzipResponseWriter)
if ok { if ok {
etag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "-ng\"" etag = c.GzipStartEtag
} else { } else {
etag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "-n\"" etag = c.StartEtag
} }
w.Header().Set("ETag", etag) w.Header().Set("ETag", etag)
if match := r.Header.Get("If-None-Match"); match != "" { if match := r.Header.Get("If-None-Match"); match != "" {
@ -219,15 +219,15 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
queryBit = strings.TrimSpace(queryBit) queryBit = strings.TrimSpace(queryBit)
if queryBit[0] == '!' && len(queryBit) > 1 { if queryBit[0] == '!' && len(queryBit) > 1 {
queryBit = strings.TrimPrefix(queryBit, "!") queryBit = strings.TrimPrefix(queryBit, "!")
for _, char := range queryBit { for _, ch := range queryBit {
if !unicode.IsLetter(char) && char != '-' && char != '_' { if !unicode.IsLetter(ch) && ch != '-' && ch != '_' {
return c.PreErrorJS("No symbols allowed, only - and _", w, r) return c.PreErrorJS("No symbols allowed, only - and _", w, r)
} }
} }
negations = append(negations, queryBit) negations = append(negations, queryBit)
} else { } else {
for _, char := range queryBit { for _, ch := range queryBit {
if !unicode.IsLetter(char) && char != '-' && char != '_' { if !unicode.IsLetter(ch) && ch != '-' && ch != '_' {
return c.PreErrorJS("No symbols allowed, only - and _", w, r) return c.PreErrorJS("No symbols allowed, only - and _", w, r)
} }
} }
@ -242,14 +242,14 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
var etag string var etag string
_, ok := w.(c.GzipResponseWriter) _, ok := w.(c.GzipResponseWriter)
if ok { if ok {
etag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "-g\"" etag = "\"" + strconv.FormatInt(phrases.GetCurrentLangPack().ModTime.Unix(), 10) + "-ng\""
} else { } else {
etag = "\"" + strconv.FormatInt(c.StartTime.Unix(), 10) + "\"" etag = "\"" + strconv.FormatInt(phrases.GetCurrentLangPack().ModTime.Unix(), 10) + "-n\""
} }
var plist map[string]string var plist map[string]string
var notModified, private bool var notModified, private bool
var posLoop = func(positive string) c.RouteError { posLoop := func(positive string) c.RouteError {
// ! Constrain it to a subset of phrases for now // ! Constrain it to a subset of phrases for now
for _, item := range phraseWhitelist { for _, item := range phraseWhitelist {
if strings.HasPrefix(positive, item) { if strings.HasPrefix(positive, item) {