workticker

package
v6.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoWorkToRetrieve = errors.New("no work to retrieve")
)

Functions

This section is empty.

Types

type HandleWorkError

type HandleWorkError[T any] struct {
	ErrorMessage   string
	WorkerID       int
	Data           T
	WorkTickerName string
}

func NewHandleWorkError

func NewHandleWorkError[T any](errorMessage string, workerID int, data T) HandleWorkError[T]

func (HandleWorkError[T]) Error

func (hwe HandleWorkError[T]) Error() string

type Ticker

type Ticker[T any] interface {
	AddWorkConfiguration(config WorkConfiguration[T])
	Run(ctx context.Context)
}

type WorkConfiguration

type WorkConfiguration[T any] struct {
	Handler   WorkHandler[T]
	Retriever WorkRetriever[T]
}

WorkConfiguration is used to configure how work is retrieved and handled

type WorkHandler

type WorkHandler[T any] func(workerID int, data T, limiter ratelimit.Limiter) error

WorkHandler is a function that handles a single unit of work. It should do whatever processing is necessary. If an error is returned, that error is placed onto the error channel.

type WorkItem

type WorkItem[T any] struct {
	Data    T
	Handler WorkHandler[T]
}

WorkItem is a single unit of work.

type WorkRetriever

type WorkRetriever[T any] func(handler WorkHandler[T]) (WorkItem[T], error)

WorkRetriever is a function that returns a WorkItem and an error. It is used to retrieve work items on a tick.

type WorkTicker

type WorkTicker[T any] struct {
	// contains filtered or unexported fields
}

func NewWorkTicker

func NewWorkTicker[T any](config WorkTickerConfig[T]) *WorkTicker[T]

NewWorkTicker creates a new WorkTicker instance of type T. Example:

	workTicker := workticker.NewWorkTicker(workticker.WorkTickerConfig{
	  Logger: logrus.New().WithField("app", "example"),
	  NumWorkers: 10,
	  RateLimitPerSecond: 10,
	  TickFrequency: 5 * time.Second,
	  WorkErrorChan: errorChan,
    WorkConfiguration: workticker.WorkConfiguration[MyData]{
      Handler: handlerFunc,
      Retriever: retrieverFunc,
    },
	})

	ctx, cancel := context.WithCancel(context.Background())
	go workTicker.Run(ctx)

	// Wait for app to close or something...
	cancel()

func (*WorkTicker[T]) Run

func (wp *WorkTicker[T]) Run(ctx context.Context)

type WorkTickerConfig

type WorkTickerConfig[T any] struct {
	Name               string
	Logger             *logrus.Entry
	NumWorkers         int
	RateLimitPerSecond int
	TickFrequency      time.Duration
	WorkErrorChan      chan HandleWorkError[T]
	WorkConfiguration  WorkConfiguration[T]
}

Jump to

Keyboard shortcuts

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