Skip to content

Commit

Permalink
Add health endpoint (#71)
Browse files Browse the repository at this point in the history
* Add health endpoint

* Update modules
  • Loading branch information
muXxer authored Feb 2, 2023
1 parent 16ebaa2 commit fb0517f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 123 deletions.
2 changes: 1 addition & 1 deletion core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
Name = "inx-faucet"

// Version of the app.
Version = "1.0.0-rc.1"
Version = "1.0.0-rc.2"
)

func App() *app.App {
Expand Down
2 changes: 1 addition & 1 deletion core/faucet/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func provide(c *dig.Container) error {
CoreComponent.Daemon(),
fetchMetadata,
collectOutputs,
deps.NodeBridge.IsNodeSynced,
deps.NodeBridge.IsNodeHealthy,
deps.NodeBridge.ProtocolParameters,
&faucetAddress,
faucetSigner,
Expand Down
10 changes: 10 additions & 0 deletions core/faucet/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

const (
// RouteFaucetHealth is the route to get the health info of the faucet.
RouteFaucetHealth = "/health"

// RouteFaucetInfo is the route to give info about the faucet address.
// GET returns address, balance, bech32Hrp and tokenName of the faucet.
Expand All @@ -42,6 +44,14 @@ func setupRoutes(e *echo.Echo) {

e.Group("/*").Use(frontendMiddleware())

e.GET(RouteFaucetHealth, func(c echo.Context) error {
if !deps.NodeBridge.NodeStatus().IsHealthy {
return c.NoContent(http.StatusServiceUnavailable)
}

return c.NoContent(http.StatusOK)
})

// Pass all the requests through to the local rest API
apiGroup := e.Group("/api")

Expand Down
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/iotaledger/inx-faucet
go 1.19

require (
github.com/iotaledger/hive.go/core v1.0.0-rc.2
github.com/iotaledger/hive.go/core v1.0.0-rc.2.0.20230131175029-3fe9778d3769
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1
github.com/iotaledger/hornet/v2 v2.0.0-rc.4.0.20230119143652-01c2d0d5ae4e
github.com/iotaledger/inx-app v1.0.0-rc.2
Expand All @@ -14,7 +14,7 @@ require (
github.com/stretchr/testify v1.8.1
go.uber.org/dig v1.16.1
golang.org/x/time v0.3.0
google.golang.org/grpc v1.52.0
google.golang.org/grpc v1.52.3
)

require (
Expand All @@ -24,9 +24,9 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230118173120-15945a2be184 // indirect
github.com/cockroachdb/pebble v0.0.0-20230202003038-9fc4a2088c62 // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
Expand Down Expand Up @@ -57,7 +57,7 @@ require (
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/compress v1.15.14 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -80,10 +80,10 @@ require (
github.com/multiformats/go-multiaddr v0.8.0 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multicodec v0.7.0 // indirect
github.com/multiformats/go-multicodec v0.8.0 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/gomega v1.24.1 // indirect
github.com/onsi/gomega v1.26.0 // indirect
github.com/panjf2000/ants/v2 v2.7.1 // indirect
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
Expand All @@ -107,13 +107,13 @@ require (
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157 // indirect
golang.org/x/exp v0.0.0-20230131160201-f062dba9d201 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4 // indirect
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
70 changes: 19 additions & 51 deletions go.sum

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions pkg/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/iotaledger/iota.go/v3/builder"
)

// IsNodeSyncedFunc is a function to query if the used node is synced.
type IsNodeSyncedFunc = func() bool
// IsNodeHealthyFunc is a function to query if the used node is synced.
type IsNodeHealthyFunc = func() bool

// ProtocolParamsFunc is a function to query the node for the latest protocol parameters.
type ProtocolParamsFunc = func() *iotago.ProtocolParameters
Expand Down Expand Up @@ -76,6 +76,8 @@ type pendingTransaction struct {

// InfoResponse defines the response of a GET RouteFaucetInfo REST API call.
type InfoResponse struct {
// Whether the faucet is healthy.
IsHealthy bool `json:"isHealthy"`
// The bech32 address of the faucet.
Address string `json:"address"`
// The remaining balance of faucet.
Expand Down Expand Up @@ -107,8 +109,8 @@ type Faucet struct {
blockMetadataFunc BlockMetadataFunc
// used to collect unspent outputs for a given address.
collectOutputsFunc BasicOutputsForAddressFunc
// used to determine the sync status of the node.
nodeSyncedFunc IsNodeSyncedFunc
// used to determine the health status of the node.
nodeHealthyFunc IsNodeHealthyFunc
// Protocol parameters including byte costs
protocolParamsFunc ProtocolParamsFunc
// the address of the faucet.
Expand Down Expand Up @@ -248,7 +250,7 @@ func New(
daemon daemon.Daemon,
blockMetadataFunc BlockMetadataFunc,
collectOutputsFunc BasicOutputsForAddressFunc,
nodeSyncedFunc IsNodeSyncedFunc,
nodeHealthyFunc IsNodeHealthyFunc,
protocolParamsFunc ProtocolParamsFunc,
address iotago.Address,
addressSigner iotago.AddressSigner,
Expand All @@ -263,7 +265,7 @@ func New(
daemon: daemon,
blockMetadataFunc: blockMetadataFunc,
collectOutputsFunc: collectOutputsFunc,
nodeSyncedFunc: nodeSyncedFunc,
nodeHealthyFunc: nodeHealthyFunc,
protocolParamsFunc: protocolParamsFunc,
address: address,
addressSigner: addressSigner,
Expand Down Expand Up @@ -296,6 +298,7 @@ func (f *Faucet) Info() (*InfoResponse, error) {
protocolParams := f.protocolParamsFunc()

return &InfoResponse{
IsHealthy: f.nodeHealthyFunc(),
Address: f.address.Bech32(protocolParams.Bech32HRP),
Balance: f.faucetBalance,
TokenName: f.opts.tokenName,
Expand Down Expand Up @@ -332,9 +335,9 @@ func (f *Faucet) Enqueue(bech32Addr string) (*EnqueueResponse, error) {
return nil, err
}

if !f.nodeSyncedFunc() {
if !f.nodeHealthyFunc() {
//nolint:stylecheck,revive // this error message is shown to the user
return nil, errors.WithMessage(echo.ErrInternalServerError, "Faucet node is not synchronized. Please try again later!")
return nil, errors.WithMessage(echo.ErrInternalServerError, "Faucet node is not synchronized/healthy. Please try again later!")
}

f.Lock()
Expand Down Expand Up @@ -672,13 +675,13 @@ CollectValues:
func (f *Faucet) processRequestsWithoutLocking(collectedRequestsCounter int, amount uint64, batchedRequests []*queueItem) []*queueItem {
processedBatchedRequests := []*queueItem{}
unprocessedBatchedRequests := []*queueItem{}
nodeSynced := f.nodeSyncedFunc()
nodeHealthy := f.nodeHealthyFunc()

for i := range batchedRequests {
request := batchedRequests[i]

if !nodeSynced {
// request can't be processed because the node is not synchronized => re-add it to the queue
if !nodeHealthy {
// request can't be processed because the node is not healthy => re-add it to the queue
unprocessedBatchedRequests = append(unprocessedBatchedRequests, request)

continue
Expand Down
8 changes: 4 additions & 4 deletions tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/iotaledger/inx-faucet => ../../

require (
github.com/iotaledger/hive.go/apputils v1.0.0-rc.1
github.com/iotaledger/hive.go/core v1.0.0-rc.2
github.com/iotaledger/hive.go/core v1.0.0-rc.2.0.20230131175029-3fe9778d3769
github.com/iotaledger/inx-faucet v0.0.0-00010101000000-000000000000
)

Expand All @@ -15,7 +15,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
Expand Down Expand Up @@ -82,8 +82,8 @@ require (
golang.org/x/text v0.6.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4 // indirect
google.golang.org/grpc v1.52.0 // indirect
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619 // indirect
google.golang.org/grpc v1.52.3 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit fb0517f

Please sign in to comment.