-
Notifications
You must be signed in to change notification settings - Fork 114
/
plugin.go
56 lines (45 loc) · 1.17 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package healthz
import (
"net/http"
goSync "sync"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/syncbeaconfollower"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/node"
"github.com/labstack/echo"
)
// PluginName is the name of the web API healthz endpoint plugin.
const PluginName = "WebAPI healthz Endpoint"
var (
// plugin is the plugin instance of the web API info endpoint plugin.
plugin *node.Plugin
once goSync.Once
)
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, configure)
})
return plugin
}
func configure(_ *node.Plugin) {
webapi.Server().GET("healthz", getHealthz)
}
func getHealthz(c echo.Context) error {
if !IsNodeHealthy() {
return c.NoContent(http.StatusServiceUnavailable)
}
return c.NoContent(http.StatusOK)
}
// IsNodeHealthy returns whether the node is synced, has active neighbors.
func IsNodeHealthy() bool {
// Synced
if !syncbeaconfollower.Synced() {
return false
}
// Has connected neighbors
if len(gossip.Manager().AllNeighbors()) == 0 {
return false
}
return true
}