-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (91 loc) · 2.85 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
package main
import (
"context"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/itzmeanjan/harmony/app/bootup"
"github.com/itzmeanjan/harmony/app/config"
"github.com/itzmeanjan/harmony/app/mempool"
"github.com/itzmeanjan/harmony/app/networking"
"github.com/itzmeanjan/harmony/app/server"
)
func main() {
log.Printf("[😌] Harmony - Reducing Chaos in MemPool\n")
abs, err := filepath.Abs(".env")
if err != nil {
log.Printf("[❗️] Failed to find absolute path of file : %s\n", err.Error())
os.Exit(1)
}
// This is application's root level context, to be passed down
// to worker go routines
ctx, cancel := context.WithCancel(context.Background())
resources, err := bootup.SetGround(ctx, abs)
if err != nil {
log.Printf("[❗️] Failed to acquire resource(s) : %s\n", err.Error())
os.Exit(1)
}
// To be passed to worker go routines, for listening to
// their state changes
comm := make(chan struct{}, 1)
// Checking if user has explicitly asked to be part of
// larger `harmony` p2p network
if config.GetNetworkingChoice() {
// Attempting to set up p2p networking stack of `harmony`, so that
// this node can be part of larger network
if err := networking.Setup(ctx, comm); err != nil {
log.Printf("[❗️] Failed to bootstrap networking : %s\n", err.Error())
os.Exit(1)
}
} else {
log.Printf("[❃] Running in solo mode\n")
}
// Attempt to catch interrupt event(s)
// so that graceful shutdown can be performed
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
// To be invoked when returning from this
// go rountine's execution scope
defer func() {
resources.Release()
// Stopping process
log.Printf("\n[✅] Gracefully shut down `harmony` after %s\n", time.Now().UTC().Sub(resources.StartedAt))
os.Exit(0)
}()
OUTER:
for {
select {
case <-interruptChan:
// When interrupt is received, attempting to
// let all other go routines know, master go routine
// wants all to shut down, they must do a graceful shut down
// of what they're doing now
cancel()
// Giving workers 3 seconds, before forcing shutdown
//
// This is simply a blocking call i.e. blocks for 3 seconds
<-time.After(time.Second * time.Duration(3))
break OUTER
case <-comm:
// Supervisor go routine got to learn
// that go routine it spawned some time ago
// for polling ethereum mempool content periodically
// has died
//
// It's supposed to spawn new go routine for handling that op
//
// @note To be implemented
break OUTER
}
}
}()
// Starting tx pool monitor as a seperate worker
go mempool.PollTxPoolContent(ctx, resources, comm)
// Main go routine, starts one http server &
// interfaces with external world
server.Start(ctx, resources)
}