Documentation ¶
Overview ¶
Package policy defines functions that control which connections are allowed, and which ones are denied. For server-side imposed policies, Allow functions are used to filter connections. For client-side imposed policies, Timeout functions are used to filter connections.
When listening for incoming connections, it is impossible to control the conditions under which the remote client attempts to dial a new connection. As such, more powerful policy functions are required. The opposite is true for the dialer; when dialing a new connection to a remote peer, it can be assumed that the application-level logic will avoid dialing under conditions that the dialer does not find desirable. Here, the Timeout functions are the client-side analogy to the Allow functions. Although the Timeout functions are much less powerful, they are sufficient for dialers.
Policy functions (Allow functions and Timeout functions) are built in using a functional style, and are designed to be composed together. In this way, policy functions are able to define only a small and simple (and easily testable) amounts of filtering logic, but still be composed together to create more complex filtering logic.
// Create a policy that only allows 100 concurrent connections at any one // point. maxConns := policy.Max(100) // Create a policy that only allows 1 connection attempt per second per IP // address. rateLimit := policy.RateLimit(1.0, 1, 65535) // Compose these policies together to require that all of them pass. all := policy.All(maxConns, rateLimit) // Or, compose these policies together to require that any of them pass. any := policy.Any(maxConns, rateLimit)
Timeout functions are similarly composable.
// Create a policy to Timeout after 1 second. one := policy.ConstantTimeout(time.Second) // Create a policy to scale this constant timeout by 60% with every attempt. backoff := policy.LinearBackoff(1.6, one) // Create a policy to clamp the Timeout to an upper bound of one minute, no // matter how many attempts there have been. max := policy.MaxTimeout(time.Minute, backoff)
The policy functions available by default (of course, the programmer is free to implement their own policy functions) are quite simple. But, by providing "higher order policies" such as All, Any, or LinearBackoff, policies can be composed in more interesting ways. In practice, most of the policies that should be implemented at the raw connection level can be created by composing the available policies.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMaxConnectionsExceeded = errors.New("max connections exceeded")
ErrMaxConnectionsExceeded is returned when a connection is dropped because the maximum number of inbound/outbound connections has been reached.
var ErrRateLimited = errors.New("rate limited")
ErrRateLimited is returned when a connection is dropped because it has exceeded its rate limit for connection attempts.
Functions ¶
This section is empty.
Types ¶
type Allow ¶
Allow is a function that filters connections. If an error is returned, the connection is filtered and closed. Otherwise, it is maintained. A clean-up function is also returned. This function is called after the connection is closed, regardless of whether the closure was caused by filtering or normal control-flow.
func All ¶
All returns an Allow function that only passes a connection if all Allow functions in a set pass for that connection. Execution is lazy; when one of the Allow functions returns an error, no more Allow functions will be called.
func Any ¶
Any returns an Allow function that passes a connection if any Allow functions in a set pass for that connection. Execution is not lazy; even when one of the Allow functions returns a non-nil error, all other Allow functions will be called.
type Cleanup ¶
type Cleanup func()
Cleanup resource allocation, or reverse per-connection state mutations, done by an Allow function.
type Timeout ¶
Timeout functions accept an attempt (from 0 to the maximum integer) and return an expected duration for which the attempt should run.
func ConstantTimeout ¶
ConstantTimeout returns a Timeout function that always returns a constant duration.
func ExponentialBackoff ¶
ExponentialBackoff returns a Timeout function that scales the duration returned by another Timeout function exponentially with respect to the attempt.
func LinearBackoff ¶
LinearBackoff returns a Timeout function that scales the duration returned by another Timeout function linearly with respect to the attempt.