can cache fishing results in db

This commit is contained in:
Ted Unangst 2019-05-10 18:18:32 -04:00
parent 2f3ee823d9
commit 6be21e9f0b
2 changed files with 58 additions and 41 deletions

View File

@ -845,3 +845,61 @@ func asjonker(user *WhatAbout) map[string]interface{} {
return j
}
var handfull = make(map[string]string)
var handlock sync.Mutex
func gofish(name string) string {
if name[0] == '@' {
name = name[1:]
}
m := strings.Split(name, "@")
if len(m) != 2 {
log.Printf("bad fish name: %s", name)
return ""
}
handlock.Lock()
ref, ok := handfull[name]
handlock.Unlock()
if ok {
return ref
}
db := opendatabase()
row := db.QueryRow("select ibox from xonkers where xid = ?", name)
var href string
err := row.Scan(&href)
if err == nil {
handlock.Lock()
handfull[name] = href
handlock.Unlock()
return href
}
log.Printf("fishing for %s", name)
j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
if err != nil {
log.Printf("failed to go fish %s: %s", name, err)
handlock.Lock()
handfull[name] = ""
handlock.Unlock()
return ""
}
links, _ := jsongetarray(j, "links")
for _, l := range links {
href, _ := jsongetstring(l, "href")
rel, _ := jsongetstring(l, "rel")
t, _ := jsongetstring(l, "type")
if rel == "self" && friendorfoe(t) {
db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
name, href, "", "", "")
handlock.Lock()
handfull[name] = href
handlock.Unlock()
return href
}
}
handlock.Lock()
handfull[name] = ""
handlock.Unlock()
return ""
}

41
honk.go
View File

@ -31,7 +31,6 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/mux"
@ -982,46 +981,6 @@ func showcombos(w http.ResponseWriter, r *http.Request) {
}
}
var handfull = make(map[string]string)
var handlock sync.Mutex
func gofish(name string) string {
if name[0] == '@' {
name = name[1:]
}
m := strings.Split(name, "@")
if len(m) != 2 {
log.Printf("bad fish name: %s", name)
return ""
}
handlock.Lock()
ref, ok := handfull[name]
handlock.Unlock()
if ok {
return ref
}
j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
handlock.Lock()
defer handlock.Unlock()
if err != nil {
log.Printf("failed to go fish %s: %s", name, err)
handfull[name] = ""
return ""
}
links, _ := jsongetarray(j, "links")
for _, l := range links {
href, _ := jsongetstring(l, "href")
rel, _ := jsongetstring(l, "rel")
t, _ := jsongetstring(l, "type")
if rel == "self" && friendorfoe(t) {
handfull[name] = href
return href
}
}
handfull[name] = ""
return ""
}
func savehonker(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r)
name := r.FormValue("name")