Fix sentinel error aliasing, hot-path allocations, and resource leaks
- Deduplicate sentinel errors: httpx.ErrNoHealthy, ErrCircuitOpen, and ErrRetryExhausted are now aliases to the canonical sub-package values so errors.Is works across package boundaries - Retry transport returns ErrRetryExhausted only when all attempts are actually exhausted, not on early policy exit - Balancer: pre-parse endpoint URLs at construction, replace req.Clone with cheap shallow struct copy to avoid per-request allocations - Circuit breaker: Load before LoadOrStore to avoid allocating a Breaker on every request for known hosts - Health checker: drain response body before close for connection reuse, probe endpoints concurrently, run initial probe synchronously in Start - Client: add Close() to shut down health checker goroutine, propagate URL resolution errors instead of silently discarding them - MockClock: fix lock ordering in Reset (clock.mu before t.mu), fix timer slice compaction to avoid backing-array aliasing, extract fireExpired to deduplicate Advance/Set
This commit is contained in:
42
client.go
42
client.go
@@ -2,6 +2,7 @@ package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -15,9 +16,10 @@ import (
|
||||
// Client is a high-level HTTP client that composes middleware for retry,
|
||||
// circuit breaking, load balancing, logging, and more.
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
errorMapper ErrorMapper
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
errorMapper ErrorMapper
|
||||
balancerCloser *balancer.Closer
|
||||
}
|
||||
|
||||
// New creates a new Client with the given options.
|
||||
@@ -37,8 +39,11 @@ func New(opts ...Option) *Client {
|
||||
var chain []middleware.Middleware
|
||||
|
||||
// Balancer (innermost, wraps base transport).
|
||||
var balancerCloser *balancer.Closer
|
||||
if len(o.endpoints) > 0 {
|
||||
chain = append(chain, balancer.Transport(o.endpoints, o.balancerOpts...))
|
||||
var mw middleware.Middleware
|
||||
mw, balancerCloser = balancer.Transport(o.endpoints, o.balancerOpts...)
|
||||
chain = append(chain, mw)
|
||||
}
|
||||
|
||||
// Circuit breaker wraps balancer.
|
||||
@@ -72,15 +77,18 @@ func New(opts ...Option) *Client {
|
||||
Transport: rt,
|
||||
Timeout: o.timeout,
|
||||
},
|
||||
baseURL: o.baseURL,
|
||||
errorMapper: o.errorMapper,
|
||||
baseURL: o.baseURL,
|
||||
errorMapper: o.errorMapper,
|
||||
balancerCloser: balancerCloser,
|
||||
}
|
||||
}
|
||||
|
||||
// Do executes an HTTP request.
|
||||
func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) {
|
||||
req = req.WithContext(ctx)
|
||||
c.resolveURL(req)
|
||||
if err := c.resolveURL(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -143,14 +151,23 @@ func (c *Client) Delete(ctx context.Context, url string) (*Response, error) {
|
||||
return c.Do(ctx, req)
|
||||
}
|
||||
|
||||
// Close releases resources associated with the Client, such as background
|
||||
// health checker goroutines. It is safe to call multiple times.
|
||||
func (c *Client) Close() {
|
||||
if c.balancerCloser != nil {
|
||||
c.balancerCloser.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// HTTPClient returns the underlying *http.Client for advanced use cases.
|
||||
// Mutating the returned client may bypass the configured middleware chain.
|
||||
func (c *Client) HTTPClient() *http.Client {
|
||||
return c.httpClient
|
||||
}
|
||||
|
||||
func (c *Client) resolveURL(req *http.Request) {
|
||||
func (c *Client) resolveURL(req *http.Request) error {
|
||||
if c.baseURL == "" {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
// Only resolve relative URLs (no scheme).
|
||||
if req.URL.Scheme == "" && req.URL.Host == "" {
|
||||
@@ -159,6 +176,11 @@ func (c *Client) resolveURL(req *http.Request) {
|
||||
path = "/" + path
|
||||
}
|
||||
base := strings.TrimRight(c.baseURL, "/")
|
||||
req.URL, _ = req.URL.Parse(base + path)
|
||||
u, err := req.URL.Parse(base + path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("httpx: resolving URL %q with base %q: %w", path, c.baseURL, err)
|
||||
}
|
||||
req.URL = u
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user