Skip to content
Snippets Groups Projects
Commit 468b221f authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Use a sync map

parent a6bd16fa
No related branches found
No related tags found
3 merge requests!23Release,!13Hotfix/no host cooldown + return sende2e TS,!11Client Event Reporting API
...@@ -35,14 +35,12 @@ func (e reportableEvent) String() string { ...@@ -35,14 +35,12 @@ func (e reportableEvent) String() string {
// Holds state for the event reporting system // Holds state for the event reporting system
type eventManager struct { type eventManager struct {
eventCh chan reportableEvent eventCh chan reportableEvent
eventCbs []EventCallbackFunction eventCbs sync.Map
eventLck sync.Mutex
} }
func newEventManager() *eventManager { func newEventManager() *eventManager {
return &eventManager{ return &eventManager{
eventCh: make(chan reportableEvent, 1000), eventCh: make(chan reportableEvent, 1000),
eventCbs: make([]EventCallbackFunction, 0),
} }
} }
...@@ -67,25 +65,20 @@ func (e *eventManager) ReportEvent(priority int, category, evtType, ...@@ -67,25 +65,20 @@ func (e *eventManager) ReportEvent(priority int, category, evtType,
// RegisterEventCallback records the given function to receive // RegisterEventCallback records the given function to receive
// ReportableEvent objects. It returns the internal index // ReportableEvent objects. It returns the internal index
// of the callback so that it can be deleted later. // of the callback so that it can be deleted later.
func (e *eventManager) RegisterEventCallback(myFunc EventCallbackFunction) int { func (e *eventManager) RegisterEventCallback(name string,
e.eventLck.Lock() myFunc EventCallbackFunction) error {
defer e.eventLck.Unlock() _, existsAlready := e.eventCbs.LoadOrStore(name, myFunc)
e.eventCbs = append(e.eventCbs, myFunc) if existsAlready {
return len(e.eventCbs) - 1 return errors.Errorf("Key %s already exists as event callback",
name)
}
return nil
} }
// UnregisterEventCallback deletes the callback identified by the // UnregisterEventCallback deletes the callback identified by the
// index. It returns an error if it fails. // index. It returns an error if it fails.
func (e *eventManager) UnregisterEventCallback(index int) error { func (e *eventManager) UnregisterEventCallback(name string) {
e.eventLck.Lock() e.eventCbs.Delete(name)
defer e.eventLck.Unlock()
if index > 0 && index < len(e.eventCbs) {
e.eventCbs = append(e.eventCbs[:index], e.eventCbs[index+1:]...)
} else {
return errors.Errorf("Index %d out of bounds: %d -> %d",
index, 0, len(e.eventCbs))
}
return nil
} }
func (e *eventManager) eventService() (stoppable.Stoppable, error) { func (e *eventManager) eventService() (stoppable.Stoppable, error) {
...@@ -109,10 +102,12 @@ func (e *eventManager) reportEventsHandler(stop *stoppable.Single) { ...@@ -109,10 +102,12 @@ func (e *eventManager) reportEventsHandler(stop *stoppable.Single) {
// against it. It's the users responsibility not to let // against it. It's the users responsibility not to let
// the event queue explode. The API will report errors // the event queue explode. The API will report errors
// in the logging any time the event queue gets full. // in the logging any time the event queue gets full.
for i := 0; i < len(e.eventCbs); i++ { e.eventCbs.Range(func(name, myFunc interface{}) bool {
e.eventCbs[i](evt.Priority, evt.Category, f := myFunc.(EventCallbackFunction)
evt.EventType, evt.Details) f(evt.Priority, evt.Category, evt.EventType,
} evt.Details)
return true
})
} }
} }
} }
...@@ -126,12 +121,13 @@ func (c *Client) ReportEvent(priority int, category, evtType, details string) { ...@@ -126,12 +121,13 @@ func (c *Client) ReportEvent(priority int, category, evtType, details string) {
// RegisterEventCallback records the given function to receive // RegisterEventCallback records the given function to receive
// ReportableEvent objects. It returns the internal index // ReportableEvent objects. It returns the internal index
// of the callback so that it can be deleted later. // of the callback so that it can be deleted later.
func (c *Client) RegisterEventCallback(myFunc EventCallbackFunction) int { func (c *Client) RegisterEventCallback(name string,
return c.events.RegisterEventCallback(myFunc) myFunc EventCallbackFunction) error {
return c.events.RegisterEventCallback(name, myFunc)
} }
// UnregisterEventCallback deletes the callback identified by the // UnregisterEventCallback deletes the callback identified by the
// index. It returns an error if it fails. // index. It returns an error if it fails.
func (c *Client) UnregisterEventCallback(index int) error { func (c *Client) UnregisterEventCallback(name string) {
return c.events.UnregisterEventCallback(index) c.events.UnregisterEventCallback(name)
} }
...@@ -14,12 +14,13 @@ import ( ...@@ -14,12 +14,13 @@ import (
// RegisterEventCallback records the given function to receive // RegisterEventCallback records the given function to receive
// ReportableEvent objects. It returns the internal index // ReportableEvent objects. It returns the internal index
// of the callback so that it can be deleted later. // of the callback so that it can be deleted later.
func (c *Client) RegisterEventCallback(myFunc api.EventCallbackFunction) int { func (c *Client) RegisterEventCallback(name string,
return c.api.RegisterEventCallback(myFunc) myFunc api.EventCallbackFunction) error {
return c.api.RegisterEventCallback(name, myFunc)
} }
// UnregisterEventCallback deletes the callback identified by the // UnregisterEventCallback deletes the callback identified by the
// index. It returns an error if it fails. // index. It returns an error if it fails.
func (c *Client) UnregisterEventCallback(index int) error { func (c *Client) UnregisterEventCallback(name string) {
return c.api.UnregisterEventCallback(index) c.api.UnregisterEventCallback(name)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment