Skip to content

Commit 205a28f

Browse files
authored
add health check endpoint (bnb-chain#120)
1 parent 7a1262c commit 205a28f

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎internal/ethapi/api.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import (
4747
"github.com/tyler-smith/go-bip39"
4848
)
4949

50+
const UnHealthyTimeout = 5 * time.Second
51+
5052
// PublicEthereumAPI provides an API to access Ethereum related information.
5153
// It offers only methods that operate on public data that is freely available to anyone.
5254
type PublicEthereumAPI struct {
@@ -657,6 +659,13 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Ha
657659
return nil, err
658660
}
659661

662+
func (s *PublicBlockChainAPI) Health() bool {
663+
if rpc.RpcServingTimer != nil {
664+
return rpc.RpcServingTimer.Percentile(0.75) < float64(UnHealthyTimeout)
665+
}
666+
return true
667+
}
668+
660669
// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true
661670
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
662671
func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {

‎rpc/handler.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
340340
} else {
341341
successfulRequestGauge.Inc(1)
342342
}
343-
rpcServingTimer.UpdateSince(start)
343+
RpcServingTimer.UpdateSince(start)
344344
newRPCRequestGauge(msg.Method).Inc(1)
345345
newRPCServingTimer(msg.Method, answer.Error == nil).UpdateSince(start)
346346
}

‎rpc/metrics.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
rpcRequestGauge = metrics.NewRegisteredGauge("rpc/requests", nil)
2727
successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
2828
failedReqeustGauge = metrics.NewRegisteredGauge("rpc/failure", nil)
29-
rpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
29+
RpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
3030
)
3131

3232
func newRPCServingTimer(method string, valid bool) metrics.Timer {

0 commit comments

Comments
 (0)