@@ -20,28 +20,32 @@ func GetServiceResults() *map[string][]*core.Result {
2020
2121func Monitor (cfg * config.Config ) {
2222 for _ , service := range cfg .Services {
23- go func (service * core.Service ) {
24- for {
25- log .Printf ("[watchdog][Monitor] Monitoring serviceName=%s" , service .Name )
26- result := service .EvaluateConditions ()
27- metric .PublishMetricsForService (service , result )
28- rwLock .Lock ()
29- serviceResults [service .Name ] = append (serviceResults [service .Name ], result )
30- if len (serviceResults [service .Name ]) > 20 {
31- serviceResults [service .Name ] = serviceResults [service .Name ][1 :]
32- }
33- rwLock .Unlock ()
34- log .Printf (
35- "[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s" ,
36- service .Name ,
37- len (result .Errors ),
38- result .Duration .Round (time .Millisecond ),
39- )
40- log .Printf ("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s" , service .Interval , service .Name )
41- time .Sleep (service .Interval )
42- }
43- }(service )
44- // To prevent multiple requests from running exactly at the same time
45- time .Sleep (100 * time .Millisecond )
23+ go monitor (service )
24+ // To prevent multiple requests from running at the same time
25+ time .Sleep (500 * time .Millisecond )
26+ }
27+ }
28+
29+ func monitor (service * core.Service ) {
30+ for {
31+ // By placing the lock here, we prevent multiple services from being monitored at the exact same time, which
32+ // could cause performance issues and return inaccurate results
33+ rwLock .Lock ()
34+ log .Printf ("[watchdog][Monitor] Monitoring serviceName=%s" , service .Name )
35+ result := service .EvaluateConditions ()
36+ metric .PublishMetricsForService (service , result )
37+ serviceResults [service .Name ] = append (serviceResults [service .Name ], result )
38+ if len (serviceResults [service .Name ]) > 20 {
39+ serviceResults [service .Name ] = serviceResults [service .Name ][1 :]
40+ }
41+ rwLock .Unlock ()
42+ log .Printf (
43+ "[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s" ,
44+ service .Name ,
45+ len (result .Errors ),
46+ result .Duration .Round (time .Millisecond ),
47+ )
48+ log .Printf ("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s" , service .Interval , service .Name )
49+ time .Sleep (service .Interval )
4650 }
4751}
0 commit comments