diff --git a/activity.go b/activity.go index 486f44e..1cdfeaa 100644 --- a/activity.go +++ b/activity.go @@ -29,10 +29,10 @@ import ( "os" "regexp" "strings" - "sync" "time" "humungus.tedunangst.com/r/webs/cache" + "humungus.tedunangst.com/r/webs/gate" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/junk" @@ -117,48 +117,29 @@ func GetJunkHardMode(url string) (junk.Junk, error) { return j, err } -var flightdeck = make(map[string][]chan JunkError) -var decklock sync.Mutex +var flightdeck = gate.NewSerializer() func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) { - decklock.Lock() - inflight, ok := flightdeck[url] - if ok { - log.Printf("awaiting result for %s", url) - c := make(chan JunkError) - flightdeck[url] = append(inflight, c) - decklock.Unlock() - je := <-c - close(c) - return je.Junk, je.Err - } - flightdeck[url] = inflight - decklock.Unlock() - at := thefakename - if strings.Contains(url, ".well-known/webfinger?resource") { - at = "application/jrd+json" + fn := func() (interface{}, error) { + at := thefakename + if strings.Contains(url, ".well-known/webfinger?resource") { + at = "application/jrd+json" + } + j, err := junk.Get(url, junk.GetArgs{ + Accept: at, + Agent: "honksnonk/5.0; " + serverName, + Timeout: timeout, + }) + return j, err } - j, err := junk.Get(url, junk.GetArgs{ - Accept: at, - Agent: "honksnonk/5.0; " + serverName, - Timeout: timeout, - }) - decklock.Lock() - inflight = flightdeck[url] - delete(flightdeck, url) - decklock.Unlock() - if len(inflight) > 0 { - je := JunkError{Junk: j, Err: err} - go func() { - for _, c := range inflight { - log.Printf("returning awaited result for %s", url) - c <- je - } - }() + ji, err := flightdeck.Call(url, fn) + if err != nil { + return nil, err } - return j, err + j := ji.(junk.Junk) + return j, nil } func savedonk(url string, name, desc, media string, localize bool) *Donk { diff --git a/deliverator.go b/deliverator.go index d9efb7e..17a51a0 100644 --- a/deliverator.go +++ b/deliverator.go @@ -18,8 +18,9 @@ package main import ( "log" notrand "math/rand" - "sync" "time" + + "humungus.tedunangst.com/r/webs/gate" ) func init() { @@ -60,30 +61,11 @@ func sayitagain(goarounds int64, userid int64, rcpt string, msg []byte) { } } -var trucksout = 0 -var maxtrucksout = 20 -var garagelock sync.Mutex -var garagebell = sync.NewCond(&garagelock) - -func truckgoesout() { - garagelock.Lock() - for trucksout >= maxtrucksout { - garagebell.Wait() - } - trucksout++ - garagelock.Unlock() -} - -func truckcomesin() { - garagelock.Lock() - trucksout-- - garagebell.Broadcast() - garagelock.Unlock() -} +var garage = gate.NewLimiter(20) func deliverate(goarounds int64, userid int64, rcpt string, msg []byte) { - truckgoesout() - defer truckcomesin() + garage.Start() + defer garage.Finish() var ki *KeyInfo ok := ziggies.Get(userid, &ki) diff --git a/go.mod b/go.mod index 481f4f0..ff88420 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 golang.org/x/net v0.0.0-20190620200207-3b0461eec859 humungus.tedunangst.com/r/go-sqlite3 v1.1.3 - humungus.tedunangst.com/r/webs v0.6.20 + humungus.tedunangst.com/r/webs v0.6.21 ) go 1.11 diff --git a/go.sum b/go.sum index 5472890..333974f 100644 --- a/go.sum +++ b/go.sum @@ -34,7 +34,5 @@ gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdr gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw= humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI= humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= -humungus.tedunangst.com/r/webs v0.6.19 h1:0cxR4JmHDMQmMwboRlycRO+67OPtgtCwLX8fuDu9IRo= -humungus.tedunangst.com/r/webs v0.6.19/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= -humungus.tedunangst.com/r/webs v0.6.20 h1:fKTjJ+EQUlxj8vWBtFMhX7aYbMwAnUWbDhY7hotkXq0= -humungus.tedunangst.com/r/webs v0.6.20/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= +humungus.tedunangst.com/r/webs v0.6.21 h1:bxI4ZFpxSZw06AbQsaTii5011wjxceF2MOfqJrFnM/w= +humungus.tedunangst.com/r/webs v0.6.21/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps=