This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from jcooklin/fb/enables-streaming
Enables streaming collectors
- Loading branch information
Showing
12 changed files
with
303 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package util | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin/rpc" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type streamsMgr struct { | ||
*sync.Mutex | ||
collection map[rpc.StreamCollector_StreamMetricsServer]context.CancelFunc | ||
} | ||
|
||
func New() *streamsMgr { | ||
return &streamsMgr{ | ||
Mutex: &sync.Mutex{}, | ||
collection: make(map[rpc.StreamCollector_StreamMetricsServer]context.CancelFunc), | ||
} | ||
} | ||
|
||
func (s *streamsMgr) Add(stream rpc.StreamCollector_StreamMetricsServer, cancel context.CancelFunc) { | ||
s.Lock() | ||
defer s.Unlock() | ||
s.collection[stream] = cancel | ||
} | ||
|
||
func (s *streamsMgr) RemoveAndCancel(stream rpc.StreamCollector_StreamMetricsServer) error { | ||
s.Lock() | ||
defer s.Unlock() | ||
cancel, ok := s.collection[stream] | ||
if !ok { | ||
return errors.New("stream not found") | ||
} | ||
cancel() | ||
delete(s.collection, stream) | ||
return nil | ||
} | ||
|
||
func (s *streamsMgr) GetAll() []rpc.StreamCollector_StreamMetricsServer { | ||
s.Lock() | ||
defer s.Unlock() | ||
keys := make([]rpc.StreamCollector_StreamMetricsServer, len(s.collection)) | ||
i := 0 | ||
for k := range s.collection { | ||
keys[i] = k | ||
i++ | ||
} | ||
return keys | ||
} | ||
|
||
func (s *streamsMgr) Count() int { | ||
s.Lock() | ||
defer s.Unlock() | ||
return len(s.collection) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.