func initRedis(app *App) { if app.config.RedisUrl == "" { return } redisURL, err := url.Parse(app.config.RedisUrl) if err != nil { log.Panic(err) } app.redis = &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: dialRedis(redisURL), TestOnBorrow: pingRedis, } // test the connection c := app.redis.Get() defer c.Close() _, err = c.Do("PING") if err != nil { log.Panic(err) } }
// Serve starts the horizon system, binding it to a socket, setting up // the shutdown signals and starting the appropriate db-streaming pumps. func (a *App) Serve() { a.web.router.Compile() http.Handle("/", a.web.router) listenStr := fmt.Sprintf(":%d", a.config.Port) listener := bind.Socket(listenStr) log.Infof(a.ctx, "Starting horizon on %s", listener.Addr()) graceful.HandleSignals() bind.Ready() graceful.PreHook(func() { log.Info(a.ctx, "received signal, gracefully stopping") a.Cancel() }) graceful.PostHook(func() { log.Info(a.ctx, "stopped") }) sse.SetPump(a.ctx, a.pump.Subscribe()) err := graceful.Serve(listener, http.DefaultServeMux) if err != nil { log.Panic(a.ctx, err) } graceful.Wait() }
func initHistoryDb(app *App) { historyDb, err := db.Open(app.config.DatabaseUrl) if err != nil { log.Panic(err) } historyDb.SetMaxIdleConns(4) historyDb.SetMaxOpenConns(12) app.historyDb = historyDb }
func initCoreDb(app *App) { coreDb, err := db.Open(app.config.StellarCoreDatabaseUrl) if err != nil { log.Panic(err) } coreDb.SetMaxIdleConns(4) coreDb.SetMaxOpenConns(12) app.coreDb = coreDb }
func initHorizonDb(app *App) { repo, err := db2.Open(app.config.DatabaseURL) if err != nil { log.Panic(err) } repo.DB.SetMaxIdleConns(4) repo.DB.SetMaxOpenConns(12) app.historyQ = &history.Q{repo} }
func initCoreDb(app *App) { repo, err := db2.Open(app.config.StellarCoreDatabaseURL) if err != nil { log.Panic(err) } repo.DB.SetMaxIdleConns(4) repo.DB.SetMaxOpenConns(12) app.coreQ = &core.Q{repo} }
// Run initializes the provided application, but running every Initializer func (is *initializerSet) Run(app *App) { init := *is alreadyRun := make(map[string]bool) for { ranInitializer := false for _, i := range init { runnable := true // if we've already been run, skip if _, ok := alreadyRun[i.Name]; ok { runnable = false } // if any of our dependencies haven't been run, skip for _, d := range i.Deps { if _, ok := alreadyRun[d]; !ok { runnable = false break } } if !runnable { continue } log.WithField("init_name", i.Name).Debug("running initializer") i.Fn(app) alreadyRun[i.Name] = true ranInitializer = true } // If, after a full loop through the initializers we ran nothing // we are done if !ranInitializer { break } } // if we didn't get to run all initializers, we have a cycle if len(alreadyRun) != len(init) { log.Panic("initializer cycle detected") } }
// Serve starts the horizon system, binding it to a socket, setting up // the shutdown signals and starting the appropriate db-streaming pumps. func (a *App) Serve() { a.web.router.Compile() 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) sse.SetPump(a.pump.Subscribe()) log.Infof("Starting horizon on %s", addr) var err error if a.config.TLSCert != "" { err = srv.ListenAndServeTLS(a.config.TLSCert, a.config.TLSKey) } else { err = srv.ListenAndServe() } if err != nil { log.Panic(err) } log.Info("stopped") }