Skip to content

Commit

Permalink
chore: use atomic.Bool for the ready flag
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Nussbaumer <clement.nussbaumer@postfinance.ch>
  • Loading branch information
clementnuss committed Jul 22, 2024
1 parent e5e0c55 commit f64d442
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
7 changes: 2 additions & 5 deletions internal/kubenurse/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (

func (s *Server) readyHandler() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()

if s.ready {
if s.ready.Load() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusServiceUnavailable)
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions internal/kubenurse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/postfinance/kubenurse/internal/servicecheck"
Expand All @@ -34,9 +35,7 @@ type Server struct {
// If we want to consider kubenurses on unschedulable nodes
allowUnschedulable bool

// Mutex to protect ready flag
mu *sync.Mutex
ready bool
ready atomic.Bool

// Neighbourhood incoming checks
neighbouringIncomingChecks prometheus.Gauge
Expand Down Expand Up @@ -94,10 +93,10 @@ func New(ctx context.Context, c client.Client) (*Server, error) { //nolint:funle
useTLS: os.Getenv("KUBENURSE_USE_TLS") == "true",
allowUnschedulable: os.Getenv("KUBENURSE_ALLOW_UNSCHEDULABLE") == "true",
checkInterval: checkInterval,
mu: new(sync.Mutex),
ready: true,
ready: atomic.Bool{},
}

server.ready.Store(true)
server.neighboursTTLCache.Init(60 * time.Second)

promRegistry := prometheus.NewRegistry()
Expand Down Expand Up @@ -261,9 +260,7 @@ func (s *Server) Run() error {

// Shutdown disables the readiness probe and then gracefully halts the kubenurse http/https server(s).
func (s *Server) Shutdown(ctx context.Context) error {
s.mu.Lock()
s.ready = false
s.mu.Unlock()
s.ready.Store(false)

// wait before actually shutting down the http/s server, as the updated
// endpoints for the kubenurse service might not have propagated everywhere
Expand Down

0 comments on commit f64d442

Please sign in to comment.