/
pubhandler.go
58 lines (50 loc) · 1.21 KB
/
pubhandler.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
package main
import (
"bytes"
"log"
"math/rand"
"net/http"
"time"
"github.com/littlexiang/nsq-http-sub/http_api"
)
func PubHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := http_api.NewReqParams(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data := reqParams.Body
if len(data) == 0 {
http.Error(w, "empty message payload", http.StatusBadRequest)
return
}
for _, addr := range getAllProducersHttpAddr("") {
resp, err := http.Post(addr+"/pub?topic="+topicName, "text/plain", bytes.NewReader(data))
log.Println("pub to ", addr, topicName)
if err != nil {
log.Println("pub failed ", addr, topicName, err)
//try next producer
continue
} else {
resp.Body.Close()
w.Write([]byte("OK"))
return
}
}
http.Error(w, err.Error(), http.StatusInternalServerError)
}
func ShuffleStringArray(a []string) []string {
rand.Seed(time.Now().UnixNano())
a_copy := make([]string, len(a))
copy(a_copy, a)
for i := range a_copy {
j := rand.Intn(i + 1)
a_copy[i], a_copy[j] = a_copy[j], a_copy[i]
}
return a_copy
}