keepalive

package module
v0.0.0-...-7f54623 Latest Latest
Warning

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

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

README

keepalive

Build Status Go Report Card Coverage Status Go Reference

keepalive is a lightweight goroutine manager that keeps a goroutine running and restarts it after errors.

Manager

A manager manages one or more workers. Workers can be registered with an identifier by calling the AddWorker method on a manager instance. Once registered, a worker can then be started and stopped using the StartWorker and StopWorker methods respectively.

If a worker encounters an error, it will be restarted after a delay that is increased exponentially.

Worker

Workers must implement the Worker interface, which requires implementing the Run and Stop methods. When Run is called on a worker, it must block until Stop is called on that worker. If an error is encountered by a worker during normal operation, it should be returned from the Run method.

Example

package main

import (
	"fmt"
	"time"

	"github.com/ronelliott/keepalive"
)

type myWorker struct {
	stopped bool
}

func (service *myWorker) Run() error {
	service.stopped = false
	for {
		if service.stopped {
			break
		}

		fmt.Println("cycle")
		time.Sleep(time.Second * 5)
	}

	return nil
}

func (service *myWorker) Stop() error {
	service.stopped = true
	return nil
}

func main() {
	manager := keepalive.NewManager()

	manager.AddWorker("worker1", &myWorker{})
	manager.AddWorker("worker2", &myWorker{})

	manager.StartWorker("worker1", "worker2")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorWorkerAlreadyExists is returned when a worker is added with a name that already exists.
	ErrorWorkerAlreadyExists = errors.New("worker already exists")

	// ErrorWorkerAlreadyRunning is returned when a worker is started that is already running.
	ErrorWorkerAlreadyRunning = errors.New("worker already running")

	// ErrorWorkerNotRunning is returned when a worker is stopped that is not running.
	ErrorWorkerNotRunning = errors.New("worker not running")

	// ErrorWorkerNotFound is returned when a worker is stopped that doesn't exist.
	ErrorWorkerNotFound = errors.New("worker not found")
)

Functions

func NewExponentialBackoff

func NewExponentialBackoff(start time.Duration, max time.Duration) func() time.Duration

NewExponentialBackoff returns a function that returns an exponentially increasing duration. The first call to the function will return the start duration. Each subsequent call will double the duration until the max duration is reached. The max duration will be returned for all subsequent calls to the function after the max duration is reached.

Types

type Manager

type Manager interface {
	// AddWorker registers a new worker with the given name. If a worker with the
	// given name already exists, ErrorWorkerAlreadyExists is returned. The worker
	// is not started until StartWorker is called.
	AddWorker(string, Worker) error

	// Names returns a slice of all registered worker names.
	Names() []string

	// StartWorker starts the worker with the given name. If the worker is not found,
	// ErrorWorkerNotFound is returned. If multiple workers are given, they are
	// started in the order given. If any worker fails to start, the error is logged
	// and returned and no further workers are started. If any workers were already
	// running prior to the error being encountered, they are not stopped. If the
	// worker is already running, it is not restarted.
	StartWorker(...string) error

	// StopWorker stops the worker with the given name and waits for it to stop. If
	// the worker is not found, ErrorWorkerNotFound is returned. If multiple workers
	// are given, they are stopped in the order given. If any worker fails to stop,
	// the error is logged and returned and no further workers are stopped, however
	// any workers already in the process of stopping will be waited for.
	StopWorker(...string) error

	// StopAllWorkers stops all registered workers. This is a convenience method that
	// calls StopWorker with all registered worker names. If any worker fails to stop,
	// the error is logged and iteration continues to the next worker.
	StopAllWorkers()
}

Manager is an interface for managing workers. It provides methods for adding workers, starting and stopping workers, and enumerating registered workers.

func NewManager

func NewManager() Manager

NewManager creates a new Manager instance with no registered workers.

type Worker

type Worker interface {
	// Run starts the worker. This method should block until the worker is stopped.
	Run() error

	// Stop stops the worker. This method should unblock the Run method.
	Stop() error
}

Worker is an interface for a worker. It provides methods for starting and stopping the worker.

Jump to

Keyboard shortcuts

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