new day, new libs. use gate library.
Ted Unangst tedu@tedunangst.com
Tue, 29 Oct 2019 13:38:51 -0400
4 files changed,
26 insertions(+),
65 deletions(-)
M
activity.go
→
activity.go
@@ -29,10 +29,10 @@ "net/url"
"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 @@ }
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 {
M
deliverator.go
→
deliverator.go
@@ -18,8 +18,9 @@
import ( "log" notrand "math/rand" - "sync" "time" + + "humungus.tedunangst.com/r/webs/gate" ) func init() {@@ -60,30 +61,11 @@ default:
} } -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)
M
go.mod
→
go.mod
@@ -10,7 +10,7 @@ github.com/rivo/uniseg v0.0.0-20190313204849-f699dde9c340 // indirect
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
M
go.sum
→
go.sum
@@ -34,7 +34,5 @@ gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdrxJNoY=
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=