Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
probes: Make Probes an interface
Browse files Browse the repository at this point in the history
In preparation for a "do nothing" listener.
  • Loading branch information
mjs committed May 1, 2018
1 parent 0461ccf commit e2b485e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions probes/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ import (
"sync/atomic"
)

// Probes defines the available operations for a probes listener.
type Probes interface {
SetAlive(bool)
SetReady(bool)
Close()
}

// Listen starts a simple HTTP listener for responding to Kubernetes
// liveness and readiness probes on the port specified. The returned
// Probes instance has methods for setting the liveness and readiness
// states.
//
// Liveness probes are served at /healthz.
// Readiness probes are served at /readyz.
func Listen(port int) *Probes {
p := &Probes{
func Listen(port int) Probes {
p := &listener{
alive: new(atomic.Value),
ready: new(atomic.Value),
server: &http.Server{
Expand All @@ -55,28 +62,26 @@ func Listen(port int) *Probes {
return p
}

// Probes contains a simple HTTP listener for serving Kubernetes
// liveness and readiness probes.
type Probes struct {
type listener struct {
alive *atomic.Value
ready *atomic.Value
server *http.Server
wg sync.WaitGroup
}

// SetAlive set the liveness state - true means alive/healthy.
func (p *Probes) SetAlive(alive bool) {
func (p *listener) SetAlive(alive bool) {
p.alive.Store(alive)
}

// SetReady set the readiness state - true means ready.
func (p *Probes) SetReady(ready bool) {
func (p *listener) SetReady(ready bool) {
p.ready.Store(ready)
}

// Close shuts down the probes listener. It blocks until the listener
// has stopped.
func (p *Probes) Close() {
func (p *listener) Close() {
p.server.Close()
p.wg.Wait()
}
Expand Down

0 comments on commit e2b485e

Please sign in to comment.