Documentation
¶
Overview ¶
Package usage keeps a rolling 24-hour record of how much each third-party service (TMDB, Trakt, OpenSubtitles) is used and how often it refused a request because a limit was reached. Mediarium ships shared keys for these services, so the record lets it tell the person when the shared key is busy and a free personal key would help.
Request counts are kept in memory; limit hits are also stored in the database so they survive a restart.
Index ¶
Constants ¶
const Window = 24 * time.Hour
Window is how far back the record looks.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Stats ¶
type Stats struct {
Requests int
LimitHits int
LastLimitHit time.Time // zero when there were none
}
Stats is the last 24 hours for one service.
type Tracker ¶
type Tracker struct {
// OnLimitHit, when set, is called after each limit hit. It must not block.
OnLimitHit func(service string)
// contains filtered or unexported fields
}
Tracker records requests and limit hits per service. It is safe for concurrent use.
func NewTracker ¶
NewTracker creates a tracker and loads the limit hits stored in db within the window. db may be nil (memory only).
func (*Tracker) RecordLimitHit ¶
RecordLimitHit notes that service refused a request because of a limit.
func (*Tracker) RecordRequest ¶
RecordRequest counts one request to service.
func (*Tracker) Wrap ¶
func (t *Tracker) Wrap(service string, isLimit LimitFunc) func(http.RoundTripper) http.RoundTripper
Wrap returns a function that wraps an http.RoundTripper so every request is counted for service, and every response isLimit accepts is recorded as a limit hit.