1、runner
runner 包用于展示如何使用通道来监视程序的执行时间,如果程序执行时间太长,也可以用runner 包来终止程序。
runner 包程序
// Example is provided with help by Gabriel Aszalos.
// Package runner manages the running and lifetime of a process.
package runner
import (
"errors"
"os"
"os/signal"
"time"
)
// Runner runs a set of tasks within a given timeout and can be
// shut down on an operating system interrupt.
type Runner struct {
// interrupt channel reports a signal from the
// operating system.
interrupt chan os.Signal
// complete channel reports that processing is done.
complete chan error
// timeout reports that time has run out.
timeout <-chan time.Time
// tasks holds a set of functions that are executed
// synchronously in index order.
tasks []func(int)
}
// ErrTimeout is returned when a value is received on the timeout channel.
var ErrTimeout = errors.New("received timeout")
// ErrInterrupt is returned when an event from the OS is received.
var ErrInterrupt = errors.New("received interrupt")
// New returns a new ready-to-use Runner.
func New(d time.Duration) *Runner {
return &Runner{
interrupt: make(chan os.Signal, 1),
complete: make(chan error),
timeout: time.After(d),
}
}
// Add attaches tasks to the Runner. A task is a function that
// takes an int ID.
func (r *Runner) Add(tasks ...func(int)) {
r.tasks = append(r.tasks, tasks...)
}
// Start runs all tasks and monitors channel events.
func (r *Runner) Start() error {
// We want to receive all interrupt based signals.
signal.Notify(r.interrupt, os.Interrupt)
// Run the different tasks on a different goroutine.
go func() {
r.complete <- r.run()
}()
select {
// Signaled when processing is done.
case err := <-r.complete:
return err
// Signaled when we run out of time.
case <-r.timeout:
return ErrTimeout
}
}
// run executes each registered task.
func (r *Runner) run() error {
for id, task := range r.tasks {
// Check for an interrupt signal from the OS.
if r.gotInterrupt() {
return ErrInterrupt
}
// Execute the registered task.
task(id)
}
return nil
}
// gotInterrupt verifies if the interrupt signal has been issued.
func (r *Runner) gotInterrupt() bool {
select {
// Signaled when an interrupt event is sent.
case <-r.interrupt:
// Stop receiving any further signals.
signal.Stop(r.interrupt)
return true
// Continue running as normal.
default:
return false
}
}
下面的程序将展示如何使用通道来监视程序运行的时间,以在程序运行时间过长时如何终止程序
package main
import (
"log"
"os"
"time"
"myExtends/runner"
)
const timeout = 3 * time.Second
func main() {
log.Println("starting work.")
r := runner.New(timeout)
r.Add(createTask(), createTask(), createTask())
if err := r.Start(); err != nil {
switch err {
case runner.ErrTimeout:
log.Println("Terminating due to timeout")
os.Exit(1)
case runner.ErrInterrupt:
log.Println("Termination due to interrupt")
os.Exit(2)
}
}
log.Println("Process end.")
}
func createTask() func(int) {
return func(id int) {
log.Printf("Processor -- Task #%d.",id)
time.Sleep(time.Duration(id) * time.Second)
}
}
2、pool
pool包使用有缓冲的通道来实现资源池,来管理可以在任意数量的 goroutine 之间共享及独立使用的资源。这种模式在需要共享一组静态资源的情况(如共享数据库连接或者内存缓冲区)下非常有用。如果goroutine 需要从池里得到这些资源中的一个,它可以从池里申请,使用完后归还到资源池里。
pool 包程序
// Example provided with help from Fatih Arslan and Gabriel Aszalos.
// Package pool manages a user defined set of resources.
package pool
import (
"errors"
"io"
"log"
"sync"
)
// Pool manages a set of resources that can be shared safely by
// multiple goroutines. The resource being managed must implement
// the io.Closer interface.
type Pool struct {
m sync.Mutex
resources chan io.Closer
factory func() (io.Closer, error)
closed bool
}
// ErrPoolClosed is returned when an Acquire returns on a
// closed pool.
var ErrPoolClosed = errors.New("Pool has been closed.")
// New creates a pool that manages resources. A pool requires a
// function that can allocate a new resource and the size of
// the pool.
func New(fn func() (io.Closer, error), size uint) (*Pool, error) {
if size <= 0 {
return nil, errors.New("Size value too small.")
}
return &Pool{
factory: fn,
resources: make(chan io.Closer, size),
}, nil
}
// Acquire retrieves a resource from the pool.
func (p *Pool) Acquire() (io.Closer, error) {
select {
// Check for a free resource.
case r, ok := <-p.resources:
log.Println("Acquire:", "Shared Resource")
if !ok {
return nil, ErrPoolClosed
}
return r, nil
// Provide a new resource since there are none available.
default:
log.Println("Acquire:", "New Resource")
return p.factory()
}
}
// Release places a new resource onto the pool.
func (p *Pool) Release(r io.Closer) {
// Secure this operation with the Close operation.
p.m.Lock()
defer p.m.Unlock()
// If the pool is closed, discard the resource.
if p.closed {
r.Close()
return
}
select {
// Attempt to place the new resource on the queue.
case p.resources <- r:
log.Println("Release:", "In Queue")
// If the queue is already at cap we close the resource.
default:
log.Println("Release:", "Closing")
r.Close()
}
}
// Close will shutdown the pool and close all existing resources.
func (p *Pool) Close() {
// Secure this operation with the Release operation.
p.m.Lock()
defer p.m.Unlock()
// If the pool is already close, don't do anything.
if p.closed {
return
}
// Set the pool as closed.
p.closed = true
// Close the channel before we drain the channel of its
// resources. If we don't do this, we will have a deadlock.
close(p.resources)
// Close the resources
for r := range p.resources {
r.Close()
}
}
下面程序将展示如何使用pool包来共享一组模拟的数据库连接
package main
import (
"io"
"log"
"math/rand"
"myExtends/pool"
"sync"
"sync/atomic"
"time"
)
const (
maxGoroutines = 25
pooledResources = 2
)
type dbConnection struct {
ID int32
}
// Close 实现了 io.Close 接口,以便 dbConnection 可以被池管理。
// Close 用来完成任意资源的释放管理
func (dbConn *dbConnection) Close() error {
log.Println("Close: Connection",dbConn.ID)
return nil
}
var idCounter int32
// createConnection 是一个工厂函数。当需要一个新连接时,资源池会调用这个函数
func createConnection() (io.Closer, error) {
id := atomic.AddInt32(&idCounter, 1)
log.Printf("create: new connection %d", id)
return &dbConnection{id}, nil
}
// performQueries 用来测试连接的资源池
func performQueries(query int, p *pool.Pool) {
// 从资源池中请求一个连接
conn, err := p.Acquire()
if err != nil{
log.Println(err)
return
}
// 将连接释放会池里
defer p.Release(conn)
// 用来等待模拟查询
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
log.Printf("QID[%d] CID[%d]\n", query, conn.(*dbConnection).ID)
}
func main() {
var wg sync.WaitGroup
wg.Add(maxGoroutines)
p, err := pool.New(createConnection, pooledResources)
if err != nil {
log.Println(err)
}
for query := 0; query < maxGoroutines; query++ {
go func(id int) {
performQueries(id, p)
wg.Done()
}(query)
}
wg.Wait()
log.Println("shutdown program.")
p.Close()
}
3、work
work包使用无缓冲的通道来创建一个 goroutine 池,这些 goroutine 执行并控制一组工作,让其并发执行。
work包程序
// Example provided with help from Jason Waldrip.
// Package work manages a pool of goroutines to perform work.
package work
import "sync"
// Worker must be implemented by types that want to use
// the work pool.
type Worker interface {
Task()
}
// Pool provides a pool of goroutines that can execute any Worker
// tasks that are submitted.
type Pool struct {
work chan Worker
wg sync.WaitGroup
}
// New creates a new work pool.
func New(maxGoroutines int) *Pool {
p := Pool{
work: make(chan Worker),
}
p.wg.Add(maxGoroutines)
for i := 0; i < maxGoroutines; i++ {
go func() {
for w := range p.work {
w.Task()
}
p.wg.Done()
}()
}
return &p
}
// Run submits work to the pool.
func (p *Pool) Run(w Worker) {
p.work <- w
}
// Shutdown waits for all the goroutines to shutdown.
func (p *Pool) Shutdown() {
close(p.work)
p.wg.Wait()
}
package main
import (
"log"
"myExtends/work"
"sync"
"time"
)
var names = []string{
"steve",
"bob",
"mary",
"therese",
"jason",
}
type namePrinter struct {
name string
}
// namePrinter 实现 Task 接口
func (m *namePrinter) Task() {
log.Println(m.name)
time.Sleep(time.Second)
}
func main() {
p := work.New(2)
var wg sync.WaitGroup
wg.Add(100 * len(names))
for i := 0; i < 100; i++ {
for _, name := range names {
np := namePrinter{
name:name,
}
go func() {
p.Run(&np)
wg.Done()
}()
}
}
wg.Wait()
p.Shutdown()
}