forked from nickalie/go-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
187 lines (141 loc) · 3.21 KB
/
fs.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package queue
import (
"bytes"
"errors"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"time"
)
var errFound = errors.New("found")
var errNotFound = errors.New("not Found")
var errLocked = errors.New("locked")
// FSBackend uses file system to manage queues.
// Suitable for multithreaded and multi-process environments.
type FSBackend struct {
path string
codec Codec
interval time.Duration
key string
}
// NewFSBackend creates new FSBackend.
func NewFSBackend(path string) (*FSBackend, error) {
b := &FSBackend{path: path}
return b.Codec(NewGOBCodec()).Interval(time.Second), nil
}
// Codec sets codec to encode/decode objects in queues. GOBCodec is default.
func (b *FSBackend) Codec(c Codec) *FSBackend {
b.codec = c
return b
}
// Interval sets interval to poll new queue element. Default value is one second.
func (b *FSBackend) Interval(interval time.Duration) *FSBackend {
b.interval = interval
return b
}
// Put adds value to the end of a queue.
func (b *FSBackend) Put(queueName string, value interface{}) error {
path := filepath.Join(b.path, queueName)
err := os.MkdirAll(path, 0777)
if err != nil {
return err
}
b.key = increaseString(b.key)
fileName := filepath.Join(path, b.key)
l, err := getLock(path)
if err != nil {
return err
}
data, err := b.codec.Marshal(value)
if err != nil {
l.unlock()
return err
}
err = ioutil.WriteFile(fileName, data, 0777)
if err != nil {
l.unlock()
return err
}
return l.unlock()
}
// Get removes the first element from a queue and put it in the value pointed to by v
func (b *FSBackend) Get(queueName string, v interface{}) error {
dir := filepath.Join(b.path, queueName)
var fileName string
var l *lock
for {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() || filepath.Ext(path) == ".lock" {
return nil
}
l, err = getLock(path)
if err != nil {
return nil
}
fileName = path
return errFound
})
if err == errFound {
err = b.readFile(l, fileName, v)
} else {
err = errNotFound
}
if err != nil {
time.Sleep(b.interval)
} else {
return nil
}
}
}
func (b *FSBackend) RemoveQueue(queueName string) error {
dir := filepath.Join(b.path, queueName)
return os.RemoveAll(dir)
}
func (b *FSBackend) readFile(l *lock, fileName string, value interface{}) error {
data, err := ioutil.ReadFile(fileName)
if err != nil {
l.unlock()
return err
}
err = l.unlock()
if err != nil {
return err
}
err = os.Remove(fileName)
if err != nil {
return err
}
return b.codec.Unmarshal(data, value)
}
type lock struct {
path string
id []byte
}
func getLock(path string) (*lock, error) {
lockFile := path + ".lock"
_, err := os.Stat(lockFile)
if err == nil {
return nil, errLocked
}
id := []byte(strconv.FormatUint(rand.Uint64(), 10))
err = ioutil.WriteFile(lockFile, id, 0777)
if err != nil {
os.Remove(lockFile)
return nil, err
}
return &lock{path: lockFile, id: id}, nil
}
func (l *lock) unlock() error {
id, err := ioutil.ReadFile(l.path)
if err != nil {
os.Remove(l.path)
return err
}
if !bytes.Equal(l.id, id) {
os.Remove(l.path)
return errLocked
}
return os.Remove(l.path)
}