-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.go
297 lines (260 loc) · 8.42 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2014 httpmq Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// httpmq is an open-source, lightweight and high-performance message queue.
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"math"
"net/http"
_ "net/http/pprof"
"runtime"
"strconv"
"github.com/go-kod/kod"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
// VERSION of httpmq
const VERSION = "0.5"
var db *leveldb.DB
var defaultMaxqueue, cpu, cacheSize, writeBuffer *int
var ip, port, defaultAuth, dbPath *string
// httpmq read metadata api
// retrieve from leveldb
// name.maxqueue - maxqueue
// name.putpos - putpos
// name.getpos - getpos
func httpmqReadMetadata(name string) []string {
maxqueue := name + ".maxqueue"
data1, _ := db.Get([]byte(maxqueue), nil)
if len(data1) == 0 {
data1 = []byte(strconv.Itoa(*defaultMaxqueue))
}
putpos := name + ".putpos"
data2, _ := db.Get([]byte(putpos), nil)
getpos := name + ".getpos"
data3, _ := db.Get([]byte(getpos), nil)
return []string{string(data1), string(data2), string(data3)}
}
// httpmq now getpos api
// get the current getpos of httpmq for request
func httpmqNowGetpos(name string) string {
metadata := httpmqReadMetadata(name)
maxqueue, _ := strconv.Atoi(metadata[0])
putpos, _ := strconv.Atoi(metadata[1])
getpos, _ := strconv.Atoi(metadata[2])
if getpos == 0 && putpos > 0 {
getpos = 1 // first get operation, set getpos 1
} else if getpos < putpos {
getpos++ // 1nd lap, increase getpos
} else if getpos > putpos && getpos < maxqueue {
getpos++ // 2nd lap
} else if getpos > putpos && getpos == maxqueue {
getpos = 1 // 2nd first operation, set getpos 1
} else {
return "0" // all data in queue has been get
}
data := strconv.Itoa(getpos)
_ = db.Put([]byte(name+".getpos"), []byte(data), nil)
return data
}
// httpmq now putpos api
// get the current putpos of httpmq for request
func httpmqNowPutpos(name string) string {
metadata := httpmqReadMetadata(name)
maxqueue, _ := strconv.Atoi(metadata[0])
putpos, _ := strconv.Atoi(metadata[1])
getpos, _ := strconv.Atoi(metadata[2])
putpos++ // increase put queue pos
if putpos == getpos { // queue is full
return "0" // return 0 to reject put operation
} else if getpos <= 1 && putpos > maxqueue { // get operation less than 1
return "0" // and queue is full, just reject it
} else if putpos > maxqueue { // 2nd lap
metadata[1] = "1" // reset putpos as 1 and write to leveldb
} else { // 1nd lap, convert int to string and write to leveldb
metadata[1] = strconv.Itoa(putpos)
}
_ = db.Put([]byte(name+".putpos"), []byte(metadata[1]), nil)
return metadata[1]
}
func init() {
defaultMaxqueue = flag.Int("maxqueue", 1000000, "the max queue length")
ip = flag.String("ip", "0.0.0.0", "ip address to listen on")
port = flag.String("port", "1218", "port to listen on")
defaultAuth = flag.String("auth", "", "auth password to access httpmq")
dbPath = flag.String("db", "level.db", "database path")
cacheSize = flag.Int("cache", 64, "cache size(MB)")
writeBuffer = flag.Int("buffer", 32, "write buffer(MB)")
cpu = flag.Int("cpu", runtime.NumCPU(), "cpu number for httpmq")
flag.Parse()
var err error
db, err = leveldb.OpenFile(*dbPath, &opt.Options{BlockCacheCapacity: *cacheSize,
WriteBuffer: *writeBuffer * 1024 * 1024})
if err != nil {
log.Fatalln("db.Get(), err:", err)
}
}
type app struct {
kod.Implements[kod.Main]
}
func (*app) run() {
runtime.GOMAXPROCS(*cpu)
sync := &opt.WriteOptions{Sync: true}
putnamechan := make(chan string, 100)
putposchan := make(chan string, 100)
getnamechan := make(chan string, 100)
getposchan := make(chan string, 100)
go func(chan string, chan string) {
for {
name := <-putnamechan
putpos := httpmqNowPutpos(name)
putposchan <- putpos
}
}(putnamechan, putposchan)
go func(chan string, chan string) {
for {
name := <-getnamechan
getpos := httpmqNowGetpos(name)
getposchan <- getpos
}
}(getnamechan, getposchan)
m := &http.ServeMux{}
m.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
var data string
var buf []byte
auth := string(r.FormValue("auth"))
name := string(r.FormValue("name"))
opt := string(r.FormValue("opt"))
pos := string(r.FormValue("pos"))
num := string(r.FormValue("num"))
charset := string(r.FormValue("charset"))
if *defaultAuth != "" && *defaultAuth != auth {
_, _ = rw.Write([]byte("HTTPMQ_AUTH_FAILED"))
return
}
method := string(r.Method)
if method == "GET" {
data = string(r.FormValue("data"))
} else if method == "POST" {
if string(r.Header.Get("Content-Type")) == "application/x-www-form-urlencoded" {
data = string(r.FormValue("data"))
} else {
buf, _ = io.ReadAll(r.Body)
defer r.Body.Close()
}
}
if len(name) == 0 || len(opt) == 0 {
_, _ = rw.Write([]byte("HTTPMQ_ERROR"))
return
}
rw.Header().Set("Connection", "keep-alive")
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Content-type", "text/plain")
if len(charset) > 0 {
rw.Header().Set("Content-type", "text/plain; charset="+charset)
}
if opt == "put" {
if len(data) == 0 && len(buf) == 0 {
_, _ = rw.Write([]byte("HTTPMQ_PUT_ERROR"))
return
}
putnamechan <- name
putpos := <-putposchan
if putpos != "0" {
queueName := name + putpos
if data != "" {
_ = db.Put([]byte(queueName), []byte(data), nil)
} else if len(buf) > 0 {
_ = db.Put([]byte(queueName), buf, nil)
}
rw.Header().Set("Pos", putpos)
_, _ = rw.Write([]byte("HTTPMQ_PUT_OK"))
} else {
_, _ = rw.Write([]byte("HTTPMQ_PUT_END"))
}
} else if opt == "get" {
getnamechan <- name
getpos := <-getposchan
if getpos == "0" {
_, _ = rw.Write([]byte("HTTPMQ_GET_END"))
} else {
queueName := name + getpos
v, err := db.Get([]byte(queueName), nil)
if err == nil {
rw.Header().Set("Pos", getpos)
_, _ = rw.Write(v)
} else {
_, _ = rw.Write([]byte("HTTPMQ_GET_ERROR"))
}
}
} else if opt == "status" {
metadata := httpmqReadMetadata(name)
maxqueue, _ := strconv.Atoi(metadata[0])
putpos, _ := strconv.Atoi(metadata[1])
getpos, _ := strconv.Atoi(metadata[2])
var ungetnum float64
var putTimes, getTimes string
if putpos >= getpos {
ungetnum = math.Abs(float64(putpos - getpos))
putTimes = "1st lap"
getTimes = "1st lap"
} else if putpos < getpos {
ungetnum = math.Abs(float64(maxqueue - getpos + putpos))
putTimes = "2nd lap"
getTimes = "1st lap"
}
buf := fmt.Sprintf("HTTP Simple Queue Service v%s\n", VERSION)
buf += "------------------------------\n"
buf += fmt.Sprintf("Queue Name: %s\n", name)
buf += fmt.Sprintf("Maximum number of queues: %d\n", maxqueue)
buf += fmt.Sprintf("Put position of queue (%s): %d\n", putTimes, putpos)
buf += fmt.Sprintf("Get position of queue (%s): %d\n", getTimes, getpos)
buf += fmt.Sprintf("Number of unread queue: %g\n\n", ungetnum)
_, _ = rw.Write([]byte(buf))
} else if opt == "view" {
v, err := db.Get([]byte(name+pos), nil)
if err == nil {
_, _ = rw.Write([]byte(v))
} else {
_, _ = rw.Write([]byte("HTTPMQ_VIEW_ERROR"))
}
} else if opt == "reset" {
maxqueue := strconv.Itoa(*defaultMaxqueue)
_ = db.Put([]byte(name+".maxqueue"), []byte(maxqueue), sync)
_ = db.Put([]byte(name+".putpos"), []byte("0"), sync)
_ = db.Put([]byte(name+".getpos"), []byte("0"), sync)
_, _ = rw.Write([]byte("HTTPMQ_RESET_OK"))
} else if opt == "maxqueue" {
maxqueue, _ := strconv.Atoi(num)
if maxqueue > 0 && maxqueue <= 10000000 {
_ = db.Put([]byte(name+".maxqueue"), []byte(num), sync)
_, _ = rw.Write([]byte("HTTPMQ_MAXQUEUE_OK"))
} else {
_, _ = rw.Write([]byte("HTTPMQ_MAXQUEUE_CANCLE"))
}
}
})
log.Fatal(http.ListenAndServe(*ip+":"+*port, m))
}
//go:generate go run github.com/go-kod/kod/cmd/kod generate
func main() {
_ = kod.Run(context.Background(), func(ctx context.Context, app *app) error {
app.run()
return nil
})
}