rpchttp

package
v1.21.40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rpchttp provides request-scoped HTTP server timing that both the RPC executor path and hand-written handlers can feed, emitted once as a Server-Timing response header by TimingMiddleware.

The package deliberately imports only the standard library (not its parent rpc package) so business logic can call AddTiming without pulling in rpc's cobra/openapi dependencies and without creating an import cycle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTiming

func AddTiming(ctx context.Context, name string, d time.Duration)

AddTiming adds d to the named phase of the current request's accumulator. It is a no-op when no accumulator is present (the CLI path has no HTTP response to attach a header to).

func TimingMiddleware

func TimingMiddleware(next http.Handler) http.Handler

TimingMiddleware installs a request-scoped Timings accumulator into the request context and stamps a Server-Timing response header. The header is written on the first WriteHeader/Write, or — for handlers that return without writing anything — once the handler returns. It carries a `total` metric plus any phase metrics business logic contributed via AddTiming/Track.

func Track

func Track(ctx context.Context, name string) func()

Track starts a timer for the named phase and returns a stop function that records the elapsed time when called. Use as `defer Track(ctx, "parse")()` or keep the returned func to stop explicitly inside a loop.

Types

type Timings

type Timings struct {
	// contains filtered or unexported fields
}

Timings is a request-scoped, insertion-ordered accumulator of named phase durations. Durations for the same name sum, so AddTiming may be called once per file in a loop and still report a single total for that phase.

func TimingsFromContext

func TimingsFromContext(ctx context.Context) (*Timings, bool)

TimingsFromContext returns the accumulator installed by WithTimings, if any.

func WithTimings

func WithTimings(ctx context.Context) (context.Context, *Timings)

WithTimings returns a context carrying a fresh accumulator and the accumulator itself, so a middleware can read it back after the handler returns.

func (*Timings) Header

func (t *Timings) Header() string

Header renders the accumulated phases as a Server-Timing value fragment (`find;dur=4.1, parse;dur=6.8`), durations in milliseconds to one decimal. It returns an empty string when no phases were recorded.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL