network

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package network provides centralized HTTP client management with proxy support. It allows runtime proxy configuration updates that propagate to all HTTP clients.

Index

Constants

This section is empty.

Variables

View Source
var DefaultClientConfig = struct {
	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	MaxIdleConnDuration time.Duration
	MaxConnDuration     time.Duration
	MaxConnsPerHost     int
}{
	ReadTimeout:         60 * time.Second,
	WriteTimeout:        60 * time.Second,
	MaxIdleConnDuration: 30 * time.Second,
	MaxConnDuration:     300 * time.Second,
	MaxConnsPerHost:     200,
}

DefaultClientConfig holds default timeout values for HTTP clients

Functions

func ParseMultipartFormFields

func ParseMultipartFormFields(contentType string, body []byte) (map[string]any, error)

ParseMultipartFormFields extracts text form fields from a multipart/form-data body, skipping file parts to avoid loading binary data into memory.

func ReconstructMultipartBody

func ReconstructMultipartBody(origContentType string, origBody []byte, payload map[string]any) ([]byte, string, error)

ReconstructMultipartBody rebuilds a multipart/form-data body from the original, replacing text field values with those from payload (e.g. updated "model") and copying file parts byte-for-byte.

func SerializePayloadToRequest

func SerializePayloadToRequest(req *schemas.HTTPRequest, payload map[string]any, isMultipart bool, origContentType string) error

SerializePayloadToRequest writes the modified payload back to req.Body, using multipart reconstruction for multipart/form-data or JSON for everything else.

func StaleConnectionRetryIfErr

func StaleConnectionRetryIfErr(_ *fasthttp.Request, attempts int, err error) (resetTimeout bool, retry bool)

StaleConnectionRetryIfErr is a RetryIfErr callback that retries requests when the failure is due to a stale/dead connection being reused from the pool. This addresses intermittent "cannot find whitespace in the first line of response" errors caused by connection reuse with leftover chunked transfer encoding data (see: https://github.com/valyala/fasthttp/issues/1743).

By default fasthttp only retries idempotent requests (GET/HEAD/PUT). LLM inference requests use POST, so without this they fail immediately on stale connections. Retrying is safe here because the error occurs during response header parsing — before the server processes the new request, or on a connection the server has already closed.

func WriteMultipartField

func WriteMultipartField(writer *multipart.Writer, name string, val any) error

WriteMultipartField writes a single form field to the multipart writer, handling string, []string, and other value types.

Types

type ClientPurpose

type ClientPurpose string

ClientPurpose defines the intended use of an HTTP client for proxy filtering

const (
	// ClientPurposeSCIM is used for SCIM/OAuth provider requests
	ClientPurposeSCIM ClientPurpose = "scim"
	// ClientPurposeInference is used for LLM inference requests
	ClientPurposeInference ClientPurpose = "inference"
	// ClientPurposeAPI is used for general API requests (guardrails, etc.)
	ClientPurposeAPI ClientPurpose = "api"
)

type GlobalProxyConfig

type GlobalProxyConfig struct {
	Enabled       bool            `json:"enabled"`
	Type          GlobalProxyType `json:"type"`                      // "http", "socks5", "tcp"
	URL           string          `json:"url"`                       // Proxy URL (e.g., http://proxy.example.com:8080)
	Username      string          `json:"username,omitempty"`        // Optional authentication username
	Password      string          `json:"password,omitempty"`        // Optional authentication password
	NoProxy       string          `json:"no_proxy,omitempty"`        // Comma-separated list of hosts to bypass proxy
	Timeout       int             `json:"timeout,omitempty"`         // Connection timeout in seconds
	SkipTLSVerify bool            `json:"skip_tls_verify,omitempty"` // Skip TLS certificate verification
	// Entity enablement flags
	EnableForSCIM      bool `json:"enable_for_scim"`      // Enable proxy for SCIM requests (enterprise only)
	EnableForInference bool `json:"enable_for_inference"` // Enable proxy for inference requests
	EnableForAPI       bool `json:"enable_for_api"`       // Enable proxy for API requests
}

GlobalProxyConfig represents the global proxy configuration

type GlobalProxyType

type GlobalProxyType string

GlobalProxyType represents the type of global proxy

const (
	GlobalProxyTypeHTTP   GlobalProxyType = "http"
	GlobalProxyTypeSOCKS5 GlobalProxyType = "socks5"
	GlobalProxyTypeTCP    GlobalProxyType = "tcp"
)

type HTTPClientFactory

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

HTTPClientFactory manages HTTP clients with centralized proxy configuration. It supports both fasthttp and standard net/http clients with purpose-based proxy enablement (SCIM, Inference, API).

func NewHTTPClientFactory

func NewHTTPClientFactory(proxyConfig *GlobalProxyConfig, logger schemas.Logger) *HTTPClientFactory

NewHTTPClientFactory creates a new HTTP client factory with the given proxy configuration. Pass nil for proxyConfig if proxy is not yet configured.

func (*HTTPClientFactory) GetFasthttpClient

func (f *HTTPClientFactory) GetFasthttpClient(purpose ClientPurpose) *fasthttp.Client

GetFasthttpClient returns a fasthttp client configured for the given purpose. If proxy is enabled for this purpose, the client will be configured with proxy settings. Clients are cached and reused until proxy config changes.

func (*HTTPClientFactory) GetHTTPClient

func (f *HTTPClientFactory) GetHTTPClient(purpose ClientPurpose) *http.Client

GetHTTPClient returns a standard net/http client configured for the given purpose. If proxy is enabled for this purpose, the client will be configured with proxy settings. Clients are cached and reused until proxy config changes.

func (*HTTPClientFactory) GetProxyConfig

func (f *HTTPClientFactory) GetProxyConfig() *GlobalProxyConfig

GetProxyConfig returns the current proxy configuration (thread-safe read)

func (*HTTPClientFactory) UpdateProxyConfig

func (f *HTTPClientFactory) UpdateProxyConfig(config *GlobalProxyConfig)

UpdateProxyConfig updates the proxy configuration and recreates all cached clients. This is thread-safe and can be called at runtime.

Jump to

Keyboard shortcuts

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