Skip to content

Commit

Permalink
[performance] cache domains after max retries in transport (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaaaWhatsUpDoc authored Oct 8, 2022
1 parent 3bb45b7 commit e58a6a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
26 changes: 17 additions & 9 deletions internal/transport/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type controller struct {
fedDB federatingdb.DB
clock pub.Clock
client pub.HttpClient
cache cache.Cache[string, *transport]
trspCache cache.Cache[string, *transport]
badHosts cache.Cache[string, struct{}]
userAgent string
}

Expand All @@ -66,13 +67,20 @@ func NewController(db db.DB, federatingDB federatingdb.DB, clock pub.Clock, clie
fedDB: federatingDB,
clock: clock,
client: client,
cache: cache.New[string, *transport](),
trspCache: cache.New[string, *transport](),
badHosts: cache.New[string, struct{}](),
userAgent: fmt.Sprintf("%s; %s (gofed/activity gotosocial-%s)", applicationName, host, version),
}

// Transport cache has TTL=1hr freq=1m
c.cache.SetTTL(time.Hour, false)
if !c.cache.Start(time.Minute) {
// Transport cache has TTL=1hr freq=1min
c.trspCache.SetTTL(time.Hour, false)
if !c.trspCache.Start(time.Minute) {
log.Panic("failed to start transport controller cache")
}

// Bad hosts cache has TTL=15min freq=1min
c.badHosts.SetTTL(15*time.Minute, false)
if !c.badHosts.Start(time.Minute) {
log.Panic("failed to start transport controller cache")
}

Expand All @@ -89,7 +97,7 @@ func (c *controller) NewTransport(pubKeyID string, privkey *rsa.PrivateKey) (Tra
pubStr := privkeyToPublicStr(privkey)

// First check for cached transport
transp, ok := c.cache.Get(pubStr)
transp, ok := c.trspCache.Get(pubStr)
if ok {
return transp, nil
}
Expand All @@ -102,13 +110,13 @@ func (c *controller) NewTransport(pubKeyID string, privkey *rsa.PrivateKey) (Tra
}

// Cache this transport under pubkey
if !c.cache.Put(pubStr, transp) {
if !c.trspCache.Put(pubStr, transp) {
var cached *transport

cached, ok = c.cache.Get(pubStr)
cached, ok = c.trspCache.Get(pubStr)
if !ok {
// Some ridiculous race cond.
c.cache.Set(pubStr, transp)
c.trspCache.Set(pubStr, transp)
} else {
// Use already cached
transp = cached
Expand Down
21 changes: 20 additions & 1 deletion internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,23 @@ func (t *transport) POST(r *http.Request, body []byte, retryOn ...int) (*http.Re

func (t *transport) do(r *http.Request, signer func(*http.Request) error, retryOn ...int) (*http.Response, error) {
const maxRetries = 5
backoff := time.Second * 2

var (
// Initial backoff duration
backoff = 2 * time.Second

// Get request hostname
host = r.URL.Hostname()
)

// Check if recently reached max retries for this host
// so we don't need to bother reattempting it. The only
// errors that are retried upon are server failure and
// domain resolution type errors, so this cached result
// indicates this server is likely having issues.
if t.controller.badHosts.Has(host) {
return nil, errors.New("too many failed attempts")
}

// Start a log entry for this request
l := log.WithFields(kv.Fields{
Expand Down Expand Up @@ -155,6 +171,9 @@ func (t *transport) do(r *http.Request, signer func(*http.Request) error, retryO
}
}

// Add "bad" entry for this host
t.controller.badHosts.Set(host, struct{}{})

return nil, errors.New("transport reached max retries")
}

Expand Down

0 comments on commit e58a6a2

Please sign in to comment.