Order location stream

You build the order location stream, accepting rider coordinates through a per-rider 2000 ms Redis limit and publishing them on an order channel. The API forwards accepted payloads to the customer's WebSocket connection while the order is PICKED_UP.

Ticket

Build the backend order-location stream. It accepts rider coordinates, applies a per-rider Redis limit, publishes accepted coordinates on order:{id}:loc, and forwards them to the customer's WebSocket connection while the order is PICKED_UP.

The tests call these two methods directly:

go
func (r *Riders) AllowLocation(ctx context.Context, riderID int64) (bool, error)
func (r *Riders) PublishLocation(ctx context.Context, orderID int64, lat, lng float64) error

Customers need to follow the rider after pickup. The next exercise uses this stream to display the rider on the customer map. Keep location throttling separate from the 30-second assignment Exclusive claim with SET NX PX, which decides which rider can accept an order.

Acceptance criteria

  • The per-rider limiter allows the first update, rejects an immediate second update from the same rider without an error, allows another rider's update independently, and allows the original rider again after 2100 ms.
  • Publishing coordinates succeeds without an error and sends JSON containing numeric lat: 28.4601 and lng: 77.0302 to the order-specific channel order:{id}:loc.

Constraints

  • Use a 2000 ms expiry for the per-rider location limit.
  • Keep each rider's limit independent of other riders.

Design direction

Keep AllowLocation and PublishLocation on the existing Riders type in backend/internal/geo/riders.go. Let AllowLocation decide whether an update is accepted. Let PublishLocation handle serialization and Redis publishing.

In the rider location handler, check the limit before calling Ping or processing the coordinates. Return HTTP 429 with Retry-After: 2 when Redis rejects an update. For an accepted update, call Ping. Publish the coordinates only when the rider has an order in PICKED_UP.

Add one goroutine in backend/cmd/api/main.go with PSubscribe("order:*:loc"). Extract the order ID from each channel, look up its customer, and forward the payload with hub.Send. Keep the subscriber separate from the HTTP handler. Redis Pub/Sub does not retain messages for customers who are disconnected, so it does not provide replay.

For the app, add the rider's Delivered button. The next exercise wires it. Any package layout that meets the contract passes. Consider the trade-off between keeping Redis operations in Riders and putting application-specific routing in the API. Also keep the location stream independent of the order assignment claim.

How to check your work

Run the package tests and the race-enabled test command:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B5
bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/live/

Read the first failing assertion literally: it tells you whether the limiter accepted or rejected the wrong call, failed to allow the update after 2100 ms, or published a payload with the wrong coordinates.

Finished Exercise 5.11?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. The per-rider limiter rejects an immediate repeat and allows another rider independently.
  2. The limiter allows another update after 2100 ms.
  3. Location publishing sends lat and lng JSON on the order-specific channel.
  4. The handler checks the limit before Ping and publishes only for PICKED_UP orders.
  5. The subscriber forwards location payloads to the customer's WebSocket connection.
  6. The rider screen has a Delivered button, which the next exercise wires.

Commands

terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B5
terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/live/

Tests

  • TestW5B5_LocationUpdatesAreLimitedToOneEveryTwoSecondsPerRider in backend/internal/geo/live/location_test.go
  • TestW5B5_PublishLocationSendsLatLngOnTheOrderChannel in backend/internal/geo/live/location_test.go