| | |
| | | package shmqueue |
| | | |
| | | import ( |
| | | shm "basic.com/valib/goshm.git" |
| | | "context" |
| | | "fmt" |
| | | "github.com/gen2brain/shm" |
| | | "reflect" |
| | | "runtime" |
| | | "sync/atomic" |
| | |
| | | ) |
| | | |
| | | const ( |
| | | TimePeriodPutOrGet = time.Duration(10) * time.Microsecond |
| | | TimePeriodPutOrGet = time.Duration(5) * time.Millisecond //ms |
| | | ) |
| | | |
| | | //Element info |
| | |
| | | shmLen = shmblocks*uint32(unsafe.Sizeof(datainfo)) + uint32(unsafe.Sizeof(shmstruct)) |
| | | |
| | | data, shmid := CreateRawShm(ctx, int(shmLen), key) |
| | | if shmid == -1 { |
| | | return nil |
| | | } |
| | | q := bytes2shmEsQueue(data) |
| | | //init parameters |
| | | q.capacity = shmblocks |
| | |
| | | cache.getNo = q.capacity |
| | | cache.putNo = q.capacity |
| | | |
| | | fmt.Println("NewQueue EsCaches:", eqi.EsCaches) |
| | | //fmt.Println("NewQueue EsCaches:", eqi.EsCaches) |
| | | |
| | | eqi.ShmData = data |
| | | eqi.ShmID = shmid |
| | | eqi.ShmKey = key |
| | | eqi.Queue = q |
| | | time.Sleep(10 * time.Second) |
| | | |
| | | return &eqi |
| | | } |
| | |
| | | func AttachQueue(ctx context.Context, key int) *EsQueueInfo { |
| | | |
| | | var eqi EsQueueInfo |
| | | data, shmid := AttachRawShm(ctx, key) |
| | | data, shmid := AttachRawShmTimeout(ctx, key) |
| | | if shmid == -1 { |
| | | return nil |
| | | } |
| | | shmdata := bytes2shmEsQueue(data) |
| | | eqi.EsCaches = ptr2esCache(unsafe.Pointer(&shmdata.cache), int(shmdata.capacity)) |
| | | fmt.Println("AttachQueue EsCaches:", eqi.EsCaches) |
| | | //fmt.Println("AttachQueue EsCaches:", eqi.EsCaches) |
| | | |
| | | eqi.Queue = shmdata |
| | | eqi.ShmKey = key |
| | |
| | | // ReleaseQueue detach an exist shm queue |
| | | func (eqi *EsQueueInfo) ReleaseQueue() { |
| | | fmt.Printf("ReleaseQueue: key=%x\n", eqi.ShmKey) |
| | | DestroyShm(eqi.ShmData) |
| | | DetachShm(eqi.ShmData) |
| | | } |
| | | |
| | | // RemoveShmId remove an exist shm queue (ipcrm -m shmid) |
| | |
| | | func (eqi *EsQueueInfo) QueueSize() int { |
| | | var putPos, getPos uint32 |
| | | var quantity uint32 |
| | | |
| | | getPos = atomic.LoadUint32(&eqi.Queue.getPos) |
| | | putPos = atomic.LoadUint32(&eqi.Queue.putPos) |
| | | |
| | | if putPos >= getPos { |
| | | quantity = putPos - getPos |
| | | } else { |
| | | //quantity = q.capMod + (putPos - getPos) |
| | | quantity = (eqi.Queue.capMod + (putPos - getPos)) % eqi.Queue.capMod |
| | | quantity = eqi.Queue.capMod + (putPos - getPos) |
| | | } |
| | | |
| | | return int(quantity) |
| | |
| | | var elems []ElemInfo |
| | | |
| | | for i := 0; i < len(eqi.EsCaches); i++ { |
| | | if eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 { |
| | | if eqi.EsCaches[i].value.PicId != -1 && eqi.EsCaches[i].value.InfoId != -1 && |
| | | eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 { |
| | | elems = append(elems, eqi.EsCaches[i].value) |
| | | } |
| | | } |
| | |
| | | if putPos >= getPos { |
| | | posCnt = putPos - getPos |
| | | } else { |
| | | //posCnt = capMod + (putPos - getPos) |
| | | posCnt = (capMod + (putPos - getPos)) % capMod |
| | | posCnt = capMod + (putPos - getPos) |
| | | } |
| | | |
| | | //todo |
| | |
| | | } |
| | | |
| | | //PutForce 强制写入,失败则继续重写,直到成功为止 |
| | | func (eqi *EsQueueInfo) PutForce(val ElemInfo) int { |
| | | func (eqi *EsQueueInfo) PutForce(ctx context.Context, val ElemInfo) int { |
| | | ok, qua := eqi.Put(val) |
| | | for !ok { |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | ok, qua = eqi.Put(val) |
| | | loopB: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return 0 |
| | | default: |
| | | ok, qua = eqi.Put(val) |
| | | if ok { |
| | | break loopB |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | } |
| | | } |
| | | } |
| | | |
| | | return qua |
| | | } |
| | | |
| | | //PutForceWithinTime 在timeout ms的时间内,强制写入,失败则继续重写,直到成功为止或者超时退出 |
| | | func (eqi *EsQueueInfo) PutForceWithinTime(val ElemInfo, timeout int) (bool, int) { |
| | | func (eqi *EsQueueInfo) PutForceWithinTime(ctx context.Context, val ElemInfo, timeout int) (bool, int) { |
| | | ok, qua := eqi.Put(val) |
| | | if ok { |
| | | return ok, qua |
| | | } |
| | | |
| | | to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | defer to.Stop() |
| | | |
| | | for { |
| | | select { |
| | | case <-to.C: |
| | | return false, 0 |
| | | default: |
| | | ok, qua = eqi.Put(val) |
| | | if ok { |
| | | return ok, qua |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | count := timeout/int(TimePeriodPutOrGet) + 1 |
| | | for i := 0; i < count; i++ { |
| | | if ok, qua := eqi.Put(val); ok { |
| | | return ok, qua |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | } |
| | | return ok, qua |
| | | return false, 0 |
| | | |
| | | // to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | // defer to.Stop() |
| | | |
| | | // loopB: |
| | | // for { |
| | | // select { |
| | | // case <-ctx.Done(): |
| | | // return false, 0 |
| | | // case <-to.C: |
| | | // return false, 0 |
| | | // default: |
| | | // ok, qua = eqi.Put(val) |
| | | // if ok { |
| | | // break loopB |
| | | // } |
| | | // time.Sleep(TimePeriodPutOrGet) |
| | | // } |
| | | // } |
| | | // return ok, qua |
| | | } |
| | | |
| | | // Get 单次获取,可能失败 |
| | |
| | | putNo := atomic.LoadUint32(&cache.putNo) |
| | | if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity { |
| | | val := cache.value |
| | | cache.value = ElemInfo{PicId: 0} |
| | | cache.value = ElemInfo{PicId: 0, InfoId: 0} |
| | | atomic.AddUint32(&cache.getNo, eqi.Queue.capacity) |
| | | return val, true, int(posCnt - 1) |
| | | } else { |
| | |
| | | } |
| | | |
| | | //GetForce 强制获取数据,失败则继续获取,直到成功为止 |
| | | func (eqi *EsQueueInfo) GetForce() (ElemInfo, int) { |
| | | func (eqi *EsQueueInfo) GetForce(ctx context.Context) (ElemInfo, int) { |
| | | val, ok, qua := eqi.Get() |
| | | for !ok { |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | val, ok, qua = eqi.Get() |
| | | loopB: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return ElemInfo{}, 0 |
| | | default: |
| | | val, ok, qua = eqi.Get() |
| | | if ok { |
| | | break loopB |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | } |
| | | } |
| | | } |
| | | |
| | | return val, qua |
| | | } |
| | | |
| | | //GetForceWithinTime 在timeout ms的时间内,强制获取数据,失败则继续获取,直到成功为止 |
| | | func (eqi *EsQueueInfo) GetForceWithinTime(timeout int) (ElemInfo, bool, int) { |
| | | func (eqi *EsQueueInfo) GetForceWithinTime(ctx context.Context, timeout int) (ElemInfo, bool, int) { |
| | | val, ok, qua := eqi.Get() |
| | | if ok { |
| | | return val, ok, qua |
| | | } |
| | | |
| | | to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | defer to.Stop() |
| | | |
| | | for { |
| | | select { |
| | | case <-to.C: |
| | | return ElemInfo{}, false, 0 |
| | | default: |
| | | val, ok, qua = eqi.Get() |
| | | if ok { |
| | | return val, ok, qua |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | count := timeout/int(TimePeriodPutOrGet) + 1 |
| | | for i := 0; i < count; i++ { |
| | | if val, ok, qua := eqi.Get(); ok { |
| | | return val, ok, qua |
| | | } |
| | | time.Sleep(TimePeriodPutOrGet) |
| | | } |
| | | return val, ok, qua |
| | | return ElemInfo{}, false, 0 |
| | | |
| | | // to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | // defer to.Stop() |
| | | |
| | | // loopB: |
| | | // for { |
| | | // select { |
| | | // case <-ctx.Done(): |
| | | // return ElemInfo{}, false, 0 |
| | | // case <-to.C: |
| | | // return ElemInfo{}, false, 0 |
| | | // default: |
| | | // val, ok, qua = eqi.Get() |
| | | // if ok { |
| | | // break loopB |
| | | // } |
| | | // time.Sleep(TimePeriodPutOrGet) |
| | | // } |
| | | // } |
| | | // return val, ok, qua |
| | | } |
| | | |
| | | // round 到最近的2的倍数 |