new day, new libs. use gate library.

This commit is contained in:
Ted Unangst 2019-10-29 13:38:51 -04:00
parent 36d7ebe8ae
commit f27a4d9ce2
4 changed files with 26 additions and 65 deletions

View File

@ -29,10 +29,10 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/cache"
"humungus.tedunangst.com/r/webs/gate"
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/image"
"humungus.tedunangst.com/r/webs/junk" "humungus.tedunangst.com/r/webs/junk"
@ -117,24 +117,11 @@ func GetJunkHardMode(url string) (junk.Junk, error) {
return j, err return j, err
} }
var flightdeck = make(map[string][]chan JunkError) var flightdeck = gate.NewSerializer()
var decklock sync.Mutex
func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) { 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()
fn := func() (interface{}, error) {
at := thefakename at := thefakename
if strings.Contains(url, ".well-known/webfinger?resource") { if strings.Contains(url, ".well-known/webfinger?resource") {
at = "application/jrd+json" at = "application/jrd+json"
@ -144,23 +131,17 @@ func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
Agent: "honksnonk/5.0; " + serverName, Agent: "honksnonk/5.0; " + serverName,
Timeout: timeout, 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
}
}()
}
return j, err return j, err
} }
ji, err := flightdeck.Call(url, fn)
if err != nil {
return nil, err
}
j := ji.(junk.Junk)
return j, nil
}
func savedonk(url string, name, desc, media string, localize bool) *Donk { func savedonk(url string, name, desc, media string, localize bool) *Donk {
if url == "" { if url == "" {
return nil return nil

View File

@ -18,8 +18,9 @@ package main
import ( import (
"log" "log"
notrand "math/rand" notrand "math/rand"
"sync"
"time" "time"
"humungus.tedunangst.com/r/webs/gate"
) )
func init() { func init() {
@ -60,30 +61,11 @@ func sayitagain(goarounds int64, userid int64, rcpt string, msg []byte) {
} }
} }
var trucksout = 0 var garage = gate.NewLimiter(20)
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()
}
func deliverate(goarounds int64, userid int64, rcpt string, msg []byte) { func deliverate(goarounds int64, userid int64, rcpt string, msg []byte) {
truckgoesout() garage.Start()
defer truckcomesin() defer garage.Finish()
var ki *KeyInfo var ki *KeyInfo
ok := ziggies.Get(userid, &ki) ok := ziggies.Get(userid, &ki)

2
go.mod
View File

@ -10,7 +10,7 @@ require (
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 golang.org/x/net v0.0.0-20190620200207-3b0461eec859
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 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 go 1.11

6
go.sum
View File

@ -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= 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 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= 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.21 h1:bxI4ZFpxSZw06AbQsaTii5011wjxceF2MOfqJrFnM/w=
humungus.tedunangst.com/r/webs v0.6.19/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= humungus.tedunangst.com/r/webs v0.6.21/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=