github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/healthcheck.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  // Healthchecks hold an error value describing an arbitrary up/down status.
    21  type Healthcheck interface {
    22  	Check()
    23  	Error() error
    24  	Healthy()
    25  	Unhealthy(error)
    26  }
    27  
    28  // NewHealthcheck constructs a new Healthcheck which will use the given
    29  // function to update its status.
    30  func NewHealthcheck(f func(Healthcheck)) Healthcheck {
    31  	if !Enabled {
    32  		return NilHealthcheck{}
    33  	}
    34  	return &StandardHealthcheck{nil, f}
    35  }
    36  
    37  // NilHealthcheck is a no-op.
    38  type NilHealthcheck struct{}
    39  
    40  // Check is a no-op.
    41  func (NilHealthcheck) Check() {}
    42  
    43  // Error is a no-op.
    44  func (NilHealthcheck) Error() error { return nil }
    45  
    46  // Healthy is a no-op.
    47  func (NilHealthcheck) Healthy() {}
    48  
    49  // Unhealthy is a no-op.
    50  func (NilHealthcheck) Unhealthy(error) {}
    51  
    52  // StandardHealthcheck is the standard implementation of a Healthcheck and
    53  // stores the status and a function to call to update the status.
    54  type StandardHealthcheck struct {
    55  	err error
    56  	f   func(Healthcheck)
    57  }
    58  
    59  // Check runs the healthcheck function to update the healthcheck's status.
    60  func (h *StandardHealthcheck) Check() {
    61  	h.f(h)
    62  }
    63  
    64  // Error returns the healthcheck's status, which will be nil if it is healthy.
    65  func (h *StandardHealthcheck) Error() error {
    66  	return h.err
    67  }
    68  
    69  // Healthy marks the healthcheck as healthy.
    70  func (h *StandardHealthcheck) Healthy() {
    71  	h.err = nil
    72  }
    73  
    74  // Unhealthy marks the healthcheck as unhealthy.  The error is stored and
    75  // may be retrieved by the Error method.
    76  func (h *StandardHealthcheck) Unhealthy(err error) {
    77  	h.err = err
    78  }