From 1df33ffacf98f09a33969a8c8ed4078247524d29 Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期五, 09 十月 2020 14:27:45 +0800
Subject: [PATCH] attach增加超时
---
shmqueue.go | 2 +-
shmwrap.go | 28 +++++++++++++++++++++++++++-
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/shmqueue.go b/shmqueue.go
index 16d8b2e..9c4f16c 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -106,7 +106,7 @@
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
}
diff --git a/shmwrap.go b/shmwrap.go
index a6ae885..7697cb7 100644
--- a/shmwrap.go
+++ b/shmwrap.go
@@ -7,7 +7,10 @@
shm "basic.com/valib/goshm.git"
)
-const sleepPeriod = time.Duration(5) * time.Millisecond
+const (
+ sleepPeriod = time.Duration(5) * time.Millisecond
+ attachTimes = 100
+)
// NewBlock shm block with size
func NewBlock(size int, key int) ([]byte, int, error) {
@@ -103,6 +106,29 @@
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++
+ }
+ }
+ }
+ return data, id
+}
+
// CreateShmOnly create shm block with size, only space, no padding, return id(int)
// context for quit
func CreateShmOnly(ctx context.Context, size int, key int) int {
--
Gitblit v1.8.0