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:
func (r *Riders) AllowLocation(ctx context.Context, riderID int64) (bool, error)
func (r *Riders) PublishLocation(ctx context.Context, orderID int64, lat, lng float64) errorCustomers 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.4601andlng: 77.0302to the order-specific channelorder:{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:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B5cd 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- The per-rider limiter rejects an immediate repeat and allows another rider independently.
- The limiter allows another update after 2100 ms.
- Location publishing sends
latandlngJSON on the order-specific channel. - The handler checks the limit before
Pingand publishes only forPICKED_UPorders. - The subscriber forwards location payloads to the customer's WebSocket connection.
- The rider screen has a Delivered button, which the next exercise wires.
Commands
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B5cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/live/Tests
TestW5B5_LocationUpdatesAreLimitedToOneEveryTwoSecondsPerRiderinbackend/internal/geo/live/location_test.goTestW5B5_PublishLocationSendsLatLngOnTheOrderChannelinbackend/internal/geo/live/location_test.go