Concurrent use of message and we modify it, bad practice, copy it

This commit is contained in:
Asim 2016-06-25 03:15:42 +01:00
parent 7968a8eb6d
commit 0348d0eed9

View File

@ -288,7 +288,6 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
id := req.Form.Get("id") id := req.Form.Get("id")
h.RLock() h.RLock()
for _, subscriber := range h.subscribers[topic] { for _, subscriber := range h.subscribers[topic] {
if id == subscriber.id { if id == subscriber.id {
// sub is sync; crufty rate limiting // sub is sync; crufty rate limiting
@ -340,12 +339,18 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption)
return err return err
} }
if msg.Header == nil { m := &Message{
msg.Header = map[string]string{} Header: make(map[string]string),
Body: msg.Body,
} }
msg.Header[":topic"] = topic for k, v := range msg.Header {
b, err := json.Marshal(msg) m.Header[k] = v
}
m.Header[":topic"] = topic
b, err := json.Marshal(m)
if err != nil { if err != nil {
return err return err
} }