forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication_manager.go
114 lines (102 loc) · 3.07 KB
/
communication_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package engine
import (
"fmt"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/communications"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/log"
)
// CommunicationsManagerName is an exported subsystem name
const CommunicationsManagerName = "communications"
// CommunicationManager ensures operations of communications
type CommunicationManager struct {
started int32
shutdown chan struct{}
relayMsg chan base.Event
comms *communications.Communications
}
// SetupCommunicationManager creates a communications manager
func SetupCommunicationManager(cfg *base.CommunicationsConfig) (*CommunicationManager, error) {
if cfg == nil {
return nil, errNilConfig
}
manager := &CommunicationManager{
shutdown: make(chan struct{}),
relayMsg: make(chan base.Event),
}
var err error
manager.comms, err = communications.NewComm(cfg)
if err != nil {
return nil, err
}
return manager, nil
}
// IsRunning safely checks whether the subsystem is running
func (m *CommunicationManager) IsRunning() bool {
if m == nil {
return false
}
return atomic.LoadInt32(&m.started) == 1
}
// Start runs the subsystem
func (m *CommunicationManager) Start() error {
if m == nil {
return fmt.Errorf("communications manager server %w", ErrNilSubsystem)
}
if !atomic.CompareAndSwapInt32(&m.started, 0, 1) {
return fmt.Errorf("communications manager %w", ErrSubSystemAlreadyStarted)
}
log.Debugf(log.CommunicationMgr, "Communications manager %s", MsgSubSystemStarting)
m.shutdown = make(chan struct{})
go m.run()
return nil
}
// GetStatus returns the status of communications
func (m *CommunicationManager) GetStatus() (map[string]base.CommsStatus, error) {
if !m.IsRunning() {
return nil, fmt.Errorf("communications manager %w", ErrSubSystemNotStarted)
}
return m.comms.GetStatus(), nil
}
// Stop attempts to shutdown the subsystem
func (m *CommunicationManager) Stop() error {
if m == nil {
return fmt.Errorf("communications manager server %w", ErrNilSubsystem)
}
if atomic.LoadInt32(&m.started) == 0 {
return fmt.Errorf("communications manager %w", ErrSubSystemNotStarted)
}
defer func() {
atomic.CompareAndSwapInt32(&m.started, 1, 0)
}()
close(m.shutdown)
log.Debugf(log.CommunicationMgr, "Communications manager %s", MsgSubSystemShuttingDown)
return nil
}
// PushEvent pushes an event to the communications relay
func (m *CommunicationManager) PushEvent(evt base.Event) {
if !m.IsRunning() {
return
}
select {
case m.relayMsg <- evt:
default:
log.Errorf(log.CommunicationMgr, "Failed to send, no receiver when pushing event [%v]", evt)
}
}
// run takes awaiting messages and pushes them to be handled by communications
func (m *CommunicationManager) run() {
log.Debugf(log.Global, "Communications manager %s", MsgSubSystemStarted)
defer func() {
// TO-DO shutdown comms connections for connected services (Slack etc)
log.Debugf(log.CommunicationMgr, "Communications manager %s", MsgSubSystemShutdown)
}()
for {
select {
case msg := <-m.relayMsg:
m.comms.PushEvent(msg)
case <-m.shutdown:
return
}
}
}