Push relay service
The push relay service is a small standalone gateway that stores mobile push registrations and delivers notifications to Apple Push Notification service (APNs). The main Pipedash server sends a relay subscription ID and notification data to this service, so the relay can keep APNs credentials and device tokens outside the main application.
The relay is a child service of Event delivery and notifications. Its stored registrations and client-facing notification behavior are described with the client notifications page.
Service role
Delivery boundary
The relay separates notification delivery from the main Pipedash server. A mobile client registers its APNs token with the relay, while the Pipedash server later uses the returned subscription ID to request delivery.
The relay owns the APNs-specific work. It selects the APNs environment, signs requests with the configured Apple key, builds the alert payload, and turns APNs results into a small set of HTTP outcomes.
The following diagram shows the normal delivery path.
flowchart LR
Client[Mobile client] -->|register token| Relay[Push relay]
Relay -->|subscription ID| Server[Pipedash server]
Server -->|notification + subscription ID| Relay
Relay -->|APNs request| APNs[Apple Push Notification service]
APNs -->|delivery result| Relay
Relay -->|accepted, gone, or failure| Server
References: crates/…/main.rs• L80-116 crates/…/routes.rs• L103-114 crates/…/push.rs• L213-253
Deployment boundary
The service runs as its own binary and can run in the supplied container image. The image listens on port 8080, stores its SQLite database under /data, and runs as a non-root user. The /data volume is therefore part of the service's durable state.
References: crates/…/Cargo.toml• L1-13 docker/push-relay.Dockerfile• L42-56
Configuration
Runtime settings
The relay reads its settings from environment variables when it starts. It fails to start when the APNs identity or allowed application bundle list is missing, because these values define who may use the relay and which Apple account signs requests.
| Setting | Purpose | Default or requirement |
|---|---|---|
RELAY_BIND_ADDR | Network address for the HTTP service | 0.0.0.0:8080 |
RELAY_DATABASE_PATH | SQLite database location | pipedash-push-relay.db |
RELAY_APNS_KEY | APNs private key content | Use this or RELAY_APNS_KEY_PATH |
RELAY_APNS_KEY_PATH | File containing the APNs private key | Required when the key is not inline |
RELAY_APNS_KEY_ID | Apple key ID | Required |
RELAY_APNS_TEAM_ID | Apple team ID | Required |
RELAY_ALLOWED_BUNDLE_IDS | Comma-separated application bundle IDs | Required and must contain at least one ID |
RELAY_TRUSTED_PROXY | Allows client identity from X-Forwarded-For | Disabled unless set to true or 1 |
The allowed bundle list is checked during registration. The trusted proxy switch changes only how the relay identifies a client for registration limits, so it must match the network path that fronts the service.
References: crates/…/config.rs• L7-69
APNs credentials
The relay expects an APNs ES256 private key in PEM form, plus its key ID and team ID. It creates a signed provider token and reuses that token for less than an hour. An APNs response that says the provider token expired causes the relay to create a fresh token for later requests.
References: crates/…/config.rs• L41-68 crates/…/apns.rs• L58-121 crates/…/apns.rs• L314-335
HTTP interface
Health route
GET /healthz returns ok without reading or changing subscriptions. It is suitable for a process or load-balancer health check.
References: crates/…/routes.rs• L103-114 crates/…/routes.rs• L227-229
Subscription routes
Subscription routes create and maintain the relay's link to one device. A successful registration returns a random subscription ID. The relay stores only a hash of that ID, which lets the ID act as a secret lookup value without placing the value itself in the database.
| Route | Responsibility | Main result |
|---|---|---|
POST /v1/subscriptions | Register a device token, APNs environment, and bundle ID | 201 with subscription_id |
PUT /v1/subscriptions/{subscription_id} | Replace a rotated device token | 204 |
DELETE /v1/subscriptions/{subscription_id} | Remove a registration | 204 |
The relay accepts only valid hexadecimal device tokens, the production or sandbox environment, and bundle IDs from its allow-list. It keeps at most ten registrations for one device token, so newer registrations displace the oldest ones.
References: crates/…/routes.rs• L152-199 crates/…/routes.rs• L231-294 crates/…/store.rs• L40-55 crates/…/store.rs• L80-114
Notification route
POST /v1/notifications accepts a subscription ID and one alert description. It first resolves the subscription, then sends the alert to APNs using the stored device token, environment, and bundle ID. A successful APNs request returns 202 Accepted.
The request supports two notification forms:
- A title and body, with optional thread and pipeline details. Pipeline details add a destination that the client can use to open the related run.
- An
alert_idfor the newer generic alert form. This form carries the alert ID to the client and supports APNs collapse and expiry behavior.
The relay rejects mixed forms and oversized request fields before it contacts APNs. The total request body limit is 16 KiB.
References: crates/…/routes.rs• L60-71 crates/…/routes.rs• L164-179 crates/…/routes.rs• L296-375 crates/…/apns.rs• L139-241
Rate limiting
Request windows
The relay uses separate fixed windows for registration and delivery. Registration limits protect the public token-registration surface, while delivery limits apply independently to each subscription so one busy device does not block other devices.
| Operation | Limit | Key |
|---|---|---|
| Registration | 20 requests per hour | Client IP, or forwarded client IP when a trusted proxy is enabled |
| Notification | 30 requests per minute | Hashed subscription lookup key |
An IPv6 client is grouped by its /64 prefix. When proxy trust is disabled, forwarded headers do not change the client identity. Requests over a window receive 429 Too Many Requests.
References: crates/…/routes.rs• L67-71 crates/…/routes.rs• L202-225 crates/…/routes.rs• L231-238 crates/…/routes.rs• L343-354
Limiter cleanup
Rate-limit state is kept in memory and is removed after its window ends. A background sweep runs every five minutes to discard inactive keys, so the limiter does not grow with every client that has ever connected.
References: crates/…/rate_limit.rs• L16-66 crates/…/main.rs• L30 crates/…/main.rs• L70-77
Persistence
Subscription records
The relay uses SQLite as its durable store. Each record contains the hashed subscription ID, device token, APNs environment, bundle ID, creation time, and last activity time. Database migrations run when the service opens the database.
The store uses write-ahead logging and a short busy timeout. This supports the small concurrent workload of registration, token rotation, delivery, and cleanup without turning the relay into a separate database service.
References: crates/…/store.rs• L40-77 crates/…/20261024000001_subscriptions.sql• L1-11
Subscription lifetime
Token rotation and successful delivery update the last activity time. A background task removes registrations that have been inactive for 90 days, and it checks for expired records once per hour. A failed APNs request does not refresh or remove a registration because a temporary provider or network error does not prove that the device token is invalid.
References: crates/…/main.rs• L28-29 crates/…/main.rs• L55-67 crates/…/store.rs• L137-180 crates/…/routes.rs• L355-374
APNs delivery
Environment routing
Each subscription records whether its token belongs to the production or sandbox APNs environment. The relay sends the request to the matching Apple host and uses the registered bundle ID as the APNs topic.
The relay sends alert pushes over HTTP/2 with a short connection timeout and request timeout. It signs each request with a bearer provider token and sets alert priority and push type headers.
References: crates/…/apns.rs• L31-37 crates/…/apns.rs• L262-305
Delivery outcomes
The relay maps APNs results to subscription state and HTTP responses. This keeps token cleanup tied to an explicit invalid-token result instead of removing registrations for every provider error.
| APNs result | Relay action | HTTP result |
|---|---|---|
| Accepted | Touch the subscription's activity time | 202 Accepted |
| Unregistered or bad device token | Delete the record only if its token is still unchanged | 410 Gone |
| Network error or other APNs rejection | Keep the record for a later attempt | 502 Bad Gateway |
The token comparison matters when a device rotates its token while an older notification is still in flight. The older failure must not delete the newer token.
References: crates/…/apns.rs• L124-136 crates/…/routes.rs• L355-374 crates/…/store.rs• L162-180
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the relay request fields aligned with the Pipedash server's relay client and with the mobile payload fields.
- Keep allowed bundle IDs aligned with the APNs topic and the app builds that register device tokens.
- Preserve the same subscription-ID hashing rule for every create, lookup, rotate, delete, and notification path.
- If rate-limit keys or proxy handling change, review the edge proxy configuration because client grouping can change without an API change.
- If subscription cleanup or token error handling changes, verify that a rotated token cannot be removed by an older in-flight APNs result.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing APNs key, key ID, team ID, or bundle allow-list | The relay does not start | Startup configuration validation |
| Invalid or unapproved device registration | No subscription is created | Token validation and bundle allow-list check |
| Registration burst from one client | Registration receives 429 | Per-client hourly window |
| More than 30 notifications for one subscription in one minute | Notification receives 429 | Per-subscription delivery window |
| APNs reports an unregistered token | The matching subscription is removed and the caller receives 410 | Delete only when the stored token still matches |
| APNs is unavailable or rejects another request | The subscription remains and the caller receives 502 | Failure classification keeps the record |
| Database path is not persistent in deployment | Registrations disappear after restart | Mount the relay data volume |
Key files
| File | Why to read it |
|---|---|
| crates/…/main.rs | Service startup, background cleanup, listener, and shutdown |
| crates/…/config.rs | Runtime settings and required APNs configuration |
| crates/…/routes.rs | HTTP routes, request contracts, validation, and response outcomes |
| crates/…/store.rs | Subscription state and lifecycle rules |
| crates/…/apns.rs | APNs signing, payloads, environment routing, and result mapping |
| crates/…/rate_limit.rs | Fixed-window rate-limit behavior |
| crates/…/20261024000001_subscriptions.sql | Durable subscription schema |
docker/push-relay.Dockerfile | Container defaults, port, user, and data volume |
| crates/…/push.rs | Main-server calls into the relay and handling of relay outcomes |