-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
242 lines (201 loc) · 6.23 KB
/
app.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package horizon
import (
"fmt"
"gitlab.com/distributed_lab/kit/pgdb"
"net/http"
"runtime"
"sync"
"time"
metrics "github.com/rcrowley/go-metrics"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/distributed_lab/txsub"
"gitlab.com/tokend/horizon/cache"
"gitlab.com/tokend/horizon/config"
"gitlab.com/tokend/horizon/corer"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/ingest"
"gitlab.com/tokend/horizon/ledger"
"gitlab.com/tokend/horizon/log"
"gitlab.com/tokend/horizon/render/sse"
txsub2 "gitlab.com/tokend/horizon/txsub/v2"
"golang.org/x/net/context"
"golang.org/x/net/http2"
graceful "gopkg.in/tylerb/graceful.v1"
)
// You can override this variable using: gb build -ldflags "-X main.version aabbccdd"
var version = ""
// App represents the root of the state of a horizon instance.
type App struct {
config config.Config
web *Web
webV2 *WebV2
historyQ history.QInterface
coreQ core.QInterface
ctx context.Context
cancel func()
submitter *txsub.System
submitterV2 *txsub2.System
ingester *ingest.System
ticks *time.Ticker
CoreInfo *corer.Info
horizonVersion string
cacheProvider *cache.Provider
CoreConnector *corer.Connector
// metrics
metrics metrics.Registry
historyLatestLedgerGauge metrics.Gauge
historyElderLedgerGauge metrics.Gauge
horizonConnGauge metrics.Gauge
coreLatestLedgerGauge metrics.Gauge
coreElderLedgerGauge metrics.Gauge
coreConnGauge metrics.Gauge
goroutineGauge metrics.Gauge
}
// SetVersion records the provided version string in the package level `version`
// var, which will be used for the reported horizon version.
func SetVersion(v string) {
version = v
}
// NewApp constructs an new App instance from the provided config.
func NewApp(config config.Config) (*App, error) {
result := &App{config: config}
result.horizonVersion = version
result.ticks = time.NewTicker(1 * time.Second)
result.init()
return result, nil
}
// Serve starts the horizon web server, binding it to a socket, setting up
// the shutdown signals.
func (a *App) Serve() {
a.web.router.Compile()
http.Handle("/v3/", a.webV2.mux)
http.Handle("/", a.web.router)
addr := fmt.Sprintf(":%d", a.config.Port)
srv := &graceful.Server{
Timeout: 10 * time.Second,
Server: &http.Server{
Addr: addr,
Handler: http.DefaultServeMux,
},
ShutdownInitiated: func() {
log.Info("received signal, gracefully stopping")
a.Close()
},
}
http2.ConfigureServer(srv.Server, nil)
log.Infof("Starting horizon on %s", addr)
go a.run()
err := srv.ListenAndServe()
if err != nil {
log.Panic(err)
}
log.Info("stopped")
}
// Close cancels the app and forces the closure of db connections
func (a *App) Close() {
a.cancel()
a.ticks.Stop()
a.historyQ.GetRepo().RawDB().Close()
a.coreQ.GetRepo().RawDB().Close()
}
// CoreQ returns a helper object for performing sql queries against the
// stellar core database.
func (a *App) CoreQ() core.QInterface {
return a.coreQ
}
// HistoryQ returns a helper object for performing sql queries against the
// history portion of horizon's database.
func (a *App) HistoryQ() history.QInterface {
return a.historyQ
}
// CoreRepoLogged returns a new repo that loads data from the core database.
func (a *App) CoreRepoLogged(log *logan.Entry) *pgdb.DB {
return a.coreQ.GetRepo().Clone()
}
// HistoryRepoLogged returns a new repo that loads data from the horizon database.
func (a *App) HistoryRepoLogged(log *logan.Entry) *pgdb.DB {
return a.historyQ.GetRepo().Clone()
}
// IsHistoryStale returns true if the latest history ledger is more than
// `StaleThreshold` ledgers behind the latest core ledger
func (a *App) IsHistoryStale() bool {
if a.config.StaleThreshold == 0 {
return false
}
ls := ledger.CurrentState()
return (ls.Core.Latest - ls.History.Latest) > int32(a.config.StaleThreshold)
}
// UpdateCoreInfo updates the value of coreVersion and networkPassphrase
// from the Stellar core API.
func (a *App) UpdateCoreInfo() error {
if a.config.StellarCoreURL == "" {
return nil
}
var info *corer.Info
info, err := a.CoreConnector.GetCoreInfo()
if err != nil {
log.WithField("service", "core-info").WithError(err).Error("could not load stellar-core info")
return errors.Wrap(err, "could not load stellar-core info")
}
a.CoreInfo = info
return nil
}
// UpdateMetrics triggers a refresh of several metrics gauges, such as open
// db connections and ledger state
func (a *App) UpdateMetrics() {
a.goroutineGauge.Update(int64(runtime.NumGoroutine()))
ls := ledger.CurrentState()
a.historyLatestLedgerGauge.Update(int64(ls.History.Latest))
a.historyElderLedgerGauge.Update(int64(ls.History.OldestOnStart))
a.coreLatestLedgerGauge.Update(int64(ls.Core.Latest))
a.coreElderLedgerGauge.Update(int64(ls.Core.OldestOnStart))
//a.horizonConnGauge.Update(int64(a.historyQ.Repo.DB.Stats().OpenConnections))
//a.coreConnGauge.Update(int64(a.coreQ.Repo.DB.Stats().OpenConnections))
}
// UpdateWebV2Metrics updates the metrics for the web_v2 requests
func (a *App) UpdateWebV2Metrics(requestDuration time.Duration, responseStatus int) {
a.webV2.metrics.Update(requestDuration, responseStatus)
}
// Tick triggers horizon to update all of it's background processes such as
// transaction submission, metrics, ingestion and reaping.
func (a *App) Tick() {
var wg sync.WaitGroup
log.Debug("ticking app")
// update ledger state and stellar-core info in parallel
wg.Add(1)
go func() { a.UpdateCoreInfo(); wg.Done() }()
wg.Wait()
if a.ingester != nil {
go a.ingester.Tick()
}
wg.Add(1)
go func() { a.submitter.Tick(a.ctx); wg.Done() }()
wg.Wait()
sse.Tick()
// finally, update metrics
a.UpdateMetrics()
log.Debug("finished ticking app")
}
// Init initializes app, using the config to populate db connections and
// whatnot.
func (a *App) init() {
appInit.Run(a)
}
// run is the function that runs in the background that triggers Tick each
// second
func (a *App) run() {
for {
select {
case <-a.ticks.C:
a.Tick()
case <-a.ctx.Done():
log.Info("finished background ticker")
return
}
}
}
func (a *App) Conf() config.Config {
return a.config
}