@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"strconv"
24
24
"strings"
25
+ "sync"
25
26
"time"
26
27
27
28
"github.com/bisohns/saido/config"
@@ -81,18 +82,31 @@ type Hosts struct {
81
82
Config * config.Config
82
83
// Connections : hostname mapped to connection instances to reuse
83
84
// across metrics
85
+ mu sync.Mutex
84
86
Drivers map [string ]* driver.Driver
85
87
Client chan * Client
86
88
Start chan bool
87
89
}
88
90
91
+ func (hosts * Hosts ) getDriver (address string ) * driver.Driver {
92
+ hosts .mu .Lock ()
93
+ defer hosts .mu .Unlock ()
94
+ return hosts .Drivers [address ]
95
+ }
96
+
97
+ func (hosts * Hosts ) resetDriver (host config.Host ) {
98
+ hosts .mu .Lock ()
99
+ defer hosts .mu .Unlock ()
100
+ hostDriver := host .Connection .ToDriver ()
101
+ hosts .Drivers [host .Address ] = & hostDriver
102
+ }
103
+
89
104
func (hosts * Hosts ) sendMetric (host config.Host , client * Client ) {
90
- if hosts .Drivers [host .Address ] == nil {
91
- hostDriver := host .Connection .ToDriver ()
92
- hosts .Drivers [host .Address ] = & hostDriver
105
+ if hosts .getDriver (host .Address ) == nil {
106
+ hosts .resetDriver (host )
93
107
}
94
108
for _ , metric := range config .GetDashboardInfoConfig (hosts .Config ).Metrics {
95
- initializedMetric , err := inspector .Init (metric , hosts .Drivers [ host .Address ] )
109
+ initializedMetric , err := inspector .Init (metric , hosts .getDriver ( host .Address ) )
96
110
data , err := initializedMetric .Execute ()
97
111
if err == nil {
98
112
var unmarsh interface {}
@@ -115,7 +129,7 @@ func (hosts *Hosts) sendMetric(host config.Host, client *Client) {
115
129
errorContent = fmt .Sprintf ("Command %s not found on driver %s" , metric , host .Address )
116
130
}
117
131
log .Error (errorContent )
118
- hosts .Drivers [ host . Address ] = nil
132
+ hosts .resetDriver ( host )
119
133
message := & FullMessage {
120
134
Message : errorContent ,
121
135
Error : true ,
@@ -166,7 +180,7 @@ func newHosts(cfg *config.Config) *Hosts {
166
180
return hosts
167
181
}
168
182
169
- func SetHostHandler (w http.ResponseWriter , r * http.Request ) {
183
+ func setHostHandler (w http.ResponseWriter , r * http.Request ) {
170
184
b , _ := json .Marshal ("Hello World" )
171
185
w .Write (b )
172
186
}
@@ -179,7 +193,7 @@ var apiCmd = &cobra.Command{
179
193
// server.HandleFunc("/set-hosts", SetHostHandler)
180
194
// FIXME: set up cfg using set-hosts endpoint
181
195
hosts := newHosts (cfg )
182
- server .HandleFunc ("/set-hosts" , SetHostHandler )
196
+ server .HandleFunc ("/set-hosts" , setHostHandler )
183
197
server .Handle ("/metrics" , hosts )
184
198
log .Info ("listening on :" , port )
185
199
_ , err := strconv .Atoi (port )
0 commit comments