Documentation
¶
Index ¶
- func MemoryUsage() float64
- func Protect(limit float64) func(chi.Handler) chi.Handler
- func Selfdestruct(limit float64) func(chi.Handler) chi.Handler
- func SelfdestructFn(limit float64, fn func(ctx context.Context) bool) func(chi.Handler) chi.Handler
- func SetUpdateInteval(i time.Duration)
- func SignalSelfdestructGroup(ctx context.Context) bool
- func SignalSelfdestructProcess(ctx context.Context) bool
- type MemInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Protect ¶
Protect is a middleware that cancels incoming requests and returns a 503 HTTP code if system memory use exceeds the threshold set.
This middleware should be inserted fairly early in the middleware stack to ensure that request is cancelled early
Protect accepts a single parameter - a float64 defining a fraction of the system memory process can use before it starts cancelling requests
Right now it relies on memory usage data provided by /proc/meminfo so it is Linux specific. For convenience (dev envs) it will do nothing on other archs
func Selfdestruct ¶
Selfdestruct is a middleware that terminates current process if system memory use exceeds the threshold set.
Most common use case is automatic restart of leaky *cough*GCO*cough service all you need is this middleware and something that will start the process again after it's been terminated (docker --restart=always for example )
OOMSelfdestruct accepts one parameter - a float64 defining a fraction of the system memory that can be used before process self-terminates using system signal (SIG_TERM). As long as the app handles shutdown gracefully on SIG_TERM it should just work
Right now it relies on memory usage data provided by /proc/meminfo so it is Linux specific. For convenience (dev envs) it will do nothing on other archs
func SelfdestructFn ¶
SelfdestructFn is a version of Selfdestruct that accepts a custom termination function ( func(context.Context) bool ) that handles the process shutdown. Use it if your app requires extra steps to gracefully shut down.
func SetUpdateInteval ¶
SetUpdateInteval updates the interval between MemInfo updates. By default Updates happen on every request
func SignalSelfdestructGroup ¶
SignalSelfdestructGroup sends SIGTERM to whole process group
func SignalSelfdestructProcess ¶
SignalSelfdestructProcess sends SIGTERM to current process
Types ¶
type MemInfo ¶
MemInfo contains data feched from /proc/meminfo, mutex preventing races and update frequency related fields
func (*MemInfo) Available ¶
Available returns the amount of still RAM available It uses new process mem available estimation - factors in reclaiming file buffers and cache. On Linux kernels 3.14+ it tens to err on the side of caution On Linux kernels older than 3.14 it's a tiny bit too optimistic, unless you are using next to no file buffers