can cache fishing results in db
Ted Unangst tedu@tedunangst.com
Fri, 10 May 2019 18:18:32 -0400
2 files changed,
58 insertions(+),
41 deletions(-)
M
activity.go
→
activity.go
@@ -845,3 +845,61 @@ j["publicKey"] = k
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 "" +} +
M
honk.go
→
honk.go
@@ -31,7 +31,6 @@ "os"
"sort" "strconv" "strings" - "sync" "time" "github.com/gorilla/mux"@@ -980,46 +979,6 @@ err := readviews.Execute(w, "combos.html", templinfo)
if err != nil { log.Print(err) } -} - -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) {