2016-08-05 07:47:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-05 09:35:17 +02:00
|
|
|
"net/http"
|
2016-08-05 07:47:04 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime/pprof"
|
|
|
|
"syscall"
|
|
|
|
|
2016-09-05 18:10:42 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-08-05 09:35:17 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-08-05 07:47:04 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
httpfrontend "github.com/chihaya/chihaya/frontend/http"
|
|
|
|
udpfrontend "github.com/chihaya/chihaya/frontend/udp"
|
|
|
|
"github.com/chihaya/chihaya/middleware"
|
2016-11-27 10:57:32 +01:00
|
|
|
"github.com/chihaya/chihaya/storage"
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/storage/memory"
|
2016-08-05 07:47:04 +02:00
|
|
|
)
|
|
|
|
|
2016-08-24 23:21:06 +02:00
|
|
|
func rootCmdRun(cmd *cobra.Command, args []string) error {
|
2016-09-05 18:19:54 +02:00
|
|
|
debugLog, _ := cmd.Flags().GetBool("debug")
|
|
|
|
if debugLog {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
log.Debugln("debug logging enabled")
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
cpuProfilePath, _ := cmd.Flags().GetString("cpuprofile")
|
|
|
|
if cpuProfilePath != "" {
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Infoln("enabled CPU profiling to", cpuProfilePath)
|
2016-08-24 23:21:06 +02:00
|
|
|
f, err := os.Create(cpuProfilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 23:21:06 +02:00
|
|
|
configFilePath, _ := cmd.Flags().GetString("config")
|
|
|
|
configFile, err := ParseConfigFile(configFilePath)
|
2016-08-05 09:35:17 +02:00
|
|
|
if err != nil {
|
2016-08-24 23:21:06 +02:00
|
|
|
return errors.New("failed to read config: " + err.Error())
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
cfg := configFile.MainConfigBlock
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
promServer := http.Server{
|
|
|
|
Addr: cfg.PrometheusAddr,
|
|
|
|
Handler: prometheus.Handler(),
|
|
|
|
}
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Infoln("started serving prometheus stats on", cfg.PrometheusAddr)
|
2016-08-24 23:21:06 +02:00
|
|
|
if err := promServer.ListenAndServe(); err != nil {
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Fatalln("failed to start prometheus server:", err.Error())
|
2016-08-24 23:21:06 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
peerStore, err := memory.New(cfg.Storage)
|
2016-08-05 09:35:17 +02:00
|
|
|
if err != nil {
|
2016-09-05 18:10:42 +02:00
|
|
|
return errors.New("failed to create memory storage: " + err.Error())
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:09:46 +02:00
|
|
|
preHooks, postHooks, err := configFile.CreateHooks()
|
|
|
|
if err != nil {
|
2016-09-05 18:10:42 +02:00
|
|
|
return errors.New("failed to create hooks: " + err.Error())
|
2016-09-01 03:09:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logic := middleware.NewLogic(cfg.Config, peerStore, preHooks, postHooks)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
httpFrontend, udpFrontend := startFrontends(cfg.HTTPConfig, cfg.UDPConfig, logic, errChan)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
shutdown := make(chan struct{})
|
|
|
|
quit := make(chan os.Signal)
|
|
|
|
restart := make(chan os.Signal)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
signal.Notify(restart, syscall.SIGUSR1)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-restart:
|
|
|
|
log.Info("Got signal to restart")
|
2016-08-05 09:35:17 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
// Reload config
|
|
|
|
configFile, err = ParseConfigFile(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to read config: " + err.Error())
|
|
|
|
}
|
|
|
|
cfg = configFile.MainConfigBlock
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
preHooks, postHooks, err = configFile.CreateHooks()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to create hooks: " + err.Error())
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
// Stop frontends and logic
|
|
|
|
stopFrontends(udpFrontend, httpFrontend)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
stopLogic(logic, errChan)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
// Restart
|
|
|
|
log.Debug("Restarting logic")
|
|
|
|
logic = middleware.NewLogic(cfg.Config, peerStore, preHooks, postHooks)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
log.Debug("Restarting frontends")
|
|
|
|
httpFrontend, udpFrontend = startFrontends(cfg.HTTPConfig, cfg.UDPConfig, logic, errChan)
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
log.Debug("Successfully restarted")
|
2016-09-05 18:10:42 +02:00
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
case <-quit:
|
|
|
|
stop(udpFrontend, httpFrontend, logic, errChan, peerStore)
|
|
|
|
case <-shutdown:
|
|
|
|
stop(udpFrontend, httpFrontend, logic, errChan, peerStore)
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
closed := false
|
|
|
|
var bufErr error
|
|
|
|
for err = range errChan {
|
|
|
|
if err != nil {
|
|
|
|
if !closed {
|
|
|
|
close(shutdown)
|
|
|
|
closed = true
|
|
|
|
} else {
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Infoln(bufErr)
|
2016-08-24 23:21:06 +02:00
|
|
|
}
|
|
|
|
bufErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufErr
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 10:57:32 +01:00
|
|
|
func stopFrontends(udpFrontend *udpfrontend.Frontend, httpFrontend *httpfrontend.Frontend) {
|
|
|
|
log.Debug("Stopping frontends")
|
|
|
|
if udpFrontend != nil {
|
|
|
|
udpFrontend.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if httpFrontend != nil {
|
|
|
|
httpFrontend.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopLogic(logic *middleware.Logic, errChan chan error) {
|
|
|
|
log.Debug("Stopping logic")
|
|
|
|
errs := logic.Stop()
|
|
|
|
for _, err := range errs {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stop(udpFrontend *udpfrontend.Frontend, httpFrontend *httpfrontend.Frontend, logic *middleware.Logic, errChan chan error, peerStore storage.PeerStore) {
|
|
|
|
stopFrontends(udpFrontend, httpFrontend)
|
|
|
|
|
|
|
|
stopLogic(logic, errChan)
|
|
|
|
|
|
|
|
// Stop storage
|
|
|
|
log.Debug("Stopping storage")
|
|
|
|
for err := range peerStore.Stop() {
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(errChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startFrontends(httpConfig httpfrontend.Config, udpConfig udpfrontend.Config, logic *middleware.Logic, errChan chan<- error) (httpFrontend *httpfrontend.Frontend, udpFrontend *udpfrontend.Frontend) {
|
|
|
|
if httpConfig.Addr != "" {
|
|
|
|
httpFrontend = httpfrontend.NewFrontend(logic, httpConfig)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Infoln("started serving HTTP on", httpConfig.Addr)
|
|
|
|
if err := httpFrontend.ListenAndServe(); err != nil {
|
|
|
|
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if udpConfig.Addr != "" {
|
|
|
|
udpFrontend = udpfrontend.NewFrontend(logic, udpConfig)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Infoln("started serving UDP on", udpConfig.Addr)
|
|
|
|
if err := udpFrontend.ListenAndServe(); err != nil {
|
|
|
|
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
func main() {
|
|
|
|
var rootCmd = &cobra.Command{
|
2016-08-17 03:42:08 +02:00
|
|
|
Use: "chihaya",
|
2016-08-05 07:47:04 +02:00
|
|
|
Short: "BitTorrent Tracker",
|
2017-01-23 01:06:13 +01:00
|
|
|
Long: "A customizable, multi-protocol BitTorrent Tracker",
|
2016-08-05 07:47:04 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-08-24 23:21:06 +02:00
|
|
|
if err := rootCmdRun(cmd, args); err != nil {
|
2016-08-05 07:47:04 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
rootCmd.Flags().String("config", "/etc/chihaya.yaml", "location of configuration file")
|
|
|
|
rootCmd.Flags().String("cpuprofile", "", "location to save a CPU profile")
|
2016-09-05 18:19:54 +02:00
|
|
|
rootCmd.Flags().Bool("debug", false, "enable debug logging")
|
2016-08-05 07:47:04 +02:00
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|