Ai
9 Star 34 Fork 19

ShirDon-廖显东/Go语言设计模式-goDesignPattern-实战源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
objectPoolResource.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2023-03-02 17:06 +08:00 . commit
//++++++++++++++++++++++++++++++++++++++++
// 《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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goDesignPattern.git
git@gitee.com:shirdonl/goDesignPattern.git
shirdonl
goDesignPattern
Go语言设计模式-goDesignPattern-实战源码
8d22ec3ed188

搜索帮助