runutil

package
v0.0.0-...-e2b8481 Latest Latest
Warning

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

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

Documentation

Overview

Initially copied from Thanos

Copyright (c) The Thanos Authors. Licensed under the Apache License 2.0.

Package runutil provides helpers to advanced function scheduling control like repeat or retry.

It's very often the case when you need to excutes some code every fixed intervals or have it retried automatically. To make it reliably with proper timeout, you need to carefully arrange some boilerplate for this. Below function does it for you.

For repeat executes, use Repeat:

err := runutil.Repeat(10*time.Second, stopc, func() error {
	// ...
})

Retry starts executing closure function f until no error is returned from f:

err := runutil.Retry(10*time.Second, stopc, func() error {
	// ...
})

For logging an error on each f error, use RetryWithLog:

err := runutil.RetryWithLog(logger, 10*time.Second, stopc, func() error {
	// ...
})

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Repeat

func Repeat(interval time.Duration, stopc <-chan struct{}, f func() error) error

Repeat executes f every interval seconds until stopc is closed or f returns an error. It executes f once right after being called.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/efficientgo/tools/pkg/runutil"
)

func main() {
	// It will stop Repeat 10 seconds later.
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	// It will print out "Repeat" every 5 seconds.
	err := runutil.Repeat(5*time.Second, ctx.Done(), func() error {
		fmt.Println("Repeat")
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}
Output:

func Retry

func Retry(interval time.Duration, stopc <-chan struct{}, f func() error) error

Retry executes f every interval seconds until timeout or no error is returned from f.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/efficientgo/tools/pkg/runutil"
	"github.com/pkg/errors"
)

func main() {
	// It will stop Retry 10 seconds later.
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	// It will print out "Retry" every 5 seconds.
	err := runutil.Retry(5*time.Second, ctx.Done(), func() error {
		fmt.Println("Retry")
		return errors.New("Try to retry")
	})
	if err != nil {
		log.Fatal(err)
	}
}
Output:

func RetryWithLog

func RetryWithLog(logger Logger, interval time.Duration, stopc <-chan struct{}, f func() error) error

RetryWithLog executes f every interval seconds until timeout or no error is returned from f. It logs an error on each f error.

Types

type Logger

type Logger interface {
	Log(keyvals ...interface{}) error
}

Logger interface compatible with go-kit/logger.

Jump to

Keyboard shortcuts

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