From 7e71d0cc7aa8ddca17b257c829b4bde5974454a6 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Mon, 1 Feb 2021 00:28:20 +0000 Subject: [PATCH] Installed recent requests structure to prevent repeat requests Default size is 5 Only targets posts, not users --- Makefile | 4 ++-- fedilogue.go | 5 +---- fifo.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ headers.go | 1 + instance.go | 6 +++++- retrieve.go | 23 ++++++++++++++++------- 6 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 fifo.go diff --git a/Makefile b/Makefile index 9ea23a0..ee2f26c 100644 --- a/Makefile +++ b/Makefile @@ -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 -FEDICTL_GOFILES = fedictl.go headers.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 fifo.go build: go build -o fedilogue $(FEDILOGUE_GOFILES) diff --git a/fedilogue.go b/fedilogue.go index 3eb762d..9599322 100644 --- a/fedilogue.go +++ b/fedilogue.go @@ -43,13 +43,10 @@ func main() { for _, endpoint := range settings.Autostart { logInfo.Print("Autostarting " + endpoint) - ri_mutex.Lock() - _, exists := runninginstances[endpoint] + _, exists := GetRunner(endpoint) if exists == false { - runninginstances[endpoint] = RunningInstance{} go StartInstance(endpoint) } - ri_mutex.Unlock() } go startctl() diff --git a/fifo.go b/fifo.go new file mode 100644 index 0000000..5290ed5 --- /dev/null +++ b/fifo.go @@ -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 +} diff --git a/headers.go b/headers.go index e2e8ac2..376fb44 100644 --- a/headers.go +++ b/headers.go @@ -60,6 +60,7 @@ type RunningInstance struct { client http.Client client_id string client_secret string + recenturis *Fifo } type NodeInfoSoftware struct { diff --git a/instance.go b/instance.go index caaf77b..50ed1bd 100644 --- a/instance.go +++ b/instance.go @@ -58,10 +58,12 @@ func BuildClient(endpoint string) http.Client { func GetRunner(endpoint string) (RunningInstance, bool) { ri_mutex.Lock() o, exists := runninginstances[endpoint] + if exists == false { - o := RunningInstance{} + o = RunningInstance{} o.client = BuildClient(endpoint) o.Status = KEEPALIVE + o.recenturis = newfifo(5) runninginstances[endpoint] = o } ri_mutex.Unlock() @@ -166,11 +168,13 @@ func CheckInstance(newinstance string, callerEndpoint string) { return } + // Going forward, this might be merged into GetRunner ri_mutex.Lock() o, exists := runninginstances[newinstance] if exists == false || o.Status == KEEPALIVE { m := RunningInstance{} m.client = BuildClient(newinstance) + m.recenturis = newfifo(5) runninginstances[newinstance] = m go StartInstance(newinstance) } diff --git a/retrieve.go b/retrieve.go index 485f958..8af5594 100644 --- a/retrieve.go +++ b/retrieve.go @@ -72,25 +72,34 @@ type PostJson struct { func check_post(uri string) (PostJson, error) { var postjson PostJson + + // Ignore banned for _, banned := range settings.Banned { if strings.Index(uri, "https://"+banned+"/") == 0 { 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) err := selectRet.Scan(&postjson.ID, &postjson.InReplyTo, &postjson.Published, &postjson.Summary, &postjson.Content, &postjson.normalized, &postjson.AttributedTo, &postjson.posthash, &postjson.receivedAt) if err == 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.Header.Set("User-Agent", "Tusky") req.Header.Add("Accept", "application/ld+json")