/go-wait

Useful package for the polling of conditions in intervals, with timeouts, cancelation, and jittering. Additionally provides a throttle for event processing.

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Tideland Go Wait

GitHub release GitHub license Go Module GoDoc Workflow Go Report Card

Description

Tideland Go Wait provides provides a flexible and controlled waiting for wanted conditions by polling. The function for testing has to be defined, different tickers for the polling can be generated for

  • simple constant intervals,
  • a maximum number of constant intervals,
  • a constant number of intervals with a deadline,
  • a onstant number of intervals with a timeout, and
  • jittering intervals.

Own tickers, e.g. with changing intervals, can be implemented too.

Another component of the package is the throttle, others would call it limiter. It allows the limited processing of events per second. Events are closures or functions with a defined signature. Depending on the burst size of the throttle multiple events can be processed with one call.

I hope you like it. ;)

Examples

Polling

A simple check for an existing file by polling every second for maximal 30 seconds.

// Tick every second for maximal 30 seconds.
ticker := wait.MakeExpiringIntervalTicker(time.Second, 30*time.Second),

// Check for existence of a file.
contition := func() (bool, error) {
    _, err := os.Stat("myfile.txt")
    if err != nil {
        if os.IsNotExist(err) {
            return false, nil
        }
        return false, err
    }
    // Found file.
    return true, nil
}

// And now poll.
wait.Poll(ctx, ticker, condition)

Throttling

A throttled wrapper of a http.Handler.

type ThrottledHandler struct {
    throttle *wait.Throttle
    handler  http.Handler
}

func NewThrottledHandler(limit wait.Limit, handler http.Handler) http.Handler {
    return &ThrottledHandler{
        throttle: wait.NewThrottle(limit, 1),
        handler:  handler,
    }
}

func (h *ThrottledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    evt := func() error {
        h.handler.ServeHTTP(w, r)
        return nil
    }
    h.throttle.Process(context.Background(), evt)
}

Contributors