Fixing race condition on status serialization (#14147)

* More atomic serialization process for status

* Improving the granularity of tests on api4/status_test.go
This commit is contained in:
Jesús Espino
2020-03-27 11:40:52 +01:00
committed by GitHub
parent d92cfc851a
commit d2848cc167
2 changed files with 144 additions and 101 deletions

View File

@@ -28,10 +28,9 @@ type Status struct {
}
func (o *Status) ToJson() string {
tempChannelId := o.ActiveChannel
o.ActiveChannel = ""
b, _ := json.Marshal(o)
o.ActiveChannel = tempChannelId
oCopy := *o
oCopy.ActiveChannel = ""
b, _ := json.Marshal(oCopy)
return string(b)
}
@@ -47,18 +46,14 @@ func StatusFromJson(data io.Reader) *Status {
}
func StatusListToJson(u []*Status) string {
activeChannels := make([]string, len(u))
for index, s := range u {
activeChannels[index] = s.ActiveChannel
s.ActiveChannel = ""
}
b, _ := json.Marshal(u)
for index, s := range u {
s.ActiveChannel = activeChannels[index]
uCopy := make([]Status, len(u))
for i, s := range u {
sCopy := *s
sCopy.ActiveChannel = ""
uCopy[i] = sCopy
}
b, _ := json.Marshal(uCopy)
return string(b)
}