usage

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Window = 24 * time.Hour

Window is how far back the record looks.

Variables

This section is empty.

Functions

func OnTooManyRequests

func OnTooManyRequests(_ *http.Request, resp *http.Response) bool

OnTooManyRequests treats HTTP 429 as a limit hit.

Types

type LimitFunc

type LimitFunc func(req *http.Request, resp *http.Response) bool

LimitFunc says whether a response means the service refused because of a limit.

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

func NewTracker(db *sql.DB) (*Tracker, error)

NewTracker creates a tracker and loads the limit hits stored in db within the window. db may be nil (memory only).

func (*Tracker) RecordLimitHit

func (t *Tracker) RecordLimitHit(service string)

RecordLimitHit notes that service refused a request because of a limit.

func (*Tracker) RecordRequest

func (t *Tracker) RecordRequest(service string)

RecordRequest counts one request to service.

func (*Tracker) Stats

func (t *Tracker) Stats(service string) Stats

Stats returns the last 24 hours for 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL