Skip to main content

Publishing the sheets

The gateways work from two kinds of signed sheets. The product sheet holds the routes of the API host, the tiers and the limit of your backend; a key sheet holds the keys of one scope. The package writes both into the store the gateways read, and a copy of each into the object storage.

The product sheet​

\Pushery\FrontlineApi\Jobs\PublishRuleSheet publishes the product sheet the gateway reads: the routes of the API host, the tiers of your tier source and the limits of your backend, signed with the key in frontline-api.signing.key. Dispatch it after a deployment:

use Pushery\FrontlineApi\Jobs\PublishRuleSheet;

PublishRuleSheet::dispatch();

A changed tier reaches the gateways through an event, and the package's own schedule runs every minute, which also gives the gateway every sheet again within a day, as it expects: your schedule needs no entry for it (When a key or a tier changes). A run whose sheet has not changed publishes nothing, unless the store no longer holds the sheet or 20 hours have passed since it went out. Each publication takes the next number of the counter in the frontline_sequences table, never one below the current time in milliseconds, and the frontline_sheets table records what went out. The job needs:

  • frontline-api.product, frontline-api.environment, and the Redis connection named in frontline-api.store.connection, which must put no prefix before its keys;
  • the signing key and its label;
  • the filesystem disk named in frontline-api.copies.disk, frontline by default, on the object storage the gateways read when the store is down: every sheet goes there first, byte for byte, under frontline/v1/<product>/<environment>/sheets/, and a copy the disk does not take stops the publication before the store sees it;
  • a default cache store that can hold a lock, such as redis or database.

Without one of them the job fails, and its message names the setting and its environment variable, never a key. When the store already holds the sheet under a higher number, as after the database was restored from an older backup, the job refuses to publish below it: move the counter in frontline_sequences past the highest number your gateways report, then dispatch the job again. It refuses a number not above the highest one a gateway reports for the product sheet or the heartbeat it holds the same way, as when the store lost its index as well, with a \Pushery\FrontlineApi\Exceptions\SequenceBelowGateways that names the gateway and its number, before anything goes to the disk or the store; the refusal stays on the record of the sheet, and the health check sheets names it. A report the job cannot read stops nothing.

A gateway loads no envelope of more than 40,000,000 bytes, of the product sheet, a key sheet or the heartbeat, and keeps the sheet it holds. The job refuses a product sheet above that bound before it takes a number, with a \Pushery\FrontlineApi\Exceptions\SheetTooLarge that names the sheet: nothing goes to the disk or the store, and the refusal stays on the record of the sheet in effect, where the health check sheets names it. It takes tens of thousands of routes, or of media types on them, to come near that bound.

The key sheets​

\Pushery\FrontlineApi\Jobs\PublishKeySheet publishes the key sheet of one scope from your key source: the product scope, or one tenant's. Dispatch it whenever a key is issued, changed or revoked, or a tenant changes its tier, for the scope the key belongs to:

use Pushery\FrontlineApi\Jobs\PublishKeySheet;
use Pushery\FrontlineApi\Keys\KeyScope;

PublishKeySheet::dispatch(KeyScope::product());

Where each tenant's keys have a sheet of their own, dispatch it for KeyScope::tenant(...) with the tenant part of the key, as your key source names the scopes.

A key your source no longer returns is missing from the next sheet, and so is a key whose not_after has passed. One job of a scope waits in the queue at a time: one dispatched while another waits is dropped, and one dispatched while another runs waits for it and then publishes what changed meanwhile, so no change is lost. Both jobs return the sheet they published, with its name, sequence and size, or nothing when it stood unchanged; set force on a job to publish it anyway.

The size of a key sheet​

A gateway loads a key sheet of at most 100,000 keys whose envelope holds at most 40,000,000 bytes, and refuses a larger one, keeping the sheet it holds. The job stops building a sheet at its 100,001st key, and refuses an envelope above the bound before the sheet takes a sequence. Nothing goes to the store or the disk then, and the job fails with a \Pushery\FrontlineApi\Exceptions\SheetTooLarge that names the sheet. The refusal stays on the record of the sheet in effect, and the health check sheets fails at once and names it, since a key revoked after that sheet still works at the gateways; the next publication of the sheet clears it. A scope that never went out has no record, and its keys reach no gateway at all. Up to 20,000 keys a gateway reloads a sheet in under a second while it serves requests; above that each reload slows them, and the health check sheets warns. Before the product scope grows that far, let your key source name a scope per tenant, so that each tenant's keys go out in a sheet of their own.

When a key or a tier changes​

Dispatch an event of the package whenever your keys or tiers change, and it queues the publication of the sheet that changed, once the transaction you dispatch it in commits:

use Pushery\FrontlineApi\Events\KeysChanged;
use Pushery\FrontlineApi\Events\TiersChanged;
use Pushery\FrontlineApi\Keys\KeyScope;

KeysChanged::dispatch(KeyScope::product());
TiersChanged::dispatch();

KeysChanged stands for a key issued, changed or revoked, or a tenant that changed its tier, and names the scope of its key sheet. TiersChanged stands for a tier or a limit that changed. While frontline-api.product and frontline-api.environment are set and the package runs its migrations, it also adds a job to your schedule that publishes every sheet each minute, on one server at a time: the net under the events. It catches what a lost event left behind and a key whose not_after passed, and leaves every unchanged sheet as it is. Each run ends with the heartbeat, a signed message with the number of the current sheets and a digest over their names and sequences, both without a sheet that has expired, as a gateway counts the sheets it holds: a gateway whose sheets give another digest reconciles at once, and one that misses the heartbeat for three minutes raises an alarm. Run your scheduler and a queue worker for both.

Each of these jobs waits in the queue once at a time, and a dispatch while it waits is dropped. A job the queue loses before a worker starts it, to php artisan queue:clear or to a Redis database flushed while the cache kept its keys, holds its place for five minutes at most. The next dispatch after that queues it again, so the net and the heartbeat resume on their own.

Publishing at once​

php artisan frontline:publish publishes the product sheet and the key sheet of every scope your key source names, without the queue, and says what went out: each sheet with its sequence and size, or that it stands unchanged in the store. Run it as a step of each deployment. --product publishes the product sheet only, --keys the key sheets only, and --scope=<tenant> the key sheet of one scope your key source names; --scope=product names the product scope, or a tenant called product where your key source keeps a scope per tenant. A value that names no scope of your key source is refused, since its key sheet would go out without keys. A sheet the store holds unchanged is left alone; --force publishes it again under a new sequence, the way back when a sheet in the store is damaged. A sheet that cannot be published is reported with its message, never with a key, the others are still tried, and the command fails. A key source that cannot name its scopes is reported the same way, after the product sheet was tried.

Withdrawing a scope​

A scope your key source no longer names, as after a tenant was deleted, is withdrawn by the net that publishes every sheet each minute. Its key sheet goes out once more without keys under a new sequence, so every key of the scope is unknown to the gateways from then on, as after any revocation. The sheet without keys lives as long as the last sheet of the scope would have, and at least ten minutes, and it is not renewed. Once its lifetime has ended, the package removes it from the store, its index, the object storage and the frontline_sheets table, and the heartbeat no longer names it: a heartbeat counts only the sheets whose lifetime has not ended. A scope that comes back gets its next sheet under the next sequence.

Testing what you publish​

Pushery\FrontlineApi\Testing\FakeSheetStore holds the store in memory for your tests. bind() puts it in place of the connection frontline-api.store.connection names, without a key prefix, and fakes the disk of the copies, while every other Redis connection of your application stays as it was. useTestSigningKey() signs every sheet with a key from a fixed seed and returns its public half; it refuses to run outside the environment testing. The store then takes what the package publishes the way the real store does, and gives each sheet back decoded:

use Pushery\FrontlineApi\Testing\FakeSheetStore;

it('publishes the product sheet with the blocked route', function () {
$store = FakeSheetStore::bind();
FakeSheetStore::useTestSigningKey();

$this->artisan('frontline:block', ['route' => 'api.imports.store', '--reason' => 'Imports overload the database.'])
->assertSuccessful();

$route = collect($store->sheet('product')['routes'])->firstWhere('name', 'api.imports.store');

expect($route['blocked'])->toBeTrue();
});

sheet() takes product, keys, keys:<tenant> or heartbeat, and returns null while the store holds none. envelope() returns the envelope as the store holds it, with its signatures, sequence() the sequence the index records, and publications() every sheet written, in order.