Skip to content

Commit 3494a46

Browse files
author
Michal Tichák
committed
fixup! [monitoring] fixed racecondition on Run monitoring and Stop
1 parent 37f8aa3 commit 3494a46

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

common/monitoring/monitoring.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,26 @@ func handleFunc(endpointName string) {
155155
//
156156
// If we attempt send more messages than the size of the buffer, these overflowing messages will be ignored and warning will be logged.
157157
func Run(port uint16, endpointName string) error {
158-
srv := &http.Server{Addr: fmt.Sprintf(":%d", port)}
158+
localServer := &http.Server{Addr: fmt.Sprintf(":%d", port)}
159159
// only one Run should initialize and serve
160-
if !server.CompareAndSwap(nil, srv) {
160+
if !server.CompareAndSwap(nil, localServer) {
161161
return nil
162162
}
163163
initChannels()
164164
go eventLoop()
165165
handleFunc(endpointName)
166166
// block until Shutdown is called
167-
return srv.ListenAndServe()
167+
return localServer.ListenAndServe()
168168
}
169169

170170
func Stop() {
171-
srv := server.Swap(nil)
172-
if srv == nil {
171+
localServer := server.Swap(nil)
172+
if localServer == nil {
173173
return
174174
}
175175
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
176176
defer cancel()
177-
srv.Shutdown(ctx)
177+
localServer.Shutdown(ctx)
178178
endChannel <- struct{}{}
179179
<-endChannel
180180
}

common/monitoring/monitoring_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,12 @@ func TestStartMultipleStop(t *testing.T) {
8181
Stop()
8282
}
8383

84-
func cleaningUpAfterTest() {
85-
Stop()
86-
}
87-
88-
func initTest() {
89-
go Run(12345, "notimportant")
90-
}
91-
9284
// decorator function that properly inits and cleans after higher level test of Monitoring package
9385
func testFunction(t *testing.T, testToRun func(*testing.T)) {
94-
initTest()
86+
go Run(12345, "notimportant")
9587
isRunningWithTimeout(t, time.Second)
9688
testToRun(t)
97-
cleaningUpAfterTest()
89+
Stop()
9890
}
9991

10092
func TestSendingSingleMetric(t *testing.T) {

0 commit comments

Comments
 (0)