代码拉取完成,页面将自动刷新
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 作者知乎:https://www.zhihu.com/people/shirdonl
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++
package actualCombat
import (
"errors"
"log"
"math/rand"
"sync"
"time"
)
const getResMaxTime = 3 * time.Second
var (
ErrPoolNotExist = errors.New("pool not exist")
ErrGetResTimeout = errors.New("get resource time out")
)
//资源类
type Resource struct {
reusable int
}
//初始化资源对象
//模拟缓慢的资源访问,例如,TCP 连接等
func NewResource(id int) *Resource {
time.Sleep(500 * time.Millisecond)
return &Resource{reusable: id}
}
//模拟资源耗时
func (r *Resource) Do(workId int) {
time.Sleep(time.Duration(rand.Intn(5)) * 100 * time.Millisecond)
log.Printf("using resource #%d finished work %d finish\n", r.reusable, workId)
}
//对象池
type Pool chan *Resource
//并发创建资源对象,节省资源对象初始化时间
func New(size int) Pool {
p := make(Pool, size)
wg := new(sync.WaitGroup)
wg.Add(size)
for i := 0; i < size; i++ {
go func(reusable int) {
p <- NewResource(reusable)
wg.Done()
}(i)
}
wg.Wait()
return p
}
//从获取对象池获取对象
func (p Pool) GetResource() (r *Resource, err error) {
select {
case r := <-p:
return r, nil
case <-time.After(getResMaxTime):
return nil, ErrGetResTimeout
}
}
//将资源返回到资源池
func (p Pool) GiveBackResource(r *Resource) error {
if p == nil {
return ErrPoolNotExist
}
p <- r
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。