Skip to content

🔥 feat: narrow client hook lock scope to avoid serializing concurrent requests #4363

@pageton

Description

@pageton

Problem

The Fiber client (client/core.go:156-193) holds a write lock (c.client.mu.Lock()) while executing pre-hooks and post-hooks. This means:

  1. Hook functions execute while holding the write lock.
  2. Concurrent requests through the same client serialize on hook execution.
  3. User hooks that perform I/O (logging, metrics) block all other requests.

Proposed fix

Snapshot the hook slices under the lock, then execute without it:

func (c *Core) preHooks(req *Request) {
    c.client.mu.RLock()
    hooks := c.client.preHooks // slice header copy
    c.client.mu.RUnlock()
    
    for _, hook := range hooks {
        hook(req)
    }
}

Use RLock for reads (hook execution) and reserve Lock only for mutation (adding/removing hooks).

Priority

P1 — Matters for high-throughput client usage with concurrent requests.


Identified during a full performance architecture review of the Fiber codebase.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions