mirror of
https://gitee.com/johng/gf
synced 2026-06-27 17:57:21 +08:00
组件优化
This commit is contained in:
@ -8,31 +8,28 @@
|
||||
package gchan
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"errors"
|
||||
"gitee.com/johng/gf/g/container/gtype"
|
||||
)
|
||||
|
||||
type Chan struct {
|
||||
mu sync.RWMutex
|
||||
list chan interface{}
|
||||
closed bool
|
||||
closed *gtype.Bool
|
||||
}
|
||||
|
||||
func New(limit int) *Chan {
|
||||
return &Chan {
|
||||
list : make(chan interface{}, limit),
|
||||
list : make(chan interface{}, limit),
|
||||
closed : gtype.NewBool(),
|
||||
}
|
||||
}
|
||||
|
||||
// 将数据压入队列
|
||||
func (q *Chan) Push(v interface{}) error {
|
||||
q.mu.RLock()
|
||||
if q.closed {
|
||||
q.mu.RUnlock()
|
||||
if q.closed.Val() {
|
||||
return errors.New("closed")
|
||||
}
|
||||
q.list <- v
|
||||
q.mu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -43,12 +40,10 @@ func (q *Chan) Pop() interface{} {
|
||||
|
||||
// 关闭队列(通知所有通过Pop阻塞的协程退出)
|
||||
func (q *Chan) Close() {
|
||||
q.mu.Lock()
|
||||
if !q.closed {
|
||||
q.closed = true
|
||||
if !q.closed.Val() {
|
||||
q.closed.Set(true)
|
||||
close(q.list)
|
||||
}
|
||||
q.mu.Unlock()
|
||||
}
|
||||
|
||||
// 获取当前队列大小
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *IntBoolMap) Contains(key int) bool {
|
||||
// 哈希表大小
|
||||
func (this *IntBoolMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *IntIntMap) Contains(key int) bool {
|
||||
// 哈希表大小
|
||||
func (this *IntIntMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -163,9 +163,9 @@ func (this *IntInterfaceMap) Contains(key int) bool {
|
||||
// 哈希表大小
|
||||
func (this *IntInterfaceMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *IntStringMap) Contains(key int) bool {
|
||||
// 哈希表大小
|
||||
func (this *IntStringMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -163,9 +163,9 @@ func (this *InterfaceInterfaceMap) Contains(key interface{}) bool {
|
||||
// 哈希表大小
|
||||
func (this *InterfaceInterfaceMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *StringBoolMap) Contains(key string) bool {
|
||||
// 哈希表大小
|
||||
func (this *StringBoolMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *StringIntMap) Contains(key string) bool {
|
||||
// 哈希表大小
|
||||
func (this *StringIntMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -163,9 +163,9 @@ func (this *StringInterfaceMap) Contains(key string) bool {
|
||||
// 哈希表大小
|
||||
func (this *StringInterfaceMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -138,9 +138,9 @@ func (this *StringStringMap) Contains(key string) bool {
|
||||
// 哈希表大小
|
||||
func (this *StringStringMap) Size() int {
|
||||
this.mu.RLock()
|
||||
len := len(this.m)
|
||||
length := len(this.m)
|
||||
this.mu.RUnlock()
|
||||
return len
|
||||
return length
|
||||
}
|
||||
|
||||
// 哈希表是否为空
|
||||
|
||||
@ -15,7 +15,7 @@ import (
|
||||
"sync"
|
||||
"errors"
|
||||
"container/list"
|
||||
"sync/atomic"
|
||||
"gitee.com/johng/gf/g/container/gtype"
|
||||
)
|
||||
|
||||
type Queue struct {
|
||||
@ -24,7 +24,7 @@ type Queue struct {
|
||||
limit int // 队列限制大小
|
||||
limits chan struct{} // 用于队列写入限制
|
||||
events chan struct{} // 用于队列出列限制
|
||||
closed int32 // 队列是否关闭(0:未关闭,1:关闭)
|
||||
closed *gtype.Bool // 队列是否关闭
|
||||
}
|
||||
|
||||
// 队列大小为非必须参数,默认不限制
|
||||
@ -37,13 +37,14 @@ func New(limit...int) *Queue {
|
||||
list : list.New(),
|
||||
limit : 0,
|
||||
limits : make(chan struct{}, size),
|
||||
events : make(chan struct{}, math.MaxInt64),
|
||||
events : make(chan struct{}, math.MaxInt32),
|
||||
closed : gtype.NewBool(),
|
||||
}
|
||||
}
|
||||
|
||||
// 将数据压入队列, 队尾
|
||||
func (q *Queue) PushBack(v interface{}) error {
|
||||
if q.isClosed() {
|
||||
if q.closed.Val() {
|
||||
return errors.New("closed")
|
||||
}
|
||||
if q.limit > 0 {
|
||||
@ -60,7 +61,7 @@ func (q *Queue) PushBack(v interface{}) error {
|
||||
|
||||
// 将数据压入队列, 队头
|
||||
func (q *Queue) PushFront(v interface{}) error {
|
||||
if q.isClosed() {
|
||||
if q.closed.Val() {
|
||||
return errors.New("closed")
|
||||
}
|
||||
// 限制队列大小,使用channel进行阻塞限制
|
||||
@ -78,6 +79,9 @@ func (q *Queue) PushFront(v interface{}) error {
|
||||
|
||||
// 从队头先进先出地从队列取出一项数据,当没有数据可获取时,阻塞等待
|
||||
func (q *Queue) PopFront() interface{} {
|
||||
if q.closed.Val() {
|
||||
return nil
|
||||
}
|
||||
if q.limit > 0 {
|
||||
<- q.limits
|
||||
} else {
|
||||
@ -95,6 +99,9 @@ func (q *Queue) PopFront() interface{} {
|
||||
|
||||
// 从队尾先进先出地从队列取出一项数据,当没有数据可获取时,阻塞等待
|
||||
func (q *Queue) PopBack() interface{} {
|
||||
if q.closed.Val() {
|
||||
return nil
|
||||
}
|
||||
if q.limit > 0 {
|
||||
<- q.limits
|
||||
} else {
|
||||
@ -112,8 +119,8 @@ func (q *Queue) PopBack() interface{} {
|
||||
|
||||
// 关闭队列(通知所有通过Pop*阻塞的协程退出)
|
||||
func (q *Queue) Close() {
|
||||
if !q.isClosed() {
|
||||
atomic.StoreInt32(&q.closed, 1)
|
||||
if !q.closed.Val() {
|
||||
q.closed.Set(true)
|
||||
close(q.limits)
|
||||
close(q.events)
|
||||
}
|
||||
@ -124,7 +131,4 @@ func (q *Queue) Size() int {
|
||||
return len(q.events)
|
||||
}
|
||||
|
||||
// 队列是否关闭
|
||||
func (q *Queue) isClosed() bool {
|
||||
return atomic.LoadInt32(&q.closed) > 0
|
||||
}
|
||||
|
||||
|
||||
@ -266,9 +266,10 @@ func (c *Cache) autoSyncLoop() {
|
||||
c.lru.Push(item.k)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// LRU缓存淘汰处理+自动清理过期键值对
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
// You can obtain one at https://gitee.com/johng/gf.
|
||||
//
|
||||
|
||||
// 命令行管理
|
||||
// 命令行管理.
|
||||
package gcmd
|
||||
|
||||
import (
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://gitee.com/johng/gf.
|
||||
|
||||
// 文件管理
|
||||
// 文件管理.
|
||||
package gfile
|
||||
|
||||
import (
|
||||
|
||||
@ -13,24 +13,37 @@ import "math"
|
||||
|
||||
// ID生成器管理对象
|
||||
type Gen struct {
|
||||
ch chan uint
|
||||
ch chan uint
|
||||
closeQueue chan struct{}
|
||||
}
|
||||
|
||||
// 创建一个ID生成器,并给定ID池大小
|
||||
func New (bufsize int) *Gen {
|
||||
g := &Gen {
|
||||
ch : make(chan uint, bufsize),
|
||||
ch : make(chan uint, bufsize),
|
||||
closeQueue : make(chan struct{}),
|
||||
}
|
||||
go g.startLoop()
|
||||
return g
|
||||
}
|
||||
|
||||
// 关闭生成器
|
||||
func (g *Gen) Close() {
|
||||
close(g.closeQueue)
|
||||
close(g.ch)
|
||||
}
|
||||
|
||||
// 内部循环,当最大值使用完之后重新从1开始获取
|
||||
func (g *Gen) startLoop() {
|
||||
for {
|
||||
// 当ch达到缓冲池大小,会阻塞,只要有线程取出值,再立即填充
|
||||
for i := uint(1); i < uint(math.MaxUint64); i++ {
|
||||
g.ch <- i
|
||||
select {
|
||||
case <- g.closeQueue:
|
||||
return
|
||||
default:
|
||||
// 当ch达到缓冲池大小,会阻塞,只要有线程取出值,再立即填充
|
||||
for i := uint(1); i < uint(math.MaxUint64); i++ {
|
||||
g.ch <- i
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -42,11 +55,14 @@ func (g *Gen) Uint() uint {
|
||||
|
||||
// 从池中获取一个ID返回(int)
|
||||
func (g *Gen) Int() int {
|
||||
i := int(<- g.ch & 0x7FFFFFFFFFFFFFFF)
|
||||
// 可能是int与uint之间的临界点
|
||||
if i == 0 {
|
||||
i = int(<- g.ch & 0x7FFFFFFFFFFFFFFF)
|
||||
i := <- g.ch
|
||||
if i != 0 {
|
||||
i = i & 0x7FFFFFFFFFFFFFFF
|
||||
// 可能是int与uint之间的临界点
|
||||
if i == 0 {
|
||||
return g.Int()
|
||||
}
|
||||
}
|
||||
return i
|
||||
return int(i)
|
||||
}
|
||||
|
||||
|
||||
@ -2,13 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
"math"
|
||||
"gitee.com/johng/gf/g/util/gidgen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(gtime.Millisecond())
|
||||
fmt.Println(math.MaxInt64)
|
||||
g := gidgen.New(2)
|
||||
for i := 0; i < 11; i++ {
|
||||
fmt.Println(g.Int())
|
||||
}
|
||||
g.Close()
|
||||
fmt.Println(g.Uint())
|
||||
//events2 := make(chan int, 100)
|
||||
//go func() {
|
||||
// for{
|
||||
|
||||
Reference in New Issue
Block a user