-
Notifications
You must be signed in to change notification settings - Fork 15
/
mq_helper_linux.go
92 lines (85 loc) · 2.31 KB
/
mq_helper_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2016 Aleksandr Demakin. All rights reserved.
package main
import (
"fmt"
"os"
"time"
"bitbucket.org/avd/go-ipc/mq"
)
func createMqWithType(name string, perm os.FileMode, typ, opt string) (mq.Messenger, error) {
switch typ {
case "default":
return mq.New(name, os.O_RDWR, perm)
case "sysv":
return mq.CreateSystemVMessageQueue(name, os.O_RDWR, perm)
case "fast":
mqSize, msgSize := mq.DefaultLinuxMqMaxSize, mq.DefaultLinuxMqMessageSize
if first, second, err := parseTwoInts(opt); err == nil {
mqSize, msgSize = first, second
}
return mq.CreateFastMq(name, 0, perm, mqSize, msgSize)
case "linux":
mqSize, msgSize := mq.DefaultLinuxMqMaxSize, mq.DefaultLinuxMqMessageSize
if first, second, err := parseTwoInts(opt); err == nil {
mqSize, msgSize = first, second
}
return mq.CreateLinuxMessageQueue(name, os.O_RDWR, perm, mqSize, msgSize)
default:
return nil, fmt.Errorf("unknown mq type %q", typ)
}
}
func openMqWithType(name string, flags int, typ string) (mq.Messenger, error) {
switch typ {
case "default":
return mq.Open(name, flags)
case "sysv":
return mq.OpenSystemVMessageQueue(name, flags)
case "fast":
return mq.OpenFastMq(name, flags)
case "linux":
return mq.OpenLinuxMessageQueue(name, flags)
default:
return nil, fmt.Errorf("unknown mq type %q", typ)
}
}
func destroyMqWithType(name, typ string) error {
switch typ {
case "default":
return mq.Destroy(name)
case "sysv":
return mq.DestroySystemVMessageQueue(name)
case "fast":
return mq.DestroyFastMq(name)
case "linux":
return mq.DestroyLinuxMessageQueue(name)
default:
return fmt.Errorf("unknown mq type %q", typ)
}
}
func notifywait(name string, timeout int, typ string) error {
if typ != "linux" {
return fmt.Errorf("notifywait is supported for 'linux' mq, not '%s'", typ)
}
mq, err := mq.OpenLinuxMessageQueue(name, os.O_RDWR)
if err != nil {
return err
}
defer mq.Close()
notifyChan := make(chan int, 1)
if err = mq.Notify(notifyChan); err != nil {
return err
}
var timeChan <-chan time.Time
if timeout > 0 {
timeChan = time.After(time.Duration(timeout) * time.Millisecond)
}
select {
case id := <-notifyChan:
if id != mq.ID() {
return fmt.Errorf("expected mq with id %q, got with %q", mq.ID(), id)
}
case <-timeChan:
return fmt.Errorf("operation timeout")
}
return nil
}