From 1df33ffacf98f09a33969a8c8ed4078247524d29 Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期五, 09 十月 2020 14:27:45 +0800
Subject: [PATCH] attach增加超时

---
 shmwrap.go |   76 +++++++++++++++++++++++++++++++++++--
 1 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/shmwrap.go b/shmwrap.go
index 8dad1d7..7697cb7 100644
--- a/shmwrap.go
+++ b/shmwrap.go
@@ -4,7 +4,12 @@
     "context"
     "fmt"
     "time"
-    "github.com/gen2brain/shm"
+    shm "basic.com/valib/goshm.git"
+)
+
+const (
+    sleepPeriod = time.Duration(5) * time.Millisecond
+    attachTimes = 100
 )
 
 // NewBlock shm block with size
@@ -72,7 +77,7 @@
                 if err == nil {
                     break loopB
                 }
-                time.Sleep(time.Millisecond)
+                time.Sleep(sleepPeriod)
                 data, id, err = NewBlock(size, key)
             }
         }
@@ -93,8 +98,31 @@
                 if err == nil {
                     break loopB
                 }
-                time.Sleep(time.Millisecond)
+                time.Sleep(sleepPeriod)
                 data, id, err = AttachBlock(key)
+            }
+        }
+    }
+    return data, id
+}
+
+//AttachRawShmTimeout don't create
+func AttachRawShmTimeout(ctx context.Context, key int) ([]byte, int) {
+    tryCount := 0
+    data, id, err := AttachBlock(key)
+    if err != nil {
+    loopB:
+        for {
+            select {
+            case <-ctx.Done():
+                return nil, -1
+            default:
+                if err == nil || tryCount > attachTimes {
+                    break loopB
+                }
+                time.Sleep(sleepPeriod)
+                data, id, err = AttachBlock(key)
+                tryCount++
             }
         }
     }
@@ -115,7 +143,33 @@
                 if err == nil {
                     break loopB
                 }
-                time.Sleep(time.Millisecond)
+                time.Sleep(sleepPeriod)
+                id, err = NewBlockOnly(size, key)
+            }
+        }
+    }
+    return id
+}
+
+// CreateShmOnly create shm block with size, only space, no padding, return id(int)
+// context for quit
+func CreateShmOnlyTime(ctx context.Context, size int, key int, timeout int) int {
+    id, err := NewBlockOnly(size, key)
+    if err != nil {
+        to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
+        defer to.Stop()
+    loopB:
+        for {
+            select {
+            case <-ctx.Done():
+                return -1
+            case <-to.C:
+                return -1
+            default:
+                if err == nil {
+                    break loopB
+                }
+                time.Sleep(sleepPeriod)
                 id, err = NewBlockOnly(size, key)
             }
         }
@@ -139,7 +193,7 @@
                 } else {
                     fmt.Println("createShm error:", err)
                 }
-                time.Sleep(time.Millisecond)
+                time.Sleep(sleepPeriod)
                 data, id, err = NewBlock(size, key)
             }
         }
@@ -149,6 +203,18 @@
     return data, id
 }
 
+// CheckShmExist check if shm is exist or not
+// if exist, return id and true, else return 0 and false
+func CheckShmExist(key int) (int, bool) {
+    id, err := shm.Get(key, 0, 0)
+    if err != nil || id == -1 { //no exist
+        fmt.Printf("AttachBlock Get:%x, %v\n", key, err)
+        return 0, false
+    }
+
+    return id, true
+}
+
 // DetachShm destroy
 func DetachShm(data []byte) {
     ReleaseBlock(data)

--
Gitblit v1.8.0