dutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServeHTTPSWithContext

func ListenAndServeHTTPSWithContext(ctx context.Context, server *http.Server, certFile, keyFile string) error

ListenAndServeHTTPSWithContext runs server.ListenAndServeTLS() on an http.Server, but properly calls server.Shutdown when the Context is canceled.

It obeys hard/soft cancellation as implemented by dcontext.WithSoftness; it calls server.Shutdown() when the soft Context is canceled, and the hard Context being canceled causes the .Shutdown() to hurry along and kill any live requests and return, instead of waiting for them to be completed gracefully.

func ListenAndServeHTTPWithContext

func ListenAndServeHTTPWithContext(ctx context.Context, server *http.Server) error

ListenAndServeHTTPWithContext runs server.ListenAndServe() on an http.Server, but properly calls server.Shutdown when the Context is canceled.

It obeys hard/soft cancellation as implemented by dcontext.WithSoftness; it calls server.Shutdown() when the soft Context is canceled, and the hard Context being canceled causes the .Shutdown() to hurry along and kill any live requests and return, instead of waiting for them to be completed gracefully.

func PanicToError

func PanicToError(rec interface{}) error

PanicToError takes an arbitrary object returned from recover(), and returns an appropriate error.

If the input is nil, then nil is returned.

If the input is an error returned from a previus call to PanicToError(), then it is returned verbatim.

If the input is an error, it is wrapped with the message "PANIC:" and has a stack trace attached to it.

If the input is anything else, it is formatted with "%+v" and returned as an error with a stack trace attached.

func ServeHTTPSWithContext

func ServeHTTPSWithContext(ctx context.Context, server *http.Server, ln net.Listener, certFile, keyFile string) error

ServeHTTPSWithContext runs server.ServeTLS() on an http.Server, but properly calls server.Shutdown when the Context is canceled.

It obeys hard/soft cancellation as implemented by dcontext.WithSoftness; it calls server.Shutdown() when the soft Context is canceled, and the hard Context being canceled causes the .Shutdown() to hurry along and kill any live requests and return, instead of waiting for them to be completed gracefully.

func ServeHTTPWithContext

func ServeHTTPWithContext(ctx context.Context, server *http.Server, listener net.Listener) error

ServeHTTPWithContext(ln) runs server.Serve(ln) on an http.Server, but properly calls server.Shutdown when the Context is canceled.

It obeys hard/soft cancellation as implemented by dcontext.WithSoftness; it calls server.Shutdown() when the soft Context is canceled, and the hard Context being canceled causes the .Shutdown() to hurry along and kill any live requests and return, instead of waiting for them to be completed gracefully.

func SleepWithContext

func SleepWithContext(ctx context.Context, d time.Duration)

Sleep pauses the current goroutine for at least the duration d, or until the Context is done, whichever happens first.

You may be thinking, why not just do:

select {
case <-ctx.Done():
case <-time.After(d):
}

well, time.After can't get garbage collected until the timer expires, even if the Context is done. What this function provides is properly stopping the timer so that it can be garbage collected sooner.

https://medium.com/@oboturov/golang-time-after-is-not-garbage-collected-4cbc94740082

Types

This section is empty.

Jump to

Keyboard shortcuts

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