From a31747e6ba0c6c02461c30b64ad02ebc16eb1f2d Mon Sep 17 00:00:00 2001
From: "Ching-Hua (Vivian) Lin" <jkrvivian@gmail.com>
Date: Tue, 20 Oct 2020 11:42:01 +0800
Subject: [PATCH] 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
---
 plugins/core.go              |  2 +-
 plugins/pow/plugin.go        | 13 ++++++++++++-
 plugins/prometheus/info.go   | 12 ++++++------
 plugins/prometheus/plugin.go | 15 ++++++++++++++-
 plugins/research.go          |  2 +-
 5 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/plugins/core.go b/plugins/core.go
index ea95d45a..f304538f 100644
--- a/plugins/core.go
+++ b/plugins/core.go
@@ -36,7 +36,7 @@ var Core = node.Plugins(
 	profiling.Plugin(),
 	database.Plugin(),
 	autopeering.Plugin(),
-	pow.Plugin,
+	pow.Plugin(),
 	messagelayer.Plugin(),
 	gossip.Plugin(),
 	issuer.Plugin(),
diff --git a/plugins/pow/plugin.go b/plugins/pow/plugin.go
index 5b4f04d0..2529afc9 100644
--- a/plugins/pow/plugin.go
+++ b/plugins/pow/plugin.go
@@ -1,6 +1,8 @@
 package pow
 
 import (
+	"sync"
+
 	"github.com/iotaledger/goshimmer/packages/tangle"
 	"github.com/iotaledger/goshimmer/plugins/messagelayer"
 	"github.com/iotaledger/hive.go/logger"
@@ -12,9 +14,18 @@ const PluginName = "PoW"
 
 var (
 	// 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) {
 	// assure that the logger is available
 	log := logger.NewLogger(PluginName)
diff --git a/plugins/prometheus/info.go b/plugins/prometheus/info.go
index 3a0a479b..2c3050c7 100644
--- a/plugins/prometheus/info.go
+++ b/plugins/prometheus/info.go
@@ -8,9 +8,9 @@ import (
 )
 
 var (
-	infoApp *prometheus.GaugeVec
-	sync    prometheus.Gauge
-	nodeID  string
+	infoApp    *prometheus.GaugeVec
+	syncStatus prometheus.Gauge
+	nodeID     string
 )
 
 func registerInfoMetrics() {
@@ -26,19 +26,19 @@ func registerInfoMetrics() {
 	}
 	infoApp.WithLabelValues(banner.AppName, banner.AppVersion, nodeID).Set(1)
 
-	sync = prometheus.NewGauge(prometheus.GaugeOpts{
+	syncStatus = prometheus.NewGauge(prometheus.GaugeOpts{
 		Name: "sync",
 		Help: "Node sync status.",
 	})
 
 	registry.MustRegister(infoApp)
-	registry.MustRegister(sync)
+	registry.MustRegister(syncStatus)
 
 	addCollect(collectInfoMetrics)
 }
 
 func collectInfoMetrics() {
-	sync.Set(func() float64 {
+	syncStatus.Set(func() float64 {
 		if metrics.Synced() {
 			return 1.0
 		}
diff --git a/plugins/prometheus/plugin.go b/plugins/prometheus/plugin.go
index 1a612d87..723e6806 100644
--- a/plugins/prometheus/plugin.go
+++ b/plugins/prometheus/plugin.go
@@ -3,6 +3,7 @@ package prometheus
 import (
 	"context"
 	"net/http"
+	"sync"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -16,9 +17,13 @@ import (
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 )
 
+// PluginName is the name of the prometheus plugin.
+const PluginName = "Prometheus"
+
 // Plugin Prometheus
 var (
-	Plugin = node.NewPlugin("Prometheus", node.Disabled, configure, run)
+	plugin *node.Plugin
+	once   sync.Once
 	log    *logger.Logger
 
 	server   *http.Server
@@ -26,6 +31,14 @@ var (
 	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) {
 	log = logger.NewLogger(plugin.Name)
 
diff --git a/plugins/research.go b/plugins/research.go
index c624e49b..adf5144b 100644
--- a/plugins/research.go
+++ b/plugins/research.go
@@ -16,6 +16,6 @@ var Research = node.Plugins(
 	analysisserver.Plugin(),
 	analysisclient.Plugin(),
 	analysisdashboard.Plugin(),
-	prometheus.Plugin,
+	prometheus.Plugin(),
 	networkdelay.App(),
 )
-- 
GitLab