gortinep

package module
v0.0.0-...-f6ce3eb Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2019 License: MIT Imports: 7 Imported by: 0

README

gortinep

gortinep is thinnest goroutine pool library for go application.

Requirement

Go (>= 1.11)

Install

go get github.com/hlts2/gortinep

Example

Basic Example
package main

import (
	"context"
	"fmt"

	"github.com/hlts2/gortinep"
	"github.com/hlts2/gortinep/middlewares"
	"github.com/hlts2/gortinep/middlewares/logger/zap"
	"github.com/hlts2/gortinep/middlewares/recovery"
	"go.uber.org/zap"
)

func main() {
	z := zap.NewExample()

	g := gortinep.New(
		gortinep.WithErrorChannel(make(chan error, 1)),
		gortinep.WithWorkerSize(256),
		gortinep.WithInterceptor(
			middlewares.ChainInterceptors(
				gortinep_recovery.Interceptor(
					func(p interface{}) {
						z.Info("recovery from panic")
					},
				),
				gortinep_zap.Interceptor(
					z,
				),
			),
		),
	).Start(context.Background())
	defer g.Stop()

	const jobSize = 100000

	for i := 0; i < jobSize; i++ {
		// Register job.
		g.Add(func(context.Context) error {
			z.Info("finish job")
			return nil
		})
	}

	// Get error of job. If execution of all jobs is completed, exit Wait function.
	for err := range g.Wait() {
		if err != nil {
			fmt.Println(err)
		}
	}

	// Register job again.
	for i := 0; i < jobSize; i++ {
		g.Add(func(context.Context) error {
			z.Info("finish job")
			return nil
		})
	}

	for err := range g.Wait() {
		if err != nil {
			fmt.Println(err)
		}
	}
}
Example with no error channel option
func main() {
	z := zap.NewExample()

	g := gortinep.New(
		gortinep.WithWorkerSize(256),
		gortinep.WithInterceptor(
			middlewares.ChainInterceptors(
				gortinep_recovery.Interceptor(
					func(p interface{}) {
						z.Info("recovery from panic")
					},
				),
				gortinep_zap.Interceptor(
					z,
				),
			),
		),
	).Start(context.Background())
	defer g.Stop()

	const jobSize = 100000

	for i := 0; i < jobSize; i++ {
		// Register job.
		g.Add(func(context.Context) error {
			z.Info("finish job")
			return nil
		})
	}

        // Execution of all jobs is completed, exit Wait function.
        g.Wait()
}

TODO

  • Test Code 🙏

Author

hlts2

LICENSE

gortinep released under MIT license, refer LICENSE file.

Documentation

Index

Constants

View Source
const (
	// DefaultWorkerSize is default worker size.
	DefaultWorkerSize = 128

	// DefaultJobSize is default job size.
	DefaultJobSize = 100000000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Gortinep

type Gortinep interface {
	Add(Job)
	Start(context.Context) Gortinep
	Stop() Gortinep
	Wait() chan error
}

Gortinep is base gortinep interface.

func New

func New(opts ...Option) Gortinep

New creates Gortinep(*gortinep) instance.

type Interceptor

type Interceptor func(context.Context, Job) error

Interceptor provides a hook to intercept the execution of Job.

type Job

type Job func(context.Context) error

Job defines the handler of job for goroutine pool.

type Option

type Option func(*gortinep)

Option configures gortinep.

func WithErrorChannel

func WithErrorChannel(ch chan error) Option

WithErrorChannel returns an option that sets channel for job error processed by goroutine worker. The result of each goroutine is sent to this channe.

func WithInterceptor

func WithInterceptor(interceptor Interceptor) Option

WithInterceptor returns an option that sets the Interceptor implementation.

func WithJobSize

func WithJobSize(size int) Option

WithJobSize returns an option that sets the job size.

func WithWorkerSize

func WithWorkerSize(size int) Option

WithWorkerSize returns an option that sets the worker size.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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