Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the implicit use of init functions #173

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package config
import (
"strings"

"github.com/Conflux-Chain/confura/node"
"github.com/Conflux-Chain/confura/rpc"
"github.com/Conflux-Chain/confura/store"
"github.com/Conflux-Chain/confura/util/alert"
"github.com/Conflux-Chain/confura/util/blacklist"
"github.com/Conflux-Chain/confura/util/metrics"
"github.com/Conflux-Chain/confura/util/pprof"
rpcutil "github.com/Conflux-Chain/confura/util/rpc"
"github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/ethereum/go-ethereum/log"
"github.com/pkg/errors"
Expand All @@ -21,17 +26,26 @@ import (
// eg., `INFURA_LOG_LEVEL` will override "log.level" config item from the config file.
const viperEnvPrefix = "infura"

func init() {
func Init() {
// init viper
viper.MustInit(viperEnvPrefix)
// init logger
initLogger()
// init pprof
pprof.MustInit()
// init metrics
metrics.Init()
metrics.MustInit()
// init alert
alert.InitDingRobot()
// init misc util
rpcutil.MustInit()
blacklist.MustInit()
// init store
store.MustInit()
// init node
node.MustInit()
// init rpc
rpc.MustInit()
}

func initLogger() {
Expand Down
9 changes: 2 additions & 7 deletions config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ var (
GitCommit string

BuildDate string
BuildOS string
BuildArch string
)

func init() {
BuildOS = runtime.GOOS
BuildOS = runtime.GOOS
BuildArch = runtime.GOARCH
}
)

func DumpVersionInfo() {
strFormat := "%-12v%v\n"
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/confura/cmd"
"github.com/Conflux-Chain/confura/config"
)

func main() {
// ensure configuration initialized at first.
config.Init()

cmd.Execute()
}
5 changes: 1 addition & 4 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package node
import (
"time"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/buraksezer/consistent"
"github.com/sirupsen/logrus"
Expand All @@ -17,7 +14,7 @@ var cfg config
var urlCfg map[Group]UrlConfig
var ethUrlCfg map[Group]UrlConfig

func init() {
func MustInit() {
viper.MustUnmarshalKey("node", &cfg)
logrus.WithField("config", cfg).Debug("Node manager configurations loaded.")

Expand Down
2 changes: 1 addition & 1 deletion rpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
metricPrefixInfura = "infura/"
)

func init() {
func initMetrics() {
// Remove unused metrics imported from ethereum rpc package
var names []string
for k := range metrics.DefaultRegistry.GetAll() {
Expand Down
9 changes: 6 additions & 3 deletions rpc/server_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const (
ctxKeyClientGroup = handlers.CtxKey("Infura-RPC-Client-Group")
)

// go-rpc-provider only supports static middlewares for RPC server.
func init() {
// middlewares executed in order
func MustInit() {
// init metrics
initMetrics()

// Register middlewares for go-rpc-provider, which only supports static middlewares for RPC server.
// The following middlewares are executed in order.

// panic recovery
rpc.HookHandleCallMsg(middlewares.Recover)
Expand Down
5 changes: 1 addition & 4 deletions store/log_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package store
import (
"time"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/go-conflux-sdk/types"
"github.com/Conflux-Chain/go-conflux-sdk/types/cfxaddress"
"github.com/Conflux-Chain/go-conflux-util/viper"
Expand Down Expand Up @@ -46,7 +43,7 @@ var ( // Log filter constants
MaxLogBlockRange uint64
)

func init() {
func initLogFilter() {
var lfc struct {
MaxBlockHashCount int `default:"32"`
MaxAddressCount int `default:"32"`
Expand Down
7 changes: 3 additions & 4 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"io"
"strings"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/go-conflux-sdk/types"
"github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -167,7 +164,9 @@ func (conf *storeConfig) IsDisabledForType(edt EpochDataType) bool {
return false
}

func init() {
func MustInit() {
cfxStoreConfig.mustInit("store")
ethStoreConfig.mustInit("ethstore")

initLogFilter()
}
5 changes: 1 addition & 4 deletions util/blacklist/contract_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"encoding/json"
"strings"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/go-conflux-sdk/types/cfxaddress"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand All @@ -24,7 +21,7 @@ type BlacklistedAddrInfo struct {
Epoch uint64
}

func init() {
func MustInit() {
// Load blacklisted contract address.
var blackListAddrStrs string
if err := viper.UnmarshalKey("sync.blackListAddrs", &blackListAddrStrs); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion util/metrics/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// metric created for static variables in any package.
//
// In addition, this package should be imported after the initialization of viper and logrus.
func Init() {
func MustInit() {
var config struct {
Enabled bool `default:"true"`
Influxdb struct {
Expand Down
5 changes: 1 addition & 4 deletions util/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package rpc
import (
"time"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/go-conflux-util/viper"
)

Expand Down Expand Up @@ -86,7 +83,7 @@ func WithCircuitBreaker(maxFail int, failTimeWindow, openColdTime time.Duration)
}
}

func init() {
func MustInit() {
viper.MustUnmarshalKey("cfx", &cfxClientCfg)
viper.MustUnmarshalKey("eth", &ethClientCfg)
}
3 changes: 0 additions & 3 deletions util/rpc/middlewares/web3pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package middlewares
import (
"strings"

// ensure viper based configuration initialized at the very beginning
_ "github.com/Conflux-Chain/confura/config"

"github.com/Conflux-Chain/confura/util/rpc/handlers"
"github.com/Conflux-Chain/go-conflux-util/viper"
web3pay "github.com/Conflux-Chain/web3pay-service/client"
Expand Down
Loading