forked from solana-labs/solana-ping-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (146 loc) · 5.07 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"flag"
"log"
"strings"
"time"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"github.com/blocto/solana-go-sdk/rpc"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var config Config
// Cluster enum
type Cluster string
var database *gorm.DB
const useGCloudDB = true
type ClustersToRun string
// Cluster enum
const (
MainnetBeta Cluster = "MainnetBeta"
Testnet = "Testnet"
Devnet = "Devnet"
)
var influxdb *InfluxdbClient
var userInputClusterMode string
var mainnetFailover RPCFailover
var testnetFailover RPCFailover
var devnetFailover RPCFailover
const (
RunMainnetBeta ClustersToRun = "mainnet"
RunTestnet = "testnet"
RunDevnet = "devnet"
RunAllClusters = "all"
)
func init() {
config = loadConfig()
log.Println(" *** Config Start *** ")
log.Println("--- //// Database Config --- ")
log.Println(config.Database)
log.Println("--- //// Influxdb Config --- ")
log.Println(config.InfluxdbConfig)
log.Println("--- //// Retension --- ")
log.Println(config.Retension)
log.Println("--- //// ClusterCLIConfig--- ")
log.Println("ClusterCLIConfig Mainnet", config.ClusterCLIConfig.ConfigMain)
log.Println("ClusterCLIConfig Testnet", config.ClusterCLIConfig.ConfigTestnet)
log.Println("ClusterCLIConfig Devnet", config.ClusterCLIConfig.ConfigDevnet)
log.Println("--- Mainnet Ping --- ")
log.Println("Mainnet.ClusterPing.APIServer", config.Mainnet.ClusterPing.APIServer)
log.Println("Mainnet.ClusterPing.PingServiceEnabled", config.Mainnet.ClusterPing.PingServiceEnabled)
log.Println("Mainnet.ClusterPing.AlternativeEnpoint.HostList", config.Mainnet.ClusterPing.AlternativeEnpoint.HostList)
log.Println("Mainnet.ClusterPing.PingConfig", config.Mainnet.ClusterPing.PingConfig)
log.Println("Mainnet.ClusterPing.Report", config.Mainnet.ClusterPing.Report)
log.Println("--- Testnet Ping --- ")
log.Println("Mainnet.ClusterPing.APIServer", config.Testnet.ClusterPing.APIServer)
log.Println("Mainnet.ClusterPing.PingServiceEnabled", config.Mainnet.ClusterPing.PingServiceEnabled)
log.Println("Testnet.ClusterPing.AlternativeEnpoint.HostList", config.Testnet.ClusterPing.AlternativeEnpoint.HostList)
log.Println("Testnet.ClusterPing.PingConfig", config.Testnet.ClusterPing.PingConfig)
log.Println("Testnet.ClusterPing.Report", config.Testnet.ClusterPing.Report)
log.Println("--- Devnet Ping --- ")
log.Println("Devnet.ClusterPing.APIServer", config.Devnet.ClusterPing.APIServer)
log.Println("Devnet.ClusterPing.Enabled", config.Devnet.ClusterPing.PingServiceEnabled)
log.Println("Devnet.ClusterPing.AlternativeEnpoint.HostList", config.Devnet.ClusterPing.AlternativeEnpoint.HostList)
log.Println("Devnet.ClusterPing.PingConfig", config.Devnet.ClusterPing.PingConfig)
log.Println("Devnet.ClusterPing.Report", config.Devnet.ClusterPing.Report)
log.Println(" *** Config End *** ")
ResponseErrIdentifierInit()
StatisticErrExpectionInit()
AlertErrExpectionInit()
ReportErrExpectionInit()
PingTakeTimeErrExpectionInit()
if config.Database.UseGoogleCloud {
gormDB, err := gorm.Open(postgres.New(postgres.Config{
DriverName: "cloudsqlpostgres",
DSN: config.DBConn,
}))
if err != nil {
log.Panic(err)
}
database = gormDB
} else {
gormDB, err := gorm.Open(postgres.Open(config.DBConn), &gorm.Config{})
if err != nil {
log.Panic(err)
}
database = gormDB
}
log.Println("database connected")
if config.InfluxdbConfig.Enabled {
influxdb = NewInfluxdbClient(config.InfluxdbConfig)
}
/// ---- Start RPC Failover ---
log.Println("RPC Endpoint Failover Setting ---")
if len(config.Mainnet.AlternativeEnpoint.HostList) <= 0 {
mainnetFailover = NewRPCFailover([]RPCEndpoint{{
Endpoint: rpc.MainnetRPCEndpoint,
Piority: 1,
MaxRetry: 30}})
} else {
mainnetFailover = NewRPCFailover(config.Mainnet.AlternativeEnpoint.HostList)
}
if len(config.Testnet.AlternativeEnpoint.HostList) <= 0 {
testnetFailover = NewRPCFailover([]RPCEndpoint{{
Endpoint: rpc.MainnetRPCEndpoint,
Piority: 1,
MaxRetry: 30}})
} else {
testnetFailover = NewRPCFailover(config.Testnet.AlternativeEnpoint.HostList)
}
if len(config.Devnet.AlternativeEnpoint.HostList) <= 0 {
devnetFailover = NewRPCFailover([]RPCEndpoint{{
Endpoint: rpc.MainnetRPCEndpoint,
Piority: 1,
MaxRetry: 30}})
} else {
devnetFailover = NewRPCFailover(config.Devnet.AlternativeEnpoint.HostList)
}
}
func main() {
defer func() {
if influxdb != nil {
influxdb.ClientClose()
}
if database != nil {
sqldb, err := database.DB()
if err == nil {
sqldb.Close()
}
}
}()
flag.Parse()
clustersToRun := flag.Arg(0)
if !(strings.Compare(clustersToRun, string(RunMainnetBeta)) == 0 ||
strings.Compare(clustersToRun, string(RunTestnet)) == 0 ||
strings.Compare(clustersToRun, string(RunDevnet)) == 0 ||
strings.Compare(clustersToRun, string(RunAllClusters)) == 0) {
go launchWorkers(RunMainnetBeta)
go APIService(RunMainnetBeta)
} else {
go launchWorkers(ClustersToRun(clustersToRun))
go APIService(ClustersToRun(clustersToRun))
}
for {
time.Sleep(10 * time.Second)
}
}