Skip to content

Commit

Permalink
Fix race in StreamsList()
Browse files Browse the repository at this point in the history
This resolves issue number 1 of #294.
  • Loading branch information
Robertof committed Feb 20, 2023
1 parent 82a88e1 commit db8a796
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 6 additions & 1 deletion apiHTTPStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (

//HTTPAPIServerStreams function return stream list
func HTTPAPIServerStreams(c *gin.Context) {
c.IndentedJSON(200, Message{Status: 1, Payload: Storage.StreamsList()})
list, err := Storage.MarshalledStreamsList()
if err != nil {
c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
return
}
c.IndentedJSON(200, Message{Status: 1, Payload: list})
}

//HTTPAPIServerStreamsMultiControlAdd function add new stream's
Expand Down
16 changes: 10 additions & 6 deletions storageStream.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package main

//StreamsList list all stream
func (obj *StorageST) StreamsList() map[string]StreamST {
import "github.com/liip/sheriff"

//MarshalledStreamsList lists all streams and includes only fields which are safe to serialize.
func (obj *StorageST) MarshalledStreamsList() (interface{}, error) {
obj.mutex.RLock()
defer obj.mutex.RUnlock()
tmp := make(map[string]StreamST)
for i, i2 := range obj.Streams {
tmp[i] = i2
val, err := sheriff.Marshal(&sheriff.Options{
Groups: []string{"api"},
}, obj.Streams)
if err != nil {
return nil, err
}
return tmp
return val, nil
}

//StreamAdd add stream
Expand Down

0 comments on commit db8a796

Please sign in to comment.