Documentation
¶
Overview ¶
Package httputil pkg/httputil/compress.go c0-com-util
Package httputil pkg/httputil/dmsghttp.go c0-com-http
Package httputil pkg/httputil/error.go c0-com-http
Package httputil pkg/httputil/health.go c0-com-http
Package httputil pkg/httputil/httputil.go c0-com-http
Package httputil pkg/httputil/json_native.go c0-com-http
Native builds use jsoniter.ConfigFastest for the package-wide `json` codec. It relies on github.com/modern-go/reflect2, whose unsafe reflection (reflect.mapassign / unsafe_New / typedmemmove …) TinyGo's runtime does not provide, so the TinyGo build uses the standard library instead — see json_tinygo.go. Only NewEncoder/NewDecoder are used here, so the wire format is identical either way.
Package httputil pkg/httputil/limit.go c0-com-util
Package httputil pkg/httputil/log.go c0-com-http
Index ¶
- Constants
- Variables
- func BodyTooLarge(err error) bool
- func BoolFromQuery(r *http.Request, key string, defaultVal bool) (bool, error)
- func CompressMin(minBytes, level int) func(http.Handler) http.Handler
- func ErrorFromResp(resp *http.Response) error
- func GetLogger(r *http.Request) logrus.FieldLogger
- func LimitBody(n int64) func(http.Handler) http.Handler
- func LogEntrySetField(r *http.Request, key string, value interface{})
- func NewLogMiddleware(logger logrus.FieldLogger) func(http.Handler) http.Handler
- func ReadJSON(r *http.Request, v interface{}) error
- func SetLoggerMiddleware(log logrus.FieldLogger) func(next http.Handler) http.Handler
- func SplitRPCAddr(rpcAddr string) (host string, port uint16, err error)
- func WriteJSON(w http.ResponseWriter, r *http.Request, code int, v interface{})
- type DMSGHTTPConf
- type DMSGServersConf
- type DmsgServerHealth
- type Error
- type HTTPError
- type HealthCheckResponse
Constants ¶
const CompressMinBytes = 1024
CompressMinBytes is the default body size below which CompressMin leaves a response uncompressed. The deployment services answer most requests with a few hundred bytes (one entry, one binding, one health line); gzipping those costs a flate writer (~800 KB of state, pooled) and CPU for no wire saving — flate was 15% of dmsg-discovery's CPU and the pooled writers 110 MB of the address resolver's heap (2026-09-10).
const LoggerKey ctxKeyLogger = -1
LoggerKey defines logger HTTP context key.
Variables ¶
var RequestLogSlow = 2 * time.Second
RequestLogSlow is the latency above which a request that otherwise succeeded is still logged at Info. A var, not a const, so tests can lower it.
Functions ¶
func BodyTooLarge ¶ added in v1.3.100
BodyTooLarge reports whether err came from a body read that passed the LimitBody cap.
func BoolFromQuery ¶
BoolFromQuery obtains a boolean from a query entry.
func CompressMin ¶
CompressMin gzips responses larger than minBytes for clients that accept gzip, when the content type is text, JSON, JavaScript, XML or SVG. Smaller responses, other types, flushes before the threshold, and hijacked connections pass through untouched. level is a compress/gzip level.
func ErrorFromResp ¶
ErrorFromResp creates an HTTPError from a given server response.
func GetLogger ¶
func GetLogger(r *http.Request) logrus.FieldLogger
GetLogger returns the logger carried by the HTTP request context, or the package logger when the context has none.
The miss is the common case, not an edge case: SetLoggerMiddleware only puts a logger in the context when a request ID is present. This used to return logging.NewMasterLogger(), which built a whole logrus.Logger, TextFormatter and hook map per call, and whose entries then bypassed the process hooks and configured level because it was its own master. The package logger is created once and routes through the master logger, so hooks and level apply.
func LimitBody ¶ added in v1.3.100
LimitBody caps request bodies at n bytes. A declared Content-Length over n is answered 413 at once, and any other body fails its read past n.
func LogEntrySetField ¶
LogEntrySetField adds new key-value pair to current (request scoped) log entry. This pair will be printed along with all other pairs when the request is served. This requires log middleware from this package to be installed in the chain
func NewLogMiddleware ¶
NewLogMiddleware creates a new instance of logging middleware. This will allow adding log fields in the handler and any further middleware. At the end of request, this log entry will be printed at Info level via passed logger
func SetLoggerMiddleware ¶
SetLoggerMiddleware sets logger to context of HTTP requests.
func SplitRPCAddr ¶
SplitRPCAddr returns host and port and whatever error results from parsing the rpc address interface
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, r *http.Request, code int, v interface{})
WriteJSON writes a json object on a http.ResponseWriter with the given code. I/O errors (client disconnect, timeout) are logged but don't cause panics. Marshaling errors panic as they indicate a programming error.
Types ¶
type DMSGHTTPConf ¶
type DMSGHTTPConf struct {
DMSGServers []DMSGServersConf `json:"dmsg_servers"`
DMSGDiscovery string `json:"dmsg_discovery"`
TranspordDiscovery string `json:"transport_discovery"`
AddressResolver string `json:"address_resolver"`
RouteFinder string `json:"route_finder"`
UptimeTracker string `json:"uptime_tracker"`
ServiceDiscovery string `json:"service_discovery"`
}
DMSGHTTPConf is struct of /dmsghttp endpoint of config bootstrap
type DMSGServersConf ¶
type DMSGServersConf struct {
Static string `json:"static"`
Server struct {
Address string `json:"address"`
} `json:"server"`
}
DMSGServersConf is struct of dmsg servers list on /dmsghttp endpoint
type DmsgServerHealth ¶ added in v1.3.100
type DmsgServerHealth struct {
ClientSessions int `json:"client_sessions"`
PeerSessions int `json:"peer_sessions"`
ActiveStreams int64 `json:"active_streams"`
StreamsRelayed uint64 `json:"streams_relayed"`
BytesUp uint64 `json:"bytes_up"`
BytesDown uint64 `json:"bytes_down"`
}
DmsgServerHealth is a dmsg server's load: its connections, and the streams it relays and the bytes they carried since it started.
func DmsgServerHealthOf ¶ added in v1.3.100
func DmsgServerHealthOf(clients, peers int, active int64, streams, up, down uint64) *DmsgServerHealth
DmsgServerHealthOf converts a server's Stats for /health.
type Error ¶
type Error struct {
Error string `json:"error"`
}
Error is the object returned to the client when there's an error.
type HealthCheckResponse ¶
type HealthCheckResponse struct {
ServiceName string `json:"service_name,omitempty"`
BuildInfo *buildinfo.Info `json:"build_info,omitempty"`
StartedAt time.Time `json:"started_at"`
PublicKey string `json:"public_key,omitempty"`
DmsgAddr string `json:"dmsg_address,omitempty"`
DmsgDiscovery string `json:"dmsg_discovery,omitempty"`
DmsgServers []string `json:"dmsg_servers,omitempty"`
PeerServers []string `json:"peer_servers,omitempty"`
PublicAutoconnect bool `json:"public_autoconnect,omitempty"`
StcprCount int `json:"stcpr_count,omitempty"`
SudphCount int `json:"sudph_count,omitempty"`
// TransportCounts is a count of live transports keyed by their actual
// network type (stcpr, sudph, stcp, dmsg, squicr, swsr, swtr, webrtc, …).
// The key set is derived from the transports present, so every current and
// future transport type is represented — not just stcpr/sudph. The legacy
// StcprCount/SudphCount fields remain for backward compatibility.
TransportCounts map[string]int `json:"transport_counts,omitempty"`
NetworkTypes []string `json:"network_types,omitempty"`
DHTBootstrap bool `json:"dht_bootstrap,omitempty"`
// UDPAddr is an address resolver's public SUDPH address: visors that
// reach it over dmsg register SUDPH with it. Empty when not configured.
UDPAddr string `json:"udp_address,omitempty"`
// DmsgSessions are the visor's own sessions with dmsg servers.
DmsgSessions int `json:"dmsg_sessions,omitempty"`
// DmsgServer is what the dmsg server this process runs is carrying.
DmsgServer *DmsgServerHealth `json:"dmsg_server,omitempty"`
}
HealthCheckResponse is struct of /health endpoint
func GetServiceHealth ¶
func GetServiceHealth(ctx context.Context, url string) (health *HealthCheckResponse, err error)
GetServiceHealth gets the response from the given service url