| | |
| | | "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 |
| | |
| | | if err == nil { |
| | | break loopB |
| | | } |
| | | time.Sleep(time.Millisecond) |
| | | time.Sleep(sleepPeriod) |
| | | data, id, err = NewBlock(size, key) |
| | | } |
| | | } |
| | |
| | | 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++ |
| | | } |
| | | } |
| | | } |
| | |
| | | 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) |
| | | } |
| | | } |
| | |
| | | } else { |
| | | fmt.Println("createShm error:", err) |
| | | } |
| | | time.Sleep(time.Millisecond) |
| | | time.Sleep(sleepPeriod) |
| | | data, id, err = NewBlock(size, key) |
| | | } |
| | | } |