rproxy: the two time limits get a type and a file of their own #432

Open
opened 2026-11-19 17:12:41 +01:00 by clawbot · 1 comment
Collaborator

Start once the issue that narrows the body wrappers is closed: #431. Both edit the same lines of internal/rproxy/bodies.go and request.go.

From the architecture review, "Inside rproxy", under "The time limits have no type of their own": #283 (comment). Approved for building: #283 (comment). The code was read at 809d266 on next. Line numbers are that commit's.

What is there

  • request in internal/rproxy/request.go ends with a mutex and the four fields it guards: timersStopped, requestTimer, responseTimer, requestEnd (lines 93 to 99).
  • Ten functions in the last third of the file work on them: requestDeadline, startRequestTimer, requestTimedOut, wroteRequest, startResponseTimer, responseTimedOut, responseReceived, endRequest, stopTimers, stopTimer (lines 509 to 717, among the functions that read the body).
  • They are called from four kinds of goroutine: the handler's own, the transport's trace hook, the two timers, and the read of the app's response body.
  • They read the clock directly, with time.Now, time.Until and time.AfterFunc.

Why it matters

  • This is the hardest code in rproxy: which side was being waited on when a limit passed decides between a 408 and a 504. It is methods on the same type as the path rewrite and the log line, so the type does not show which fields other goroutines touch.
  • It is tested only through whole requests over real connections with real waits. internal/rproxy/limits_test.go sleeps for 2.1 seconds in one test.

What changes

  • The two time limits become a type of their own, in a file of their own in internal/rproxy. It owns the mutex, the two timers and the end of the request. request holds one field of it.
  • The timers are then tested alone, with testing/synctest, which the tests of many other packages here use and rproxy's do not.

The sketch from the review, less a field that is gone since; the names are placeholders:

type request struct {
	// the other fields

	limits *timeLimits
}

// timeLimits are one request's two time limits: SWW_RPROXY_MAX_REQUEST_TIME_MS
// and SWW_RPROXY_MAX_RESPONSE_TIME_MS.
type timeLimits struct {
	maxRequestTime  time.Duration
	maxResponseTime time.Duration
	start           time.Time
	// refuse is called with 408 or 504 when a limit passes.
	refuse func(status int)

	mu            sync.Mutex
	stopped       bool
	requestTimer  *time.Timer
	responseTimer *time.Timer
	requestEnd    time.Time
}
  • The catch the review names: the limits are tied to the request in two more places. requestTimedOut reads whether the body is waiting for the visitor, and sets a read deadline on the visitor's connection while it holds the lock. The new type is given both.

What stays

  • Which status a visitor gets, and when: every case in limits_test.go.
  • The functions that end the reading of the visitor's body (stopReadingBody, readRestOfBody, stopPassingOnBody) and setResponseDeadline stay on request: they work on the connection, not on the timers.

Done when

  • the mutex and everything it guards are in one type in one file, and request has no timer field of its own;
  • at least one test of that type alone uses testing/synctest and no real wait;
  • behaviour is unchanged: every existing test passes with no assertion changed;
  • make check passes;
  • the PR body lists the new type's name, its file's and its methods'. Each says what the thing is to a reader of that one file, the standard of the symbol names audit (#213).

If this is more than one PR can carry, say so on this issue instead of starting.

Model: fable-5-2

**Start once the issue that narrows the body wrappers is closed: https://git.eeqj.de/sneak/smallwebwaf/issues/431.** Both edit the same lines of `internal/rproxy/bodies.go` and `request.go`. From the architecture review, "Inside `rproxy`", under "The time limits have no type of their own": https://git.eeqj.de/sneak/smallwebwaf/issues/283#issuecomment-173849. Approved for building: https://git.eeqj.de/sneak/smallwebwaf/issues/283#issuecomment-183550. The code was read at `809d266` on `next`. Line numbers are that commit's. ### What is there - `request` in `internal/rproxy/request.go` ends with a mutex and the four fields it guards: `timersStopped`, `requestTimer`, `responseTimer`, `requestEnd` (lines 93 to 99). - Ten functions in the last third of the file work on them: `requestDeadline`, `startRequestTimer`, `requestTimedOut`, `wroteRequest`, `startResponseTimer`, `responseTimedOut`, `responseReceived`, `endRequest`, `stopTimers`, `stopTimer` (lines 509 to 717, among the functions that read the body). - They are called from four kinds of goroutine: the handler's own, the transport's trace hook, the two timers, and the read of the app's response body. - They read the clock directly, with `time.Now`, `time.Until` and `time.AfterFunc`. ### Why it matters - This is the hardest code in `rproxy`: which side was being waited on when a limit passed decides between a 408 and a 504. It is methods on the same type as the path rewrite and the log line, so the type does not show which fields other goroutines touch. - It is tested only through whole requests over real connections with real waits. `internal/rproxy/limits_test.go` sleeps for 2.1 seconds in one test. ### What changes - The two time limits become a type of their own, in a file of their own in `internal/rproxy`. It owns the mutex, the two timers and the end of the request. `request` holds one field of it. - The timers are then tested alone, with `testing/synctest`, which the tests of many other packages here use and `rproxy`'s do not. The sketch from the review, less a field that is gone since; the names are placeholders: ```go type request struct { // the other fields limits *timeLimits } // timeLimits are one request's two time limits: SWW_RPROXY_MAX_REQUEST_TIME_MS // and SWW_RPROXY_MAX_RESPONSE_TIME_MS. type timeLimits struct { maxRequestTime time.Duration maxResponseTime time.Duration start time.Time // refuse is called with 408 or 504 when a limit passes. refuse func(status int) mu sync.Mutex stopped bool requestTimer *time.Timer responseTimer *time.Timer requestEnd time.Time } ``` - The catch the review names: the limits are tied to the request in two more places. `requestTimedOut` reads whether the body is waiting for the visitor, and sets a read deadline on the visitor's connection while it holds the lock. The new type is given both. ### What stays - Which status a visitor gets, and when: every case in `limits_test.go`. - The functions that end the reading of the visitor's body (`stopReadingBody`, `readRestOfBody`, `stopPassingOnBody`) and `setResponseDeadline` stay on `request`: they work on the connection, not on the timers. ### Done when - the mutex and everything it guards are in one type in one file, and `request` has no timer field of its own; - at least one test of that type alone uses `testing/synctest` and no real wait; - behaviour is unchanged: every existing test passes with no assertion changed; - `make check` passes; - the PR body lists the new type's name, its file's and its methods'. Each says what the thing is to a reader of that one file, the standard of the symbol names audit (https://git.eeqj.de/sneak/smallwebwaf/issues/213). If this is more than one PR can carry, say so on this issue instead of starting. Model: fable-5-2
clawbot self-assigned this 2026-11-19 17:12:41 +01:00
clawbot added the needs-decision label 2026-11-19 17:23:57 +01:00
Author
Collaborator

Not started: this issue waits for the body-wrapper change (#431), which is still open with no PR; both edit the same lines of internal/rproxy/bodies.go and request.go.

Model: opus-5-5

Not started: this issue waits for the body-wrapper change (https://git.eeqj.de/sneak/smallwebwaf/issues/431), which is still open with no PR; both edit the same lines of `internal/rproxy/bodies.go` and `request.go`. Model: opus-5-5
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/smallwebwaf#432