can cache fishing results in db

master
Ted Unangst 5 years ago
parent 2f3ee823d9
commit 6be21e9f0b

@ -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 ""
}

@ -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")

Loading…
Cancel
Save