goper

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: MIT Imports: 5 Imported by: 0

README

goper


Goper is wrapped a goroutine and channal structure, it's safety to close goroutine and channal after handle all task, witch had putting into channal.


使用说明

  • frist, import goper into code: import github/min1324/goper

  • use Default to run.

  • use Deliver(arg), send arg to handle.

  • finally,Close() to release and exit goroutine.

API

type Handler func(interface{})
Close()
Default(maxGo int, hd Handler) error
Deliver(arg interface{}) error

使用示例

package main

import (
	"fmt"
	"github/min1324/goper"
)

func main() {
	var g goper.Goper
	g.Default(1, Router)
	defer g.Close()

	g.Deliver("hello world.")
}

func Router(i interface{}) {
	s, ok := i.(string)
	if ok {
		fmt.Println(s)
	}
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Function added in v1.1.0

type Function func()

Function a func type for group.

type Goper

type Goper struct {
	// contains filtered or unexported fields
}

Goper a simple half sync half async goroutine pool.

equire to go func(). Goper is safety to close all goroutine before done the task using Put() to channal.

func (*Goper) Close

func (g *Goper) Close()

Close() stop runner, wait for all work and goroutine finish.

func (*Goper) Default

func (g *Goper) Default(maxGo int, hd Handler) error

Default run maxGo num of goroutine. if maxGo<1, it will use numcpu.

func (*Goper) Deliver

func (g *Goper) Deliver(arg interface{}) error

Deliver send an arg to handler, if Goper not run, it return an error.

func (*Goper) Name

func (g *Goper) Name(name string)

Name register Goper name.

func (*Goper) Put added in v1.1.0

func (g *Goper) Put(arg interface{}) error

Put implicate Worker.

func (*Goper) String

func (g *Goper) String() string

String get Goper name.

type Handler

type Handler func(interface{})

Handler user handler fuction type.

type Pool added in v1.1.0

type Pool struct {
	// contains filtered or unexported fields
}

Pool goper pool

func (*Pool) Close added in v1.1.0

func (mgr *Pool) Close(name string)

Close stop name's worker.

func (*Pool) Default added in v1.1.0

func (p *Pool) Default(name string, maxgo int, hd Handler) error

Default Register a Worker run in default mode.

func (*Pool) Get added in v1.1.0

func (p *Pool) Get(name string) (t Worker, ok bool)

Get return name's Worker.

func (*Pool) Groud added in v1.1.0

func (p *Pool) Groud(name string, maxgo int) error

Groud Register a group. send to group's arg must be Function.

func (*Pool) GroupPut added in v1.1.0

func (mgr *Pool) GroupPut(name string, fn Function) error

GroupPut send i to name's group.

func (*Pool) Put added in v1.1.0

func (mgr *Pool) Put(name string, arg interface{}) error

Put send i to name's worker or group. if to group,arg must be Function type.

func (*Pool) Register added in v1.1.0

func (p *Pool) Register(name string, w Worker) error

Register put Worker into pool.

func (*Pool) Shutdown added in v1.1.0

func (mgr *Pool) Shutdown()

Shutdown stop all in pool.

type Worker added in v1.1.0

type Worker interface {
	Close()
	Put(arg interface{}) error
	String() string
}

Worker gopool interface.

func New added in v1.1.0

func New(hd Handler) Worker

New return an Goper using default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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