From 2baeac99a903eae2e99bf30581633da6aa7f8abc Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 24 九月 2021 11:37:17 +0800
Subject: [PATCH] bug fixed withintime

---
 shmqueue.go |   95 +++++++++++++++++++++++++++--------------------
 1 files changed, 55 insertions(+), 40 deletions(-)

diff --git a/shmqueue.go b/shmqueue.go
index 9c4f16c..cafee7c 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -1,9 +1,9 @@
 package shmqueue
 
 import (
+	shm "basic.com/valib/goshm.git"
 	"context"
 	"fmt"
-	shm "basic.com/valib/goshm.git"
 	"reflect"
 	"runtime"
 	"sync/atomic"
@@ -12,7 +12,7 @@
 )
 
 const (
-	TimePeriodPutOrGet = time.Duration(5)*time.Millisecond //ms
+	TimePeriodPutOrGet = time.Duration(5) * time.Millisecond //ms
 )
 
 //Element info
@@ -252,25 +252,33 @@
 		return ok, qua
 	}
 
-	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)
-			}
+	count := timeout/int(TimePeriodPutOrGet) + 1
+	for i := 0; i < count; i++ {
+		if ok, qua := eqi.Put(val); ok {
+			return ok, qua
 		}
-	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 鍗曟鑾峰彇锛屽彲鑳藉け璐�
@@ -306,7 +314,7 @@
 		putNo := atomic.LoadUint32(&cache.putNo)
 		if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity {
 			val := cache.value
-			cache.value = ElemInfo{PicId: 0, InfoId:0}
+			cache.value = ElemInfo{PicId: 0, InfoId: 0}
 			atomic.AddUint32(&cache.getNo, eqi.Queue.capacity)
 			return val, true, int(posCnt - 1)
 		} else {
@@ -343,26 +351,33 @@
 	if ok {
 		return val, ok, qua
 	}
-
-	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)
-			}
+	count := timeout/int(TimePeriodPutOrGet) + 1
+	for i := 0; i < count; i++ {
+		if val, ok, qua := eqi.Get(); ok {
+			return val, ok, qua
 		}
-	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鐨勫�嶆暟

--
Gitblit v1.8.0