|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package probe |
| 24 | + |
| 25 | +import ( |
| 26 | + "net/http" |
| 27 | + "sync" |
| 28 | + "time" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + livenessHandlerTimeout = time.Second * 5 |
| 33 | +) |
| 34 | + |
| 35 | +// LivenessProbe wraps a liveness probe handler. |
| 36 | +type LivenessProbe struct { |
| 37 | + lock int32 |
| 38 | + mutex sync.Mutex |
| 39 | + waitChan chan struct{} |
| 40 | +} |
| 41 | + |
| 42 | +// Lock the probe, preventing the LivenessHandler from responding to requests. |
| 43 | +func (p *LivenessProbe) Lock() { |
| 44 | + p.mutex.Lock() |
| 45 | + defer p.mutex.Unlock() |
| 46 | + |
| 47 | + p.lock++ |
| 48 | +} |
| 49 | + |
| 50 | +// Unlock the probe, allowing the LivenessHandler to respond to requests. |
| 51 | +func (p *LivenessProbe) Unlock() { |
| 52 | + p.mutex.Lock() |
| 53 | + defer p.mutex.Unlock() |
| 54 | + |
| 55 | + p.lock-- |
| 56 | + |
| 57 | + if p.lock == 0 && p.waitChan != nil { |
| 58 | + w := p.waitChan |
| 59 | + p.waitChan = nil |
| 60 | + close(w) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// waitUntilNotLocked blocks until the probe is no longer locked |
| 65 | +// or a timeout occurs. |
| 66 | +// Returns true if the probe is unlocked, false on timeout. |
| 67 | +func (p *LivenessProbe) waitUntilNotLocked(timeout time.Duration) bool { |
| 68 | + deadline := time.Now().Add(timeout) |
| 69 | + for { |
| 70 | + var w chan struct{} |
| 71 | + p.mutex.Lock() |
| 72 | + locked := p.lock != 0 |
| 73 | + if locked { |
| 74 | + if p.waitChan == nil { |
| 75 | + p.waitChan = make(chan struct{}) |
| 76 | + } |
| 77 | + w = p.waitChan |
| 78 | + } |
| 79 | + p.mutex.Unlock() |
| 80 | + if !locked { |
| 81 | + // All good |
| 82 | + return true |
| 83 | + } |
| 84 | + // We're locked, wait until w is closed |
| 85 | + select { |
| 86 | + case <-w: |
| 87 | + // continue |
| 88 | + case <-time.After(time.Until(deadline)): |
| 89 | + // Timeout |
| 90 | + return false |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// LivenessHandler writes back the HTTP status code 200 if the operator is ready, and 500 otherwise. |
| 96 | +func (p *LivenessProbe) LivenessHandler(w http.ResponseWriter, r *http.Request) { |
| 97 | + if p.waitUntilNotLocked(livenessHandlerTimeout) { |
| 98 | + w.WriteHeader(http.StatusOK) |
| 99 | + } else { |
| 100 | + w.WriteHeader(http.StatusInternalServerError) |
| 101 | + } |
| 102 | +} |
0 commit comments