Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions common/monitoring/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"context"
"fmt"
"net/http"
"sync/atomic"
"time"

"github.com/AliceO2Group/Control/common/logger"
Expand All @@ -38,8 +39,8 @@ import (
)

var (
// scraping endpoint implementation
server *http.Server
// atomic holder for the HTTP server instance
server atomic.Pointer[http.Server]
// objects to store incoming metrics
metricsInternal *MetricsAggregate
metricsHistogramInternal *MetricsReservoirSampling
Expand Down Expand Up @@ -154,33 +155,30 @@ func handleFunc(endpointName string) {
//
// If we attempt send more messages than the size of the buffer, these overflowing messages will be ignored and warning will be logged.
func Run(port uint16, endpointName string) error {
if IsRunning() {
srv := &http.Server{Addr: fmt.Sprintf(":%d", port)}
// only one Run should initialize and serve
if !server.CompareAndSwap(nil, srv) {
Comment thread
justonedev1 marked this conversation as resolved.
Outdated
return nil
}

initChannels()

go eventLoop()

server = &http.Server{Addr: fmt.Sprintf(":%d", port)}
handleFunc(endpointName)
return server.ListenAndServe()
// block until Shutdown is called
return srv.ListenAndServe()
Comment thread
knopers8 marked this conversation as resolved.
Outdated
}

func Stop() {
if !IsRunning() {
srv := server.Swap(nil)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please apply the same renaming in Stop

if srv == nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)

srv.Shutdown(ctx)
endChannel <- struct{}{}
<-endChannel
server = nil
}

func IsRunning() bool {
return server != nil
return server.Load() != nil
}
Loading