curlew

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2020 License: MIT Imports: 8 Imported by: 6

README

Curlew

Curlew is a job pool based on a local machine.

Feature

  • Automatically scale the number of workers up or down.
  • Maximum job execution time.
  • Don't ensure the job execution order.

Usage

package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/xiaojiaoyu100/curlew"
)

func monitor(err error) {
	fmt.Println(err)
}

func main() {
	d, err := curlew.New(curlew.WithMonitor(monitor))
	if err != nil {
		fmt.Println(err)
		return
	}
	j := curlew.NewJob()
	j.Arg = 3
	j.Fn = func(ctx context.Context, arg interface{}) error {
		fmt.Println("I'm done")
		return nil
	}
	d.Submit(j)
	select {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	MaxWorkerNum         int           // maximum  worker num in the pool
	MaxJobRunningTimeout time.Duration // job execution timeout
	WorkerPool           chan *Worker  // worker pool
	// contains filtered or unexported fields
}

Dispatcher takes the responsibility of dispatching jobs to available workers.

func New

func New(setters ...Setter) (*Dispatcher, error)

New creates a dispatcher instance.

func (*Dispatcher) EnableDebug added in v0.2.1

func (d *Dispatcher) EnableDebug()

EnableDebug enables debug info.

func (*Dispatcher) NewWorker added in v0.2.3

func (d *Dispatcher) NewWorker() *Worker

NewWorker 生成一个工作协程

func (*Dispatcher) RunningWorkerNum

func (d *Dispatcher) RunningWorkerNum() int

RunningWorkerNum returns the current running worker num.

func (*Dispatcher) Submit added in v0.2.0

func (d *Dispatcher) Submit(j *Job)

Submit submits a job.

type Handler added in v0.1.0

type Handler func(ctx context.Context, arg interface{}) error

Handler provides the job function signature.

type Job

type Job struct {
	Fn  Handler
	Arg interface{}
}

Job defines a job.

func NewJob

func NewJob() *Job

NewJob creates a job instance.

type Monitor

type Monitor func(err error)

Monitor 监控

type Setter

type Setter func(d *Dispatcher) error

Setter configures a Dispatcher.

func WithMaxWorkerNum

func WithMaxWorkerNum(num int) Setter

WithMaxWorkerNum configures maximum number of workers in the pool.

func WithMonitor

func WithMonitor(monitor Monitor) Setter

WithMonitor configures a monitor.

type Worker

type Worker struct {
	Jobs chan *Job
	// contains filtered or unexported fields
}

Worker 工作协程

Jump to

Keyboard shortcuts

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