Skip to content
Snippets Groups Projects
Unverified Commit a31747e6 authored by Ching-Hua (Vivian) Lin's avatar Ching-Hua (Vivian) Lin Committed by GitHub
Browse files

feat: Get instances of singletons via functions instead of global variables (#804)

* feat: Get instances of singletons via functions instead of global variables

* fix: Fix comments
parent 248d503a
No related branches found
No related tags found
No related merge requests found
...@@ -36,7 +36,7 @@ var Core = node.Plugins( ...@@ -36,7 +36,7 @@ var Core = node.Plugins(
profiling.Plugin(), profiling.Plugin(),
database.Plugin(), database.Plugin(),
autopeering.Plugin(), autopeering.Plugin(),
pow.Plugin, pow.Plugin(),
messagelayer.Plugin(), messagelayer.Plugin(),
gossip.Plugin(), gossip.Plugin(),
issuer.Plugin(), issuer.Plugin(),
......
package pow package pow
import ( import (
"sync"
"github.com/iotaledger/goshimmer/packages/tangle" "github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
...@@ -12,9 +14,18 @@ const PluginName = "PoW" ...@@ -12,9 +14,18 @@ const PluginName = "PoW"
var ( var (
// Plugin is the plugin instance of the PoW plugin. // Plugin is the plugin instance of the PoW plugin.
Plugin = node.NewPlugin(PluginName, node.Enabled, run) plugin *node.Plugin
once sync.Once
) )
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, run)
})
return plugin
}
func run(*node.Plugin) { func run(*node.Plugin) {
// assure that the logger is available // assure that the logger is available
log := logger.NewLogger(PluginName) log := logger.NewLogger(PluginName)
......
...@@ -8,9 +8,9 @@ import ( ...@@ -8,9 +8,9 @@ import (
) )
var ( var (
infoApp *prometheus.GaugeVec infoApp *prometheus.GaugeVec
sync prometheus.Gauge syncStatus prometheus.Gauge
nodeID string nodeID string
) )
func registerInfoMetrics() { func registerInfoMetrics() {
...@@ -26,19 +26,19 @@ func registerInfoMetrics() { ...@@ -26,19 +26,19 @@ func registerInfoMetrics() {
} }
infoApp.WithLabelValues(banner.AppName, banner.AppVersion, nodeID).Set(1) infoApp.WithLabelValues(banner.AppName, banner.AppVersion, nodeID).Set(1)
sync = prometheus.NewGauge(prometheus.GaugeOpts{ syncStatus = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sync", Name: "sync",
Help: "Node sync status.", Help: "Node sync status.",
}) })
registry.MustRegister(infoApp) registry.MustRegister(infoApp)
registry.MustRegister(sync) registry.MustRegister(syncStatus)
addCollect(collectInfoMetrics) addCollect(collectInfoMetrics)
} }
func collectInfoMetrics() { func collectInfoMetrics() {
sync.Set(func() float64 { syncStatus.Set(func() float64 {
if metrics.Synced() { if metrics.Synced() {
return 1.0 return 1.0
} }
......
...@@ -3,6 +3,7 @@ package prometheus ...@@ -3,6 +3,7 @@ package prometheus
import ( import (
"context" "context"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
...@@ -16,9 +17,13 @@ import ( ...@@ -16,9 +17,13 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
// PluginName is the name of the prometheus plugin.
const PluginName = "Prometheus"
// Plugin Prometheus // Plugin Prometheus
var ( var (
Plugin = node.NewPlugin("Prometheus", node.Disabled, configure, run) plugin *node.Plugin
once sync.Once
log *logger.Logger log *logger.Logger
server *http.Server server *http.Server
...@@ -26,6 +31,14 @@ var ( ...@@ -26,6 +31,14 @@ var (
collects []func() collects []func()
) )
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Disabled, configure, run)
})
return plugin
}
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger(plugin.Name) log = logger.NewLogger(plugin.Name)
......
...@@ -16,6 +16,6 @@ var Research = node.Plugins( ...@@ -16,6 +16,6 @@ var Research = node.Plugins(
analysisserver.Plugin(), analysisserver.Plugin(),
analysisclient.Plugin(), analysisclient.Plugin(),
analysisdashboard.Plugin(), analysisdashboard.Plugin(),
prometheus.Plugin, prometheus.Plugin(),
networkdelay.App(), networkdelay.App(),
) )
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment