sc: refresh prekeys when they fall below 10
context
- whenever a siganl account starts a new session with another account, the other account queries the signal server for prekeys, which the other account uses to initiate the double rathchet algorithm in an asyncronous manner (ie: one that does not require the original account to exchange messages with it synchronously to accomplish the original DH handshake)
- when an account is created, signalc publishes 100 prekeys to the signal server
- we are expected to refresh that stock periodically so that new contacts may fetch fresh keys, however, we do not
- this MR introduces a "just-in-time" design for implementing prekey refreshing that attempts to be responsive to bursty spikes in new sessions without consuming an undue amount of resources from foreground tasks
design
goals:
- provision as-needed: only ever try to refresh prekeys on an as-needed basis (when new sessions are created that might have depleted prekeys)
- maintain fairness: do not compete with (db/network) resources for message sending
- debouncing: when receiving bursty series of incoming messages to same channel, only attempt to refresh once per channel
implementation sketch:
- every time a session is created in protocol store, enqueue a background maybeRefreshPrekeysFor(account) job
- start by adding accountId to jobsInFlight set
- if newly added proceed, else return early
- wait for receive queue to drain (or timeout)
- check if prekeys below threshold (server call) if not return
- if so: make new keys (retrying if using existing id) and publish
- remove accountId from jobsInFlight set
Edited by aguestuser