mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Clone header before creating exporter HTTP request
Behind http.Header is just a map and it's not safe for concurrent use. Before this change, a panic might have occurred when doing asynchronous HTTP exports (applies to both *http_post and *http_json_map exporters). Cloning the header before adding it to the HTTP request has fixed this issue. Slightly improved the test that found this data race.
This commit is contained in:
committed by
Dan Christian Bogos
parent
1a66dd52dd
commit
bd3e18754b
@@ -99,7 +99,7 @@ func (httpEE *HTTPjsonMapEE) ExtraData(ev *utils.CGREvent) any { return nil }
|
||||
func (httpEE *HTTPjsonMapEE) PrepareMap(mp *utils.CGREvent) (any, error) {
|
||||
body, err := json.Marshal(mp.Event)
|
||||
return &HTTPPosterRequest{
|
||||
Header: httpEE.hdr,
|
||||
Header: httpEE.hdr.Clone(),
|
||||
Body: body,
|
||||
}, err
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func (httpEE *HTTPjsonMapEE) PrepareOrderMap(mp *utils.OrderedNavigableMap) (any
|
||||
}
|
||||
body, err := json.Marshal(valMp)
|
||||
return &HTTPPosterRequest{
|
||||
Header: httpEE.hdr,
|
||||
Header: httpEE.hdr.Clone(),
|
||||
Body: body,
|
||||
}, err
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func (httpPost *HTTPPostEE) PrepareMap(mp *utils.CGREvent) (any, error) {
|
||||
urlVals.Set(k, utils.IfaceAsString(v))
|
||||
}
|
||||
return &HTTPPosterRequest{
|
||||
Header: httpPost.hdr,
|
||||
Header: httpPost.hdr.Clone(),
|
||||
Body: urlVals,
|
||||
}, nil
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func (httpPost *HTTPPostEE) PrepareOrderMap(mp *utils.OrderedNavigableMap) (any,
|
||||
urlVals.Set(strings.Join(path, utils.NestingSep), nmIt.String())
|
||||
}
|
||||
return &HTTPPosterRequest{
|
||||
Header: httpPost.hdr,
|
||||
Header: httpPost.hdr.Clone(),
|
||||
Body: urlVals,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -127,20 +127,39 @@ func TestHttpPostSync(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mp := &utils.CGREvent{
|
||||
req1, err := exp.PrepareMap(&utils.CGREvent{
|
||||
Event: map[string]any{
|
||||
"Account": "1001",
|
||||
"Destination": "1002",
|
||||
},
|
||||
}
|
||||
|
||||
vals, err := exp.PrepareMap(mp)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
go exp.ExportEvent(context.Background(), vals, "")
|
||||
req2, err := exp.PrepareMap(&utils.CGREvent{
|
||||
Event: map[string]any{
|
||||
"Account": "1001",
|
||||
"Destination": "1003",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req3, err := exp.PrepareMap(&utils.CGREvent{
|
||||
Event: map[string]any{
|
||||
"Account": "1003",
|
||||
"Destination": "1001",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
requests := []any{req1, req2, req3}
|
||||
|
||||
for i := range 3 {
|
||||
go exp.ExportEvent(context.Background(), requests[i], "")
|
||||
}
|
||||
|
||||
select {
|
||||
|
||||
Reference in New Issue
Block a user