Installed recent requests structure to prevent repeat requests
Default size is 5 Only targets posts, not users
This commit is contained in:
parent
88d058528f
commit
7e71d0cc7a
4
Makefile
4
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)
|
||||
|
@ -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()
|
||||
|
45
fifo.go
Normal file
45
fifo.go
Normal 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
|
||||
}
|
@ -60,6 +60,7 @@ type RunningInstance struct {
|
||||
client http.Client
|
||||
client_id string
|
||||
client_secret string
|
||||
recenturis *Fifo
|
||||
}
|
||||
|
||||
type NodeInfoSoftware struct {
|
||||
|
@ -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)
|
||||
}
|
||||
|
23
retrieve.go
23
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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user