Installed recent requests structure to prevent repeat requests

Default size is 5
Only targets posts, not users
This commit is contained in:
farhan 2021-02-01 00:28:20 +00:00
parent 88d058528f
commit 7e71d0cc7a
6 changed files with 70 additions and 14 deletions

View File

@ -1,5 +1,5 @@
FEDILOGUE_GOFILES = fedilogue.go ctl.go headers.go instance.go poll.go stream.go web.go db.go config.go oauth.go retrieve.go log.go FEDILOGUE_GOFILES = fedilogue.go ctl.go headers.go instance.go poll.go stream.go web.go db.go config.go oauth.go retrieve.go log.go fifo.go
FEDICTL_GOFILES = fedictl.go headers.go log.go FEDICTL_GOFILES = fedictl.go headers.go log.go fifo.go
build: build:
go build -o fedilogue $(FEDILOGUE_GOFILES) go build -o fedilogue $(FEDILOGUE_GOFILES)

View File

@ -43,13 +43,10 @@ func main() {
for _, endpoint := range settings.Autostart { for _, endpoint := range settings.Autostart {
logInfo.Print("Autostarting " + endpoint) logInfo.Print("Autostarting " + endpoint)
ri_mutex.Lock() _, exists := GetRunner(endpoint)
_, exists := runninginstances[endpoint]
if exists == false { if exists == false {
runninginstances[endpoint] = RunningInstance{}
go StartInstance(endpoint) go StartInstance(endpoint)
} }
ri_mutex.Unlock()
} }
go startctl() go startctl()

45
fifo.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"sync"
)
type Fifo struct {
slice []string
mu sync.Mutex
size int
}
func newfifo(size int) *Fifo {
q := Fifo{}
q.slice = make([]string, 0)
q.size = size
return &q
}
func (q *Fifo) Add(v string) int {
q.mu.Lock()
i := q.Contains(v)
if i == -1 {
if len(q.slice) >= q.size {
q.DeleteFirst()
}
q.slice = append(q.slice, v)
}
q.mu.Unlock()
return i
}
func (q *Fifo) Contains(v string) int {
for i, val := range q.slice {
if val == v {
return i
}
}
return -1
}
func (q *Fifo) DeleteFirst() string {
a := q.slice[0]
q.slice = q.slice[1:]
return a
}

View File

@ -60,6 +60,7 @@ type RunningInstance struct {
client http.Client client http.Client
client_id string client_id string
client_secret string client_secret string
recenturis *Fifo
} }
type NodeInfoSoftware struct { type NodeInfoSoftware struct {

View File

@ -58,10 +58,12 @@ func BuildClient(endpoint string) http.Client {
func GetRunner(endpoint string) (RunningInstance, bool) { func GetRunner(endpoint string) (RunningInstance, bool) {
ri_mutex.Lock() ri_mutex.Lock()
o, exists := runninginstances[endpoint] o, exists := runninginstances[endpoint]
if exists == false { if exists == false {
o := RunningInstance{} o = RunningInstance{}
o.client = BuildClient(endpoint) o.client = BuildClient(endpoint)
o.Status = KEEPALIVE o.Status = KEEPALIVE
o.recenturis = newfifo(5)
runninginstances[endpoint] = o runninginstances[endpoint] = o
} }
ri_mutex.Unlock() ri_mutex.Unlock()
@ -166,11 +168,13 @@ func CheckInstance(newinstance string, callerEndpoint string) {
return return
} }
// Going forward, this might be merged into GetRunner
ri_mutex.Lock() ri_mutex.Lock()
o, exists := runninginstances[newinstance] o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE { if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{} m := RunningInstance{}
m.client = BuildClient(newinstance) m.client = BuildClient(newinstance)
m.recenturis = newfifo(5)
runninginstances[newinstance] = m runninginstances[newinstance] = m
go StartInstance(newinstance) go StartInstance(newinstance)
} }

View File

@ -72,25 +72,34 @@ type PostJson struct {
func check_post(uri string) (PostJson, error) { func check_post(uri string) (PostJson, error) {
var postjson PostJson var postjson PostJson
// Ignore banned
for _, banned := range settings.Banned { for _, banned := range settings.Banned {
if strings.Index(uri, "https://"+banned+"/") == 0 { if strings.Index(uri, "https://"+banned+"/") == 0 {
return postjson, errors.New("Banned instance") return postjson, errors.New("Banned instance")
} }
} }
// Ignore invalid URIs
endslash := strings.Index(uri[8:], "/")
if endslash == -1 {
return postjson, errors.New("Invalid URI " + uri)
}
postjson.instance = uri[8 : endslash+8]
o, _ := GetRunner(postjson.instance)
// Check if there were any recent requests on this
if o.recenturis.Add(uri) != -1 {
return postjson, errors.New("Recently requested within local cache")
}
selectRet := pool.QueryRow(context.Background(), "SELECT id, inReplyTo, published, summary, content, normalized, attributedto, posthash, received_at FROM posts WHERE id = $1", uri) selectRet := pool.QueryRow(context.Background(), "SELECT id, inReplyTo, published, summary, content, normalized, attributedto, posthash, received_at FROM posts WHERE id = $1", uri)
err := selectRet.Scan(&postjson.ID, &postjson.InReplyTo, &postjson.Published, &postjson.Summary, &postjson.Content, &postjson.normalized, &postjson.AttributedTo, &postjson.posthash, &postjson.receivedAt) err := selectRet.Scan(&postjson.ID, &postjson.InReplyTo, &postjson.Published, &postjson.Summary, &postjson.Content, &postjson.normalized, &postjson.AttributedTo, &postjson.posthash, &postjson.receivedAt)
if err == nil { if err == nil {
return postjson, nil return postjson, nil
} }
endslash := strings.Index(uri[8:], "/")
if endslash == -1 {
return postjson, errors.New("Invalid URI " + uri)
}
postjson.instance = uri[8 : endslash+8]
o, _ := GetRunner(postjson.instance)
req, _ := http.NewRequest("GET", uri, nil) req, _ := http.NewRequest("GET", uri, nil)
req.Header.Set("User-Agent", "Tusky") req.Header.Set("User-Agent", "Tusky")
req.Header.Add("Accept", "application/ld+json") req.Header.Add("Accept", "application/ld+json")