can use the cache functions for handles now

This commit is contained in:
Ted Unangst 2019-10-29 14:18:13 -04:00
parent 0731de07fa
commit 3b70052f3d
2 changed files with 22 additions and 40 deletions

View File

@ -262,8 +262,8 @@ func eradicatexonk(userid int64, xid string) {
func savexonk(x *Honk) { func savexonk(x *Honk) {
log.Printf("saving xonk: %s", x.XID) log.Printf("saving xonk: %s", x.XID)
go prehandle(x.Honker) go handles(x.Honker)
go prehandle(x.Oonker) go handles(x.Oonker)
savehonk(x) savehonk(x)
} }

50
fun.go
View File

@ -487,36 +487,13 @@ func originate(u string) string {
return "" return ""
} }
var allhandles = make(map[string]string) var allhandles = cache.New(cache.Options{Filler: func(xid string) (string, bool) {
var handlelock sync.Mutex
// handle, handle@host
func handles(xid string) (string, string) {
if xid == "" {
return "", ""
}
handlelock.Lock()
handle := allhandles[xid]
handlelock.Unlock()
if handle == "" {
handle = findhandle(xid)
handlelock.Lock()
allhandles[xid] = handle
handlelock.Unlock()
}
if handle == xid {
return xid, xid
}
return handle, handle + "@" + originate(xid)
}
func findhandle(xid string) string {
row := stmtGetXonker.QueryRow(xid, "handle") row := stmtGetXonker.QueryRow(xid, "handle")
var handle string var handle string
err := row.Scan(&handle) err := row.Scan(&handle)
if err != nil { if err != nil {
info, _ := investigate(xid) info, err := investigate(xid)
if info == nil { if err != nil {
m := re_unurl.FindStringSubmatch(xid) m := re_unurl.FindStringSubmatch(xid)
if len(m) > 2 { if len(m) > 2 {
handle = m[2] handle = m[2]
@ -525,21 +502,26 @@ func findhandle(xid string) string {
} }
} else { } else {
handle = info.Name handle = info.Name
}
_, err = stmtSaveXonker.Exec(xid, handle, "handle") _, err = stmtSaveXonker.Exec(xid, handle, "handle")
if err != nil { if err != nil {
log.Printf("error saving handle: %s", err) log.Printf("error saving handle: %s", err)
} }
} }
return handle
} }
return handle, true
}})
var handleprelock sync.Mutex // handle, handle@host
func handles(xid string) (string, string) {
func prehandle(xid string) { if xid == "" {
handleprelock.Lock() return "", ""
defer handleprelock.Unlock() }
handles(xid) var handle string
allhandles.Get(xid, &handle)
if handle == xid {
return xid, xid
}
return handle, handle + "@" + originate(xid)
} }
func prepend(s string, x []string) []string { func prepend(s string, x []string) []string {