mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
修复gpool数据过期处理问题
This commit is contained in:
parent
505003124e
commit
c51a9e8087
@ -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)
|
||||
}
|
||||
}
|
@ -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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user