All jobs

Add Request Timeouts to a Fiber App

Yes. An agent sets read, write and idle timeouts on your Fiber v3 server, wraps slow routes in the timeout middleware, tests the 503, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how the server is built before touching timeouts, because Fiber has two different kinds and a copied snippet usually sets only one. In an app started from gofiber/boilerplate, go.mod requires github.com/gofiber/fiber/v3 v3.6.1 on Go 1.26, and app.go creates the app with fiber.New(fiber.Config{}), an empty config, which leaves ReadTimeout, WriteTimeout and IdleTimeout at their documented default of unlimited. It adds recover.New() and logger.New(), mounts GET and POST /api/v1/users on handlers.UserList and handlers.UserCreate, serves ./static/public on /*, falls through to handlers.NotFound, and listens with app.Listen(*port, fiber.ListenConfig{EnablePrefork: *prod}) on two flags, -port defaulting to :3000 and -prod for prefork. database/database.go is a slice behind a mutex, so no handler today waits on anything, and there is no _test.go file at all even though .github/workflows/test.yml runs go test ./... -v -race on Go 1.26.x and 1.27.x across Linux and Windows runners.

Server timeouts come first. The agent adds three flag.Duration flags beside port and prod, -read-timeout, -write-timeout and -idle-timeout, with defaults of 5 seconds, 10 seconds and 2 minutes, and passes them into fiber.Config as ReadTimeout, WriteTimeout and IdleTimeout, which app.go in Fiber copies straight onto the fasthttp server. Fiber's own source notes that Shutdown does not close keep-alive connections and recommends a non-zero ReadTimeout for that reason, and that IdleTimeout falls back to ReadTimeout when zero, so the pull request quotes both lines. Because prefork children build the same fiber.Config, the -prod path gets the same limits without extra code.

Handler timeouts are the second kind, and they are per route. The agent imports github.com/gofiber/fiber/v3/middleware/timeout and wraps the two API handlers, timeout.New(handlers.UserList, timeout.Config{Timeout: 2 * time.Second}) and the same for UserCreate, leaving the static file route and NotFound alone. The middleware swaps the request context for one with a deadline, runs the handler in a goroutine, and when the deadline passes answers fiber.ErrServiceUnavailable, a 503, not a 408; the comment in timeout.go explains that a slow handler is a server-side delay and a 408 would invite the client to resend a request whose handler may still be running. Handlers that do real work are meant to watch c.Context().Done(), so the agent adds that check to UserCreate as the example for whoever replaces the in-memory slice with a database.

Tests follow the workflow's command. app_test.go builds the app through a new newApp() function the agent extracts from main, so main and the tests share the same middleware and routes. It sends GET /api/v1/users through app.Test and asserts 200, registers a deliberately slow handler wrapped in timeout.New with a 50 millisecond limit and asserts 503 with the middleware's message, and checks that app.Config().ReadTimeout carries the flag value. app.Test itself has a one-second default with FailOnTimeout true, so the slow case passes fiber.TestConfig{Timeout: 3 * time.Second}. In the sandbox the agent runs go vet ./..., go test ./... -v -race, starts go run app.go and shows curl -i against /api/v1/users, then opens the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add request timeouts to this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, database/database.go, Makefile, Dockerfile and .github/workflows/test.yml first. Add -read-timeout, -write-timeout and -idle-timeout flag.Duration flags beside the existing port and prod flags with defaults of 5s, 10s and 2m, and pass them into fiber.Config as ReadTimeout, WriteTimeout and IdleTimeout. Wrap handlers.UserList and handlers.UserCreate with timeout.New from github.com/gofiber/fiber/v3/middleware/timeout using a 2 second limit, leave the static route and NotFound unwrapped, and make UserCreate check c.Context().Done() before writing. Extract a newApp function from main so tests build the same app, then add app_test.go that asserts 200 on GET /api/v1/users, 503 from a slow handler wrapped with a 50ms timeout using fiber.TestConfig with a 3 second Test timeout, and that the flag values reach app.Config(). Explain in the pull request why the middleware answers 503 and not 408, quoting the comment in the middleware source. Run go vet ./... and go test ./... -v -race, start the app and curl -i /api/v1/users, then open a pull request that pastes the test and curl output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that changes app.go and handlers/handlers.go and adds app_test.go. The description lists the three new flags with their defaults, the two wrapped routes, the paragraph on 503 versus 408 with the line from middleware/timeout/timeout.go, and the curl -i output showing a normal 200 from /api/v1/users.

The run's record holds go vet, the race-enabled test run with the new cases, and the server start. Nothing changes in database/database.go, the Dockerfile, the Makefile or the workflows: the Go version matrix in test.yml already runs the new test file as it is.

Before merging, a reviewer picks the numbers. Five seconds of ReadTimeout is tight for a client on a slow link uploading a form, two seconds per handler is generous for an in-memory slice and may be too short once a database is behind it, and the static route may deserve a WriteTimeout of its own if large files are served from static/public. All three flags can be changed at start without a rebuild.

Run it on every new Go service

A team with several Fiber services starts one run per repository from its own server: one POST /o/{org}/sessions each, with that repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started rather than a second pull request. Each run reads that service's own fiber.Config and routes, so a service that already sets ReadTimeout gets only the handler wrapping and a service with a database gets the context check where its queries run.

Afterwards, an automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize and the prompt "if this pull request adds a route under /api that is not wrapped in timeout.New, or raises a timeout above 10 seconds, comment with the route and the limit" keeps new handlers inside the policy. The trigger adds the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The reason to hand this to an agent is that the two timeouts behave differently and the difference only shows under load: the server limits protect against slow clients, the middleware protects against slow handlers, and a handler that ignores its context keeps running after the 503 has gone out. The agent reads Fiber's source for the exact behaviour, writes the context check into the handler, and proves the 503 with a test rather than reasoning about it.

In its own sandbox the agent runs the race-enabled test suite and a real server, and the run keeps both outputs for the pull request. For a team with many Go services, each gets its own run and its own pull request with limits read from that service's routes.

Frequently asked questions

Why 503 and not 408 when a handler times out?

Because that is what Fiber's timeout middleware returns, and its source says why: a 408 tells the client its request was too slow and invites a resend, but here the client was fine and the handler was slow, and the handler goroutine may still be running. The pull request quotes the comment so a reviewer who expects 408 sees the reasoning.

Do the server timeouts apply with prefork on?

Yes. The -prod flag turns on EnablePrefork in fiber.ListenConfig, and each child process builds the app from the same fiber.Config, so ReadTimeout, WriteTimeout and IdleTimeout are set in every child. The flags are parsed before the app is created, so the children see the same values.

Why is the static route not wrapped?

The /* route serves files from static/public and a large file on a slow connection can legitimately take longer than an API call. WriteTimeout already bounds how long a response write may take, which is the right limit for file serving. The pull request says so, and wrapping it later is one line.

What happens to a handler that keeps running after the timeout?

The middleware returns at once and the goroutine finishes on its own; the source describes how the abandoned context is reclaimed once the handler exits. That is why the agent adds the c.Context().Done() check to UserCreate: a handler that stops when the deadline passes does no wasted work, and the same pattern is what a database call should follow.

More Fiber jobs

The same job on other stacks

Keep reading