2017-05-16 13:39:59 +02:00
|
|
|
package main
|
|
|
|
|
2017-05-16 15:22:40 +02:00
|
|
|
import (
|
2021-10-22 22:17:04 +02:00
|
|
|
"context"
|
2017-05-19 16:16:14 +02:00
|
|
|
"flag"
|
2022-02-08 17:29:31 +01:00
|
|
|
"fmt"
|
2017-05-16 15:22:40 +02:00
|
|
|
"log"
|
2022-02-08 17:29:31 +01:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
2017-05-16 13:39:59 +02:00
|
|
|
|
2021-10-22 22:17:04 +02:00
|
|
|
"github.com/alice-lg/alice-lg/pkg/config"
|
2021-10-25 22:03:10 +02:00
|
|
|
"github.com/alice-lg/alice-lg/pkg/http"
|
2021-10-20 18:36:30 +00:00
|
|
|
"github.com/alice-lg/alice-lg/pkg/store"
|
2021-12-07 19:11:11 +01:00
|
|
|
"github.com/alice-lg/alice-lg/pkg/store/backends/memory"
|
2022-01-13 18:28:24 +01:00
|
|
|
"github.com/alice-lg/alice-lg/pkg/store/backends/postgres"
|
2022-01-14 11:04:14 +01:00
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-03-22 16:25:47 +01:00
|
|
|
)
|
2017-05-18 15:23:36 +02:00
|
|
|
|
2022-02-08 17:29:31 +01:00
|
|
|
func createHeapProfile(filename string) {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create memory profile: ", err)
|
|
|
|
}
|
|
|
|
defer f.Close() // error handling omitted for example
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
log.Fatal("could not write memory profile: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAllocProfile(filename string) {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create alloc profile: ", err)
|
|
|
|
}
|
|
|
|
defer f.Close() // error handling omitted for example
|
|
|
|
if err := pprof.Lookup("allocs").WriteTo(f, 0); err != nil {
|
|
|
|
log.Fatal("could not write alloc profile: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startMemoryProfile(prefix string) {
|
2022-02-09 12:59:35 +01:00
|
|
|
t := 0
|
2022-02-08 17:29:31 +01:00
|
|
|
for {
|
|
|
|
filename := fmt.Sprintf("%s-heap-%03d", prefix, t)
|
2022-02-08 21:55:26 +01:00
|
|
|
runtime.GC() // get up-to-date statistics (according to docs)
|
2022-02-08 17:29:31 +01:00
|
|
|
createHeapProfile(filename)
|
|
|
|
log.Println("wrote memory heap profile:", filename)
|
|
|
|
filename = fmt.Sprintf("%s-allocs-%03d", prefix, t)
|
|
|
|
log.Println("wrote memory allocs profile:", filename)
|
|
|
|
createAllocProfile(filename)
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
t++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 13:39:59 +02:00
|
|
|
func main() {
|
2021-10-22 22:17:04 +02:00
|
|
|
ctx := context.Background()
|
2021-03-22 17:35:20 +01:00
|
|
|
|
2017-05-19 16:16:14 +02:00
|
|
|
// Handle commandline parameters
|
|
|
|
configFilenameFlag := flag.String(
|
2019-01-17 12:16:42 +01:00
|
|
|
"config", "/etc/alice-lg/alice.conf",
|
2017-05-19 16:16:14 +02:00
|
|
|
"Alice looking glass configuration file",
|
|
|
|
)
|
2022-01-13 18:28:24 +01:00
|
|
|
dbInitFlag := flag.Bool(
|
|
|
|
"db-init", false,
|
|
|
|
"Initialize the database. Clears all data.",
|
|
|
|
)
|
2022-02-08 17:29:31 +01:00
|
|
|
memprofile := flag.String(
|
|
|
|
"memprofile", "", "write memory profile to `file`",
|
|
|
|
)
|
2017-05-19 16:16:14 +02:00
|
|
|
flag.Parse()
|
2017-05-16 15:22:40 +02:00
|
|
|
|
2022-02-08 17:29:31 +01:00
|
|
|
if *memprofile != "" {
|
|
|
|
go startMemoryProfile(*memprofile)
|
|
|
|
}
|
|
|
|
|
2017-05-16 15:22:40 +02:00
|
|
|
// Load configuration
|
2021-10-27 18:01:13 +00:00
|
|
|
cfg, err := config.LoadConfig(*configFilenameFlag)
|
2021-10-22 22:17:04 +02:00
|
|
|
if err != nil {
|
2017-05-19 16:16:14 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:28:24 +01:00
|
|
|
// Setup local routes store and use backend from configuration
|
|
|
|
var (
|
|
|
|
neighborsBackend store.NeighborsStoreBackend = memory.NewNeighborsBackend()
|
|
|
|
routesBackend store.RoutesStoreBackend = memory.NewRoutesBackend()
|
2022-01-14 11:04:14 +01:00
|
|
|
|
|
|
|
pool *pgxpool.Pool
|
2022-01-13 18:28:24 +01:00
|
|
|
)
|
|
|
|
if cfg.Server.StoreBackend == "postgres" {
|
2022-01-14 11:04:14 +01:00
|
|
|
pool, err = postgres.Connect(ctx, cfg.Postgres)
|
2022-01-13 18:28:24 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
m := postgres.NewManager(pool)
|
|
|
|
|
|
|
|
// Initialize db if required
|
|
|
|
if *dbInitFlag {
|
|
|
|
if err := m.Initialize(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Println("database initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go m.Start(ctx)
|
|
|
|
|
|
|
|
neighborsBackend = postgres.NewNeighborsBackend(pool)
|
|
|
|
routesBackend = postgres.NewRoutesBackend(pool)
|
|
|
|
}
|
2021-12-07 19:11:11 +01:00
|
|
|
|
|
|
|
neighborsStore := store.NewNeighborsStore(cfg, neighborsBackend)
|
|
|
|
routesStore := store.NewRoutesStore(neighborsStore, cfg, routesBackend)
|
2021-10-27 18:01:13 +00:00
|
|
|
|
|
|
|
// Say hi
|
|
|
|
printBanner(cfg, neighborsStore, routesStore)
|
|
|
|
log.Println("Using configuration:", cfg.File)
|
2017-07-04 12:36:48 +02:00
|
|
|
|
2021-03-22 17:35:20 +01:00
|
|
|
// Start stores
|
2021-10-27 18:01:13 +00:00
|
|
|
if cfg.Server.EnablePrefixLookup == true {
|
|
|
|
go neighborsStore.Start()
|
|
|
|
go routesStore.Start()
|
2017-07-04 12:36:48 +02:00
|
|
|
}
|
2017-06-23 17:40:19 +02:00
|
|
|
|
2019-02-18 20:20:26 +01:00
|
|
|
// Start the Housekeeping
|
2021-10-25 22:03:10 +02:00
|
|
|
go store.StartHousekeeping(ctx, cfg)
|
2017-05-16 15:22:40 +02:00
|
|
|
|
2021-10-22 22:17:04 +02:00
|
|
|
// Start HTTP API
|
2022-01-14 11:04:14 +01:00
|
|
|
server := http.NewServer(cfg, pool, routesStore, neighborsStore)
|
2021-10-25 22:03:10 +02:00
|
|
|
go server.Start()
|
2017-05-18 14:44:50 +02:00
|
|
|
|
2021-12-07 19:11:11 +01:00
|
|
|
<-ctx.Done()
|
2017-05-16 13:39:59 +02:00
|
|
|
}
|