Home / Function/ New() — fiber Function Reference

New() — fiber Function Reference

Architecture documentation for the New() function in timeout.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  9bba4d56_edea_19ff_cd90_d760229c6404["New()"]
  d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5["timeout.go"]
  9bba4d56_edea_19ff_cd90_d760229c6404 -->|defined in| d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5
  100b2639_93ee_18c4_b439_6a12e4b32ee1["handleResult()"]
  9bba4d56_edea_19ff_cd90_d760229c6404 -->|calls| 100b2639_93ee_18c4_b439_6a12e4b32ee1
  4b8d9c60_fb0a_7187_d55e_7c36f415f74e["handleTimeout()"]
  9bba4d56_edea_19ff_cd90_d760229c6404 -->|calls| 4b8d9c60_fb0a_7187_d55e_7c36f415f74e
  style 9bba4d56_edea_19ff_cd90_d760229c6404 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/timeout/timeout.go lines 21–84

func New(h fiber.Handler, config ...Config) fiber.Handler {
	cfg := configDefault(config...)

	return func(ctx fiber.Ctx) error {
		if cfg.Next != nil && cfg.Next(ctx) {
			return h(ctx)
		}

		timeout := cfg.Timeout
		if timeout <= 0 {
			return h(ctx)
		}

		// Create timeout context - handler can check c.Context().Done()
		parent := ctx.Context()
		tCtx, cancel := context.WithTimeout(parent, timeout)
		ctx.SetContext(tCtx)

		// Channels for handler result and panics
		done := make(chan error, 1)
		panicChan := make(chan any, 1)

		// Run handler in goroutine so we can race against the timeout
		go func() {
			defer func() {
				if p := recover(); p != nil {
					log.Errorw("panic recovered in timeout handler", "panic", p, "stack", string(debug.Stack()))
					select {
					case panicChan <- p:
					default:
						// Middleware already returned, panic value discarded
					}
				}
			}()
			err := h(ctx)
			select {
			case done <- err:
			default:
				// Middleware already returned, error discarded
			}
		}()

		// Wait for handler completion, panic, or timeout
		select {
		case err := <-done:
			// Handler finished normally - cleanup and return
			cancel()
			ctx.SetContext(parent)
			return handleResult(err, ctx, cfg)

		case <-panicChan:
			// Handler panicked - cleanup and return error
			cancel()
			ctx.SetContext(parent)
			return fiber.ErrInternalServerError

		case <-tCtx.Done():
			// Timeout occurred - abandon context and return immediately
			// The cleanup goroutine will cancel the timeout context once the handler finishes;
			// the abandoned fiber.Ctx stays out of the pool.
			return handleTimeout(parent, ctx, cancel, done, panicChan, cfg)
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does New() do?
New() is a function in the fiber codebase, defined in middleware/timeout/timeout.go.
Where is New() defined?
New() is defined in middleware/timeout/timeout.go at line 21.
What does New() call?
New() calls 2 function(s): handleResult, handleTimeout.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free