修复gpool数据过期处理问题

This commit is contained in:
John 2018-07-21 15:23:16 +08:00
parent 505003124e
commit c51a9e8087
2 changed files with 18 additions and 10 deletions

View File

@ -30,6 +30,7 @@ type poolItem struct {
}
// 创建一个对象池,为保证执行效率,过期时间一旦设定之后无法修改
// 注意过期时间单位为**毫秒**
func New(expire int, newFunc...func() (interface{}, error)) *Pool {
r := &Pool {
list : glist.New(),
@ -82,13 +83,15 @@ func (p *Pool) Close() {
// 超时检测循环
func (p *Pool) expireCheckingLoop() {
for !p.closed.Val() {
if r := p.list.PopFront(); r != nil {
f := r.(*poolItem)
if f.expire > gtime.Millisecond() {
p.list.PushFront(f)
break
for {
if r := p.list.PopFront(); r != nil {
f := r.(*poolItem)
if f.expire > gtime.Millisecond() {
p.list.PushFront(f)
break
}
}
}
time.Sleep(3 * time.Second)
time.Sleep(time.Second)
}
}

View File

@ -1,12 +1,17 @@
package main
import (
"gitee.com/johng/gf/g/container/gpool"
"fmt"
"gitee.com/johng/gf/g/net/ghttp"
"time"
)
func main() {
r, _ := ghttp.Get("http://johng.cn")
fmt.Println(string(r.ReadAll()))
p := gpool.New(1000)
for i := 0 ; i < 100; i++ {
p.Put(i)
}
fmt.Println(p.Size())
time.Sleep(2*time.Second)
fmt.Println(p.Size())
}