YouTubePHP Documentation

Meter
in package

FinalYes

Counts what today's requests have cost against the project's daily quota.

YouTube gives each Google Cloud project a daily allowance, reset at midnight Pacific time: 10,000 units shared by most methods, plus separate buckets of 100 calls for search.list and for videos.insert. Every request costs, including ones that fail, and a project that runs out gets quotaExceeded for everything until the reset. Cost lists what each method costs.

The meter is an estimate kept on this side: it cannot see requests other programs make with the same project, and a few costs are unpublished. When Google says the quota is spent, exhaust() brings the count into line.

  • Refusing: Http charges every attempt it sends and refuses a call the day's remaining quota cannot pay for, before sending it.
  • Reserve: some units can be kept back. canAfford() leaves them out when asked to, so a background job such as reading chat stops short of the limit and leaves room for what a person asks for.
  • Restarts: with a path, the count is written to a JSON file on every charge and read back at start, so a restart does not forget the day.
Tags
since
1.0.0
author

Valithor Obsidion valithor@valgorithms.com

Table of Contents

Constants

DEFAULT_BUCKET  : mixed = 'default'
The bucket most methods draw from.
TIMEZONE  : mixed = 'America/Los_Angeles'
The quota day runs midnight to midnight here.

Properties

$clock  : ?Closure
$day  : string
The quota day the counts belong to, as `Y-m-d` in Pacific time.
$limits  : array<string, int>
$logger  : LoggerInterface
$path  : ?string
$reserve  : int
$used  : array<string, int>

Methods

__construct()  : mixed
bucket()  : string
The bucket a method draws from.
canAfford()  : bool
Whether today's quota can pay for calls to a method.
cost()  : int
What one call to a method costs, in its bucket's units.
day()  : string
The quota day, as `Y-m-d` in Pacific time.
exhaust()  : void
Google said the quota is spent, so the rest of the day is: whatever this count thought, other programs using the project got there first.
limit()  : int
A bucket's daily allowance.
remaining()  : int
What is left of a bucket today, reserve included.
reserve()  : int
Units of the default bucket kept back from background work.
resetsAt()  : CarbonImmutable
When the quota next resets: the coming midnight, Pacific time.
snapshot()  : CarbonImmutable, reserve: int, buckets: array}
Today's count, for a status command.
spend()  : void
Charges for calls to a method.
used()  : int
What today has used of a bucket.
load()  : void
now()  : CarbonImmutable
roll()  : void
Starts a fresh count once midnight Pacific has passed.
save()  : void
today()  : string

Constants

DEFAULT_BUCKET

The bucket most methods draw from.

public mixed DEFAULT_BUCKET = 'default'

TIMEZONE

The quota day runs midnight to midnight here.

public mixed TIMEZONE = 'America/Los_Angeles'

Properties

$clock read-only

private ?Closure $clock = null

$day

The quota day the counts belong to, as `Y-m-d` in Pacific time.

private string $day

$limits

private array<string, int> $limits

Bucket => its daily allowance.

$logger

private LoggerInterface $logger

$path read-only

private ?string $path = null

$reserve read-only

private int $reserve = 0

$used

private array<string, int> $used = []

Bucket => what today has used.

Methods

__construct()

public __construct([array<string, int> $limits = [] ][, int $reserve = 0 ][, string|null $path = null ][, callable(): float|null $clock = null ][, ?LoggerInterface $logger = null ]) : mixed
Parameters
$limits : array<string, int> = []

Bucket => daily allowance, for any that differ from Cost::DAILY - a project granted more quota.

$reserve : int = 0

Units of the default bucket to keep back, see canAfford().

$path : string|null = null

A JSON file to keep the count in across restarts.

$clock : callable(): float|null = null

The time as a Unix timestamp, for tests.

$logger : ?LoggerInterface = null

bucket()

The bucket a method draws from.

public bucket(string $endpoint) : string
Parameters
$endpoint : string
Return values
string

canAfford()

Whether today's quota can pay for calls to a method.

public canAfford(string $endpoint[, bool $keepReserve = false ][, int $times = 1 ]) : bool
Parameters
$endpoint : string
$keepReserve : bool = false

Leave the reserve out of what is available. A job that runs by itself should, so it does not spend what a person may need later in the day.

$times : int = 1
Return values
bool

cost()

What one call to a method costs, in its bucket's units.

public cost(string $endpoint) : int
Parameters
$endpoint : string
Return values
int

day()

The quota day, as `Y-m-d` in Pacific time.

public day() : string
Return values
string

exhaust()

Google said the quota is spent, so the rest of the day is: whatever this count thought, other programs using the project got there first.

public exhaust(string $endpoint) : void
Parameters
$endpoint : string

limit()

A bucket's daily allowance.

public limit([string $bucket = self::DEFAULT_BUCKET ]) : int
Parameters
$bucket : string = self::DEFAULT_BUCKET
Return values
int

remaining()

What is left of a bucket today, reserve included.

public remaining([string $bucket = self::DEFAULT_BUCKET ]) : int
Parameters
$bucket : string = self::DEFAULT_BUCKET
Return values
int

reserve()

Units of the default bucket kept back from background work.

public reserve() : int
Return values
int

resetsAt()

When the quota next resets: the coming midnight, Pacific time.

public resetsAt() : CarbonImmutable
Return values
CarbonImmutable

snapshot()

Today's count, for a status command.

public snapshot() : CarbonImmutable, reserve: int, buckets: array}
Return values
CarbonImmutable, reserve: int, buckets: array}

spend()

Charges for calls to a method.

public spend(string $endpoint[, int $times = 1 ]) : void
Parameters
$endpoint : string
$times : int = 1

used()

What today has used of a bucket.

public used([string $bucket = self::DEFAULT_BUCKET ]) : int
Parameters
$bucket : string = self::DEFAULT_BUCKET
Return values
int

load()

private load() : void

now()

private now() : CarbonImmutable
Return values
CarbonImmutable

roll()

Starts a fresh count once midnight Pacific has passed.

private roll() : void

save()

private save() : void

today()

private today() : string
Return values
string
On this page

Search results