timer

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deadline

func Deadline(correction float32, threshold time.Duration) func(time.Time, bool) time.Time

Deadline return a time corrector.

It useful for go.octolab.org/toolkit/protocol/http/header.Deadline and go.octolab.org/toolkit/protocol/http/middleware.Deadline.

mux := http.NewServeMux()

sla := middleware.Deadline(time.Second, timer.Deadline(0.95, time.Millisecond))
handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
mux.Handle("/", sla(handler))

log.Fatal(http.ListenAndServe(":80", mux))

It also useful for the built-in context package.

func(rw http.ResponseWriter, req *http.Request) {
	ctx := req.Context()

	corrector := timer.Deadline(0.95, time.Millisecond)
	ctx, cancel := context.WithDeadline(req.Context(), corrector(ctx.Deadline()))
	defer cancel()

	select {
	case <-ctx.Done():
		message := http.StatusText(http.StatusRequestTimeout)
		http.Error(rw, message, http.StatusRequestTimeout)
	case result := <-rpc.Call(ctx):
		unsafe.Ignore(json.NewEncoder(rw).Encode(result))
	}
}

func Stopwatch added in v0.0.14

func Stopwatch(fn func()) time.Duration

Stopwatch calculates the fn execution time.

var result interface{}

duration := fn.Stopwatch(func() { result = do.some("heavy") })

func Timeout

func Timeout(correction float32, threshold time.Duration) func(time.Duration, bool) time.Duration

Timeout return a duration corrector.

It useful for go.octolab.org/toolkit/protocol/http/header.Timeout and go.octolab.org/toolkit/protocol/http/middleware.Timeout.

mux := http.NewServeMux()

sla := middleware.Timeout(time.Second, timer.Timeout(0.95, time.Millisecond))
handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
mux.Handle("/", sla(handler))

log.Fatal(http.ListenAndServe(":80", mux))

Types

This section is empty.

Jump to

Keyboard shortcuts

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