Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
e-asphyx committed Jul 25, 2024
1 parent 945b5e3 commit 36edd4b
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 441 deletions.
105 changes: 0 additions & 105 deletions bootstrap.go

This file was deleted.

16 changes: 0 additions & 16 deletions client.go

This file was deleted.

20 changes: 10 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type Config struct {
Listen string `yaml:"listen"`
URL string `yaml:"url"`
ChainID *tz.ChainID `yaml:"chain_id"`
Timeout time.Duration `yaml:"timeout"`
Tolerance time.Duration `yaml:"tolerance"`
ReconnectDelay time.Duration `yaml:"reconnect_delay"`
UseTimestamps bool `yaml:"use_timestamps"`
BootstrappedPollInterval time.Duration `yaml:"bootstrapped_poll_interval"`
HealthUseBootstrapped bool `yaml:"health_use_bootstrapped"`
HealthUseBlockDelay bool `yaml:"health_use_block_delay"`
Listen string `yaml:"listen"`
URL string `yaml:"url"`
ChainID *tz.ChainID `yaml:"chain_id"`
Timeout time.Duration `yaml:"timeout"`
Tolerance time.Duration `yaml:"tolerance"`
ReconnectDelay time.Duration `yaml:"reconnect_delay"`
UseTimestamps bool `yaml:"use_timestamps"`
PollInterval time.Duration `yaml:"poll_interval"`
HealthUseBootstrapped bool `yaml:"health_use_bootstrapped"`
HealthUseBlockDelay bool `yaml:"health_use_block_delay"`
}
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
listen: :8080
url: http://10.100.20.44
chain_id: NetXXWAHLEvre9b
url: http://parisnet.i.ecadinfra.com:8732
chain_id: NetXR64bNAYkP4S
80 changes: 50 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

"flag"

"github.com/ecadlabs/gotez/v2/client"
"github.com/ecadlabs/gotez/v2"
client "github.com/ecadlabs/gotez/v2/clientv2"
"github.com/ecadlabs/gotez/v2/clientv2/utils"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -20,11 +22,11 @@ import (
)

const (
defaultListen = ":8080"
defaultTimeout = 30 * time.Second
defaultTolerance = 1 * time.Second
defaultReconnectDelay = 10 * time.Second
defaultBootstrappedPollInterval = 10 * time.Second
defaultListen = ":8080"
defaultTimeout = 30 * time.Second
defaultTolerance = 1 * time.Second
defaultReconnectDelay = 10 * time.Second
defaultPollInterval = 15 * time.Second
)

type debugLogger log.Logger
Expand All @@ -45,13 +47,13 @@ func main() {
log.SetLevel(ll)

conf := Config{
Listen: defaultListen,
Timeout: defaultTimeout,
Tolerance: defaultTolerance,
ReconnectDelay: defaultReconnectDelay,
HealthUseBlockDelay: true,
HealthUseBootstrapped: true,
BootstrappedPollInterval: defaultBootstrappedPollInterval,
Listen: defaultListen,
Timeout: defaultTimeout,
Tolerance: defaultTolerance,
ReconnectDelay: defaultReconnectDelay,
HealthUseBlockDelay: true,
HealthUseBootstrapped: true,
PollInterval: defaultPollInterval,
}

buf, err := os.ReadFile(*confPath)
Expand All @@ -71,39 +73,57 @@ func main() {

reg := prometheus.NewRegistry()

mon := (&HeadMonitorConfig{
hmon, err := (&HeadMonitorConfig{
Client: &cl,
ChainID: conf.ChainID,
Timeout: conf.Timeout,
Tolerance: conf.Tolerance,
ReconnectDelay: conf.ReconnectDelay,
UseTimestamps: conf.UseTimestamps,
Reg: reg,
}).New(context.Background())
if err != nil {
log.Fatal(err)
}

nextProto := func() *gotez.ProtocolHash { _, p := hmon.Protocols(); return p }

mmon := (&MempoolMonitorConfig{
Client: &cl,
ChainID: conf.ChainID,
Timeout: conf.Timeout,
ReconnectDelay: conf.ReconnectDelay,
Reg: reg,
NextProtocolFunc: nextProto,
}).New()

bs := (&BootstrapPollerConfig{
Client: &cl,
ChainID: conf.ChainID,
Timeout: conf.Timeout,
Interval: conf.BootstrappedPollInterval,
Reg: reg,
poller := (&PollerConfig{
Client: &cl,
ChainID: conf.ChainID,
Timeout: conf.Timeout,
Interval: conf.PollInterval,
Reg: reg,
NextProtocolFunc: nextProto,
}).New()

mon.Start()
defer mon.Stop(context.Background())
hmon.Start()
defer hmon.Stop(context.Background())

poller.Start()
defer poller.Stop(context.Background())

bs.Start()
defer bs.Stop(context.Background())
mmon.Start()
defer mmon.Stop(context.Background())

r := mux.NewRouter()
r.Methods("GET").Path("/health").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := true
if conf.HealthUseBootstrapped {
s := bs.Status()
status = status && s.Bootstrapped && s.SyncState == client.SyncStateSynced
s := poller.Status()
status = status && s.Bootstrapped && s.SyncState == utils.SyncStateSynced
}
if conf.HealthUseBlockDelay {
status = status && mon.Status()
status = status && hmon.Status()
}

var code int
Expand All @@ -117,9 +137,9 @@ func main() {
json.NewEncoder(w).Encode(status)
})
r.Methods("GET").Path("/sync_status").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := bs.Status()
status := poller.Status()
var code int
if status.Bootstrapped && status.SyncState == client.SyncStateSynced {
if status.Bootstrapped && status.SyncState == utils.SyncStateSynced {
code = http.StatusOK
} else {
code = http.StatusInternalServerError
Expand All @@ -129,7 +149,7 @@ func main() {
json.NewEncoder(w).Encode(status)
})
r.Methods("GET").Path("/block_delay").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := mon.Status()
status := hmon.Status()
var code int
if status {
code = http.StatusOK
Expand Down
Loading

0 comments on commit 36edd4b

Please sign in to comment.