all repos — honk @ 389115d658ec02a28a8fa5e3ab9b78462d2e93a1

my fork of honk

replace most uses of old cache with new one
Ted Unangst tedu@tedunangst.com
Thu, 21 Dec 2023 12:15:55 -0500
commit

389115d658ec02a28a8fa5e3ab9b78462d2e93a1

parent

444abdec55cb95dc1ea62496d956a8e9b0559dde

6 files changed, 74 insertions(+), 113 deletions(-)

jump to
M activity.goactivity.go

@@ -30,8 +30,8 @@ "regexp"

"strings" "time" - "humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/gate" + "humungus.tedunangst.com/r/webs/gencache" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/junk" "humungus.tedunangst.com/r/webs/templates"

@@ -141,9 +141,8 @@ if final != nil {

*final = url } sign := func(req *http.Request) error { - var ki *KeyInfo - ok := ziggies.Get(userid, &ki) - if ok { + ki := ziggy(userid) + if ki != nil { httpsig.SignRequest(ki.keyname, ki.seckey, req, nil) } return nil

@@ -349,7 +348,7 @@ Out string

Shared string } -var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) { +var boxofboxes = gencache.New(gencache.Options[string, *Box]{Fill: func(ident string) (*Box, bool) { var info string row := stmtGetXonker.QueryRow(ident, "boxes") err := row.Scan(&info)

@@ -1420,7 +1419,7 @@

return j, jo } -var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) { +var oldjonks = gencache.New(gencache.Options[string, []byte]{Fill: func(xid string) ([]byte, bool) { row := stmtAnyXonk.QueryRow(xid) honk := scanhonk(row) if honk == nil || !honk.Public {

@@ -1445,8 +1444,7 @@ return j.ToBytes(), true

}, Limit: 128}) func gimmejonk(xid string) ([]byte, bool) { - var j []byte - ok := oldjonks.Get(xid, &j) + j, ok := oldjonks.Get(xid) return j, ok }

@@ -1460,8 +1458,7 @@ if a[0] == '%' {

rcpts[a] = true continue } - var box *Box - ok := boxofboxes.Get(a, &box) + box, ok := boxofboxes.Get(a) if ok && useshared && box.Shared != "" { rcpts["%"+box.Shared] = true } else {

@@ -1537,8 +1534,7 @@ for _, h := range getdubs(user.ID) {

if h.XID == user.URL { continue } - var box *Box - ok := boxofboxes.Get(h.XID, &box) + box, ok := boxofboxes.Get(h.XID) if ok && box.Shared != "" { rcpts["%"+box.Shared] = true } else {

@@ -1549,8 +1545,7 @@ for _, f := range getbacktracks(honk.XID) {

if f[0] == '%' { rcpts[f] = true } else { - var box *Box - ok := boxofboxes.Get(f, &box) + box, ok := boxofboxes.Get(f) if ok && box.Shared != "" { rcpts["%"+box.Shared] = true } else {

@@ -1583,8 +1578,7 @@ j["object"] = honk.XID

j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:]) rcpts := make(map[string]bool) for _, dub := range dubs { - var box *Box - ok := boxofboxes.Get(dub.XID, &box) + box, ok := boxofboxes.Get(dub.XID) if ok && box.Shared != "" { rcpts["%"+box.Shared] = true } else {

@@ -1649,7 +1643,7 @@

return j } -var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) { +var oldjonkers = gencache.New(gencache.Options[string, []byte]{Fill: func(name string) ([]byte, bool) { user, err := butwhatabout(name) if err != nil { return nil, false

@@ -1659,12 +1653,11 @@ return j.ToBytes(), true

}, Duration: 1 * time.Minute}) func asjonker(name string) ([]byte, bool) { - var j []byte - ok := oldjonkers.Get(name, &j) + j, ok := oldjonkers.Get(name) return j, ok } -var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) { +var handfull = gencache.New(gencache.Options[string, string]{Fill: func(name string) (string, bool) { m := strings.Split(name, "@") if len(m) != 2 { dlog.Printf("bad fish name: %s", name)

@@ -1707,8 +1700,7 @@ func gofish(name string) string {

if name[0] == '@' { name = name[1:] } - var href string - handfull.Get(name, &href) + href, _ := handfull.Get(name) return href }

@@ -1872,8 +1864,7 @@ }

} func updateMe(username string) { - var user *WhatAbout - somenamedusers.Get(username, &user) + user, _ := somenamedusers.Get(username) dt := time.Now().UTC().Format(time.RFC3339) j := junk.New() j["@context"] = itiswhatitis

@@ -1891,9 +1882,8 @@ for _, f := range getdubs(user.ID) {

if f.XID == user.URL { continue } - var box *Box - boxofboxes.Get(f.XID, &box) - if box != nil && box.Shared != "" { + box, ok := boxofboxes.Get(f.XID) + if ok && box.Shared != "" { rcpts["%"+box.Shared] = true } else { rcpts[f.XID] = true
M database.godatabase.go

@@ -29,7 +29,7 @@ "strings"

"sync" "time" - "humungus.tedunangst.com/r/webs/cache" + "humungus.tedunangst.com/r/webs/gencache" "humungus.tedunangst.com/r/webs/htfilter" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/login"

@@ -67,7 +67,7 @@

return user, nil } -var somenamedusers = cache.New(cache.Options{Filler: func(name string) (*WhatAbout, bool) { +var somenamedusers = gencache.New(gencache.Options[string, *WhatAbout]{Fill: func(name string) (*WhatAbout, bool) { row := stmtUserByName.QueryRow(name) user, err := userfromrow(row) if err != nil {

@@ -81,7 +81,7 @@ user.Onts = marker.HashTags

return user, true }}) -var somenumberedusers = cache.New(cache.Options{Filler: func(userid int64) (*WhatAbout, bool) { +var somenumberedusers = gencache.New(gencache.Options[int64, *WhatAbout]{Fill: func(userid int64) (*WhatAbout, bool) { row := stmtUserByNumber.QueryRow(userid) user, err := userfromrow(row) if err != nil {

@@ -93,8 +93,7 @@ return user, true

}}) func getserveruser() *WhatAbout { - var user *WhatAbout - ok := somenumberedusers.Get(serverUID, &user) + user, ok := somenumberedusers.Get(serverUID) if !ok { elog.Panicf("lost server user") }

@@ -102,15 +101,14 @@ return user

} func butwhatabout(name string) (*WhatAbout, error) { - var user *WhatAbout - ok := somenamedusers.Get(name, &user) + user, ok := somenamedusers.Get(name) if !ok { return nil, fmt.Errorf("no user: %s", name) } return user, nil } -var honkerinvalidator cache.Invalidator +var honkerinvalidator gencache.Invalidator[int64] func gethonkers(userid int64) []*Honker { rows, err := stmtHonkers.Query(userid)

@@ -652,8 +650,7 @@ return err

} func chatplusone(tx *sql.Tx, userid int64) { - var user *WhatAbout - ok := somenumberedusers.Get(userid, &user) + user, ok := somenumberedusers.Get(userid) if !ok { return }

@@ -671,8 +668,7 @@ somenumberedusers.Clear(user.ID)

} func chatnewnone(userid int64) { - var user *WhatAbout - ok := somenumberedusers.Get(userid, &user) + user, ok := somenumberedusers.Get(userid) if !ok || user.Options.ChatCount == 0 { return }

@@ -691,8 +687,7 @@ somenumberedusers.Clear(user.ID)

} func meplusone(tx *sql.Tx, userid int64) { - var user *WhatAbout - ok := somenumberedusers.Get(userid, &user) + user, ok := somenumberedusers.Get(userid) if !ok { return }

@@ -710,8 +705,7 @@ somenumberedusers.Clear(user.ID)

} func menewnone(userid int64) { - var user *WhatAbout - ok := somenumberedusers.Get(userid, &user) + user, ok := somenumberedusers.Get(userid) if !ok || user.Options.MeCount == 0 { return }
M deliverator.godeliverator.go

@@ -125,9 +125,8 @@ rcpt := doover.Rcpt

garage.StartKey(rcpt) defer garage.FinishKey(rcpt) - var ki *KeyInfo - ok := ziggies.Get(doover.Userid, &ki) - if !ok { + ki := ziggy(doover.Userid) + if ki == nil { elog.Printf("lost key for delivery") return }

@@ -136,8 +135,7 @@ // already did the box indirection

if rcpt[0] == '%' { inbox = rcpt[1:] } else { - var box *Box - ok := boxofboxes.Get(rcpt, &box) + box, ok := boxofboxes.Get(rcpt) if !ok { ilog.Printf("failed getting inbox for %s", rcpt) sayitagain(doover)
M fun.gofun.go

@@ -29,7 +29,7 @@ "strings"

"time" "golang.org/x/net/html" - "humungus.tedunangst.com/r/webs/cache" + "humungus.tedunangst.com/r/webs/gencache" "humungus.tedunangst.com/r/webs/htfilter" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/mz"

@@ -62,8 +62,7 @@ }

} func reverbolate(userid int64, honks []*Honk) { - var user *WhatAbout - somenumberedusers.Get(userid, &user) + user, _ := somenumberedusers.Get(userid) for _, h := range honks { h.What += "ed" if h.What == "honked" && h.RID != "" {

@@ -163,9 +162,8 @@ }

} } if local && h.What != "bonked" { - var emu Emu - emucache.Get(e, &emu) - if emu.ID != "" { + emu, ok := emucache.Get(e) + if ok { return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID) } }

@@ -266,9 +264,8 @@ }

} } if local { - var emu Emu - emucache.Get(e, &emu) - if emu.ID != "" { + emu, ok := emucache.Get(e) + if ok { return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID) } }

@@ -425,7 +422,7 @@ }

var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`) -var emucache = cache.New(cache.Options{Filler: func(ename string) (Emu, bool) { +var emucache = gencache.New(gencache.Options[string, Emu]{Fill: func(ename string) (Emu, bool) { fname := ename[1 : len(ename)-1] exts := []string{".png", ".gif"} for _, ext := range exts {

@@ -447,9 +444,8 @@ m := re_emus.FindAllString(noise, -1)

m = oneofakind(m) var emus []Emu for _, e := range m { - var emu Emu - emucache.Get(e, &emu) - if emu.ID == "" { + emu, ok := emucache.Get(e) + if !ok { continue } emus = append(emus, emu)

@@ -554,7 +550,7 @@ }

return s } -var shortnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) { +var shortnames = gencache.New(gencache.Options[int64, map[string]string]{Fill: func(userid int64) (map[string]string, bool) { honkers := gethonkers(userid) m := make(map[string]string) for _, h := range honkers {

@@ -564,15 +560,14 @@ return m, true

}, Invalidator: &honkerinvalidator}) func shortname(userid int64, xid string) string { - var m map[string]string - ok := shortnames.Get(userid, &m) + m, ok := shortnames.Get(userid) if ok { return m[xid] } return "" } -var fullnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) { +var fullnames = gencache.New(gencache.Options[int64, map[string]string]{Fill: func(userid int64) (map[string]string, bool) { honkers := gethonkers(userid) m := make(map[string]string) for _, h := range honkers {

@@ -582,8 +577,7 @@ return m, true

}, Invalidator: &honkerinvalidator}) func fullname(name string, userid int64) string { - var m map[string]string - ok := fullnames.Get(userid, &m) + m, ok := fullnames.Get(userid) if ok { return m[name] }

@@ -616,7 +610,7 @@ }

return "" } -var allhandles = cache.New(cache.Options{Filler: func(xid string) (string, bool) { +var allhandles = gencache.New(gencache.Options[string, string]{Fill: func(xid string) (string, bool) { handle := getxonker(xid, "handle") if handle == "" { dlog.Printf("need to get a handle: %s", xid)

@@ -640,8 +634,7 @@ func handles(xid string) (string, string) {

if xid == "" || xid == thewholeworld || strings.HasSuffix(xid, "/followers") { return "", "" } - var handle string - allhandles.Get(xid, &handle) + handle, _ := allhandles.Get(xid) if handle == xid { return xid, xid }

@@ -683,9 +676,8 @@ }

return a[:j] } -var ziggies = cache.New(cache.Options{Filler: func(userid int64) (*KeyInfo, bool) { - var user *WhatAbout - ok := somenumberedusers.Get(userid, &user) +var ziggies = gencache.New(gencache.Options[int64, *KeyInfo]{Fill: func(userid int64) (*KeyInfo, bool) { + user, ok := somenumberedusers.Get(userid) if !ok { return nil, false }

@@ -696,12 +688,11 @@ return ki, true

}}) func ziggy(userid int64) *KeyInfo { - var ki *KeyInfo - ziggies.Get(userid, &ki) + ki, _ := ziggies.Get(userid) return ki } -var zaggies = cache.New(cache.Options{Filler: func(keyname string) (httpsig.PublicKey, bool) { +var zaggies = gencache.New(gencache.Options[string, httpsig.PublicKey]{Fill: func(keyname string) (httpsig.PublicKey, bool) { data := getxonker(keyname, "pubkey") if data == "" { dlog.Printf("hitting the webs for missing pubkey: %s", keyname)

@@ -734,8 +725,7 @@ return key, true

}, Limit: 512}) func zaggy(keyname string) (httpsig.PublicKey, error) { - var key httpsig.PublicKey - zaggies.Get(keyname, &key) + key, _ := zaggies.Get(keyname) return key, nil }
M hfcs.gohfcs.go

@@ -25,6 +25,7 @@ "time"

"unicode" "humungus.tedunangst.com/r/webs/cache" + "humungus.tedunangst.com/r/webs/gencache" "humungus.tedunangst.com/r/webs/login" )

@@ -72,12 +73,15 @@ }

type afiltermap map[filtType][]*Filter -var filtInvalidator cache.Invalidator -var filtcache *cache.Cache +var filtInvalidator gencache.Invalidator[int64] +var filtcache *gencache.Cache[int64, afiltermap] func init() { // resolve init loop - filtcache = cache.New(cache.Options{Filler: filtcachefiller, Invalidator: &filtInvalidator}) + filtcache = gencache.New(gencache.Options[int64, afiltermap]{ + Fill: filtcachefiller, + Invalidator: &filtInvalidator, + }) } func filtcachefiller(userid int64) (afiltermap, bool) {

@@ -185,8 +189,7 @@ filtInvalidator.Clear(userid)

} func getfilters(userid int64, scope filtType) []*Filter { - var filtmap afiltermap - ok := filtcache.Get(userid, &filtmap) + filtmap, ok := filtcache.Get(userid) if ok { return filtmap[scope] }

@@ -197,7 +200,7 @@ type arejectmap map[string][]*Filter

var rejectAnyKey = "..." -var rejectcache = cache.New(cache.Options{Filler: func(userid int64) (arejectmap, bool) { +var rejectcache = gencache.New(gencache.Options[int64, arejectmap]{Fill: func(userid int64) (arejectmap, bool) { m := make(arejectmap) filts := getfilters(userid, filtReject) for _, f := range filts {

@@ -219,8 +222,7 @@ return m, true

}, Invalidator: &filtInvalidator}) func rejectfilters(userid int64, name string) []*Filter { - var m arejectmap - rejectcache.Get(userid, &m) + m, _ := rejectcache.Get(userid) return m[name] }

@@ -280,7 +282,7 @@ }

return false } -var knownknowns = cache.New(cache.Options{Filler: func(userid int64) (map[string]bool, bool) { +var knownknowns = gencache.New(gencache.Options[int64, map[string]bool]{Fill: func(userid int64) (map[string]bool, bool) { m := make(map[string]bool) honkers := gethonkers(userid) for _, h := range honkers {

@@ -290,8 +292,7 @@ return m, true

}, Invalidator: &honkerinvalidator}) func unknownActor(userid int64, actor string) bool { - var knowns map[string]bool - knownknowns.Get(userid, &knowns) + knowns, _ := knownknowns.Get(userid) return !knowns[actor] }

@@ -387,8 +388,7 @@ return ""

} func rejectxonk(xonk *Honk) bool { - var m arejectmap - rejectcache.Get(xonk.UserID, &m) + m, _ := rejectcache.Get(xonk.UserID) filts := m[rejectAnyKey] filts = append(filts, m[xonk.Honker]...) filts = append(filts, m[originate(xonk.Honker)]...)
M web.goweb.go

@@ -42,7 +42,6 @@ "unicode/utf8"

"github.com/gorilla/mux" "humungus.tedunangst.com/r/gonix" - "humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/gencache" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/junk"

@@ -100,8 +99,7 @@ }

if u := login.GetUserInfo(r); u != nil { templinfo["UserInfo"], _ = butwhatabout(u.Username) templinfo["UserStyle"] = getuserstyle(u) - var combos []string - combocache.Get(u.UserID, &combos) + combos, _ := combocache.Get(u.UserID) templinfo["Combos"] = combos } return templinfo

@@ -338,8 +336,7 @@ if who == "" {

ilog.Printf("nobody to ping!") return } - var box *Box - ok := boxofboxes.Get(who, &box) + box, ok := boxofboxes.Get(who) if !ok { ilog.Printf("no inbox to ping %s", who) return

@@ -364,8 +361,7 @@ ilog.Printf("sent ping to %s: %s", who, j["id"])

} func pong(user *WhatAbout, who string, obj string) { - var box *Box - ok := boxofboxes.Get(who, &box) + box, ok := boxofboxes.Get(who) if !ok { ilog.Printf("no inbox to pong %s", who) return

@@ -688,7 +684,7 @@ elog.Print(err)

} } -var oldoutbox = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) { +var oldoutbox = gencache.New(gencache.Options[string, []byte]{Fill: func(name string) ([]byte, bool) { user, err := butwhatabout(name) if err != nil { return nil, false

@@ -726,8 +722,7 @@ if stealthmode(user.ID, r) {

http.NotFound(w, r) return } - var j []byte - ok := oldoutbox.Get(name, &j) + j, ok := oldoutbox.Get(name) if ok { w.Header().Set("Content-Type", theonetruename) w.Write(j)

@@ -736,7 +731,7 @@ http.NotFound(w, r)

} } -var oldempties = cache.New(cache.Options{Filler: func(url string) ([]byte, bool) { +var oldempties = gencache.New(gencache.Options[string, []byte]{Fill: func(url string) ([]byte, bool) { colname := "/followers" if strings.HasSuffix(url, "/following") { colname = "/following"

@@ -764,8 +759,7 @@ if stealthmode(user.ID, r) {

http.NotFound(w, r) return } - var j []byte - ok := oldempties.Get(r.URL.Path, &j) + j, ok := oldempties.Get(r.URL.Path) if ok { w.Header().Set("Content-Type", theonetruename) w.Write(j)

@@ -1195,8 +1189,7 @@ title, user.URL, imgurl, short)

} func honkology(honk *Honk) template.HTML { - var user *WhatAbout - ok := somenumberedusers.Get(honk.UserID, &user) + user, ok := somenumberedusers.Get(honk.UserID) if !ok { return "" }

@@ -2115,7 +2108,7 @@

http.Redirect(w, r, "/chatter", http.StatusSeeOther) } -var combocache = cache.New(cache.Options{Filler: func(userid int64) ([]string, bool) { +var combocache = gencache.New(gencache.Options[int64, []string]{Fill: func(userid int64) ([]string, bool) { honkers := gethonkers(userid) var combos []string for _, h := range honkers {

@@ -2132,9 +2125,6 @@ return combos, true

}, Invalidator: &honkerinvalidator}) func showcombos(w http.ResponseWriter, r *http.Request) { - userinfo := login.GetUserInfo(r) - var combos []string - combocache.Get(userinfo.UserID, &combos) templinfo := getInfo(r) err := readviews.Execute(w, "combos.html", templinfo) if err != nil {

@@ -2263,7 +2253,7 @@ }

http.Redirect(w, r, "/account", http.StatusSeeOther) } -var oldfingers = cache.New(cache.Options{Filler: func(orig string) ([]byte, bool) { +var oldfingers = gencache.New(gencache.Options[string, []byte]{Fill: func(orig string) ([]byte, bool) { if strings.HasPrefix(orig, "acct:") { orig = orig[5:] }

@@ -2306,8 +2296,7 @@ orig := r.FormValue("resource")

dlog.Printf("finger lick: %s", orig) - var j []byte - ok := oldfingers.Get(orig, &j) + j, ok := oldfingers.Get(orig) if ok { w.Header().Set("Content-Type", "application/jrd+json") w.Write(j)