...

Source file src/go.etcd.io/etcd/server/v3/proxy/grpcproxy/health.go

Documentation: go.etcd.io/etcd/server/v3/proxy/grpcproxy

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grpcproxy
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"time"
    22  
    23  	"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
    24  	"go.etcd.io/etcd/client/v3"
    25  	"go.etcd.io/etcd/server/v3/etcdserver/api/etcdhttp"
    26  	"go.uber.org/zap"
    27  )
    28  
    29  // HandleHealth registers health handler on '/health'.
    30  func HandleHealth(lg *zap.Logger, mux *http.ServeMux, c *clientv3.Client) {
    31  	if lg == nil {
    32  		lg = zap.NewNop()
    33  	}
    34  	mux.Handle(etcdhttp.PathHealth, etcdhttp.NewHealthHandler(lg, func(ctx context.Context, excludedAlarms etcdhttp.StringSet, serializable bool) etcdhttp.Health {
    35  		return checkHealth(c)
    36  	}))
    37  }
    38  
    39  // HandleProxyHealth registers health handler on '/proxy/health'.
    40  func HandleProxyHealth(lg *zap.Logger, mux *http.ServeMux, c *clientv3.Client) {
    41  	if lg == nil {
    42  		lg = zap.NewNop()
    43  	}
    44  	mux.Handle(etcdhttp.PathProxyHealth, etcdhttp.NewHealthHandler(lg, func(ctx context.Context, excludedAlarms etcdhttp.StringSet, serializable bool) etcdhttp.Health {
    45  		return checkProxyHealth(c)
    46  	}))
    47  }
    48  
    49  func checkHealth(c *clientv3.Client) etcdhttp.Health {
    50  	h := etcdhttp.Health{Health: "false"}
    51  	ctx, cancel := context.WithTimeout(c.Ctx(), time.Second)
    52  	_, err := c.Get(ctx, "a")
    53  	cancel()
    54  	if err == nil || err == rpctypes.ErrPermissionDenied {
    55  		h.Health = "true"
    56  	} else {
    57  		h.Reason = fmt.Sprintf("GET ERROR:%s", err)
    58  	}
    59  	return h
    60  }
    61  
    62  func checkProxyHealth(c *clientv3.Client) etcdhttp.Health {
    63  	if c == nil {
    64  		return etcdhttp.Health{Health: "false", Reason: "no connection to proxy"}
    65  	}
    66  	h := checkHealth(c)
    67  	if h.Health != "true" {
    68  		return h
    69  	}
    70  	ctx, cancel := context.WithTimeout(c.Ctx(), time.Second*3)
    71  	ch := c.Watch(ctx, "a", clientv3.WithCreatedNotify())
    72  	select {
    73  	case <-ch:
    74  	case <-ctx.Done():
    75  		h.Health = "false"
    76  		h.Reason = "WATCH TIMEOUT"
    77  	}
    78  	cancel()
    79  	return h
    80  }
    81  

View as plain text