go項目源碼分析與使用--協程池pool

1.項目介紹

地址: https://github.com/gobwas/ws-examples/blob/master/src/gopool/pool.go
只介紹一個代碼文件,它簡潔優雅的實現了一個goroutine的pool

2.關鍵源碼

pool.go

// Package gopool contains tools for goroutine reuse.
// It is implemented only for examples of github.com/gobwas/ws usage.
package gopool

import (
	"fmt"
	"time"
)

// ErrScheduleTimeout returned by Pool to indicate that there no free
// goroutines during some period of time.
var ErrScheduleTimeout = fmt.Errorf("schedule error: timed out")

// Pool contains logic of goroutine reuse.
type Pool struct {
	sem  chan struct{}  //可以理解爲坑位或者令牌
	work chan func()   //任務隊列
}

// NewPool creates new goroutine pool with given size. It also creates a work
// queue of given size. Finally, it spawns given amount of goroutines
// immediately.
func NewPool(size, queue, spawn int) *Pool {
	if spawn <= 0 && queue > 0 {
		panic("dead queue configuration detected")
	}
	if spawn > size {
		panic("spawn > workers")
	}
	p := &Pool{
		sem:  make(chan struct{}, size),   //有size個坑位
		work: make(chan func(), queue),
	}
	//每個工作goroutine都要先搶佔一個坑位才能工作,開始先啓動spawn個工作goroutine
	for i := 0; i < spawn; i++ {
		p.sem <- struct{}{}  
		go p.worker(func() {})
	}

	return p
}

// Schedule schedules task to be executed over pool's workers.
//任務調度
func (p *Pool) Schedule(task func()) {
	p.schedule(task, nil)
}

// ScheduleTimeout schedules task to be executed over pool's workers.
// It returns ErrScheduleTimeout when no free workers met during given timeout.
func (p *Pool) ScheduleTimeout(timeout time.Duration, task func()) error {
	return p.schedule(task, time.After(timeout))
}

//分四種情況討論
//p.work沒滿,p.sem沒滿:  任務可能投放到p.work中,也可能起一個新的goroutine處理
//p.work沒滿,p.sem已滿:  任務投放p.work中
//p.work已滿,p.sem沒滿:  任務被新的goroutine處理
//p.work已滿,p.sem已滿:  任務投放到p.work被阻塞,如果有timeout,則可能超時返回
func (p *Pool) schedule(task func(), timeout <-chan time.Time) error {
	select {
	case <-timeout:
		return ErrScheduleTimeout
	case p.work <- task:
		return nil
	case p.sem <- struct{}{}:
		go p.worker(task)
		return nil
	}
}

func (p *Pool) worker(task func()) {
	//退出纔會讓出坑位
	defer func() { <-p.sem }()

	task()

	for task := range p.work {
		task()
	}
}

3.例子


package main

import (
	"fmt"
	"net/http"
	"runtime"
	"strconv"
	"sync"
	"time"

	"github.com/gobwas/ws-examples/src/gopool"
)

func main() {
	//pool大小10個,任務隊列50
	workers, queue := 10, 50
	pool := gopool.NewPool(workers, queue, 1)
	go monitor()
	var wg sync.WaitGroup
	time.Sleep(5 * time.Second)
	//在這裏已經有7個goroutine,有1個是pool預啓動的工作goroutine
	//後面不斷髮送任務到隊列裏去,goroutine的個數會逐漸增長到16個就停止增長

	for i := 0; i < 100; i++ {
		time.Sleep(1 * time.Second) //方便查看goroutine個數增加的過程
		n := i + 1
		wg.Add(1)
		fn := func() {
			defer wg.Done()
			fmt.Println("task number:", n)
			time.Sleep(5 * time.Second)
		}
		pool.Schedule(fn)
	}
	wg.Wait()
}

//訪問http://localhost:6060/goroutines 可實時監控goroutine的個數
func monitor() {
	http.HandleFunc("/goroutines", func(w http.ResponseWriter, r *http.Request) {
		num := strconv.FormatInt(int64(runtime.NumGoroutine()), 10)
		w.Write([]byte(num))
	})
	http.ListenAndServe("localhost:6060", nil)
	fmt.Println("goroutine stats and pprof listen on 6060")
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章