tracker/cmd/chihaya/main.go

192 lines
4.4 KiB
Go
Raw Normal View History

2016-08-05 07:47:04 +02:00
package main
import (
"errors"
2016-08-05 09:35:17 +02:00
"io/ioutil"
2016-08-05 07:47:04 +02:00
"log"
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-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-05 09:35:17 +02:00
"gopkg.in/yaml.v2"
2016-08-05 07:47:04 +02:00
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"
"github.com/chihaya/chihaya/storage/memory"
2016-08-05 07:47:04 +02:00
)
2016-08-05 09:35:17 +02:00
type ConfigFile struct {
2016-08-10 02:08:15 +02:00
MainConfigBlock struct {
middleware.Config
2016-08-10 02:08:15 +02:00
PrometheusAddr string `yaml:"prometheus_addr"`
HTTPConfig httpfrontend.Config `yaml:"http"`
UDPConfig udpfrontend.Config `yaml:"udp"`
Storage memory.Config `yaml:"storage"`
2016-08-17 03:42:08 +02:00
} `yaml:"chihaya"`
2016-08-05 09:35:17 +02:00
}
// ParseConfigFile returns a new ConfigFile given the path to a YAML
// configuration file.
//
// It supports relative and absolute paths and environment variables.
func ParseConfigFile(path string) (*ConfigFile, error) {
if path == "" {
return nil, errors.New("no config path specified")
}
f, err := os.Open(os.ExpandEnv(path))
if err != nil {
return nil, err
}
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var cfgFile ConfigFile
err = yaml.Unmarshal(contents, &cfgFile)
if err != nil {
return nil, err
}
return &cfgFile, nil
}
2016-08-05 07:47:04 +02:00
func main() {
var configFilePath string
var cpuProfilePath string
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",
Long: "A customizible, multi-protocol BitTorrent Tracker",
Run: func(cmd *cobra.Command, args []string) {
if err := func() error {
if cpuProfilePath != "" {
log.Println("enabled CPU profiling to " + cpuProfilePath)
f, err := os.Create(cpuProfilePath)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
2016-08-05 09:35:17 +02:00
configFile, err := ParseConfigFile(configFilePath)
2016-08-05 07:47:04 +02:00
if err != nil {
return errors.New("failed to read config: " + err.Error())
}
2016-08-10 02:08:15 +02:00
cfg := configFile.MainConfigBlock
2016-08-05 07:47:04 +02:00
2016-08-05 09:35:17 +02:00
go func() {
promServer := http.Server{
2016-08-10 02:08:15 +02:00
Addr: cfg.PrometheusAddr,
2016-08-05 09:35:17 +02:00
Handler: prometheus.Handler(),
}
2016-08-10 02:08:15 +02:00
log.Println("started serving prometheus stats on", cfg.PrometheusAddr)
2016-08-05 09:35:17 +02:00
if err := promServer.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// Force the compiler to enforce memory against the storage interface.
peerStore, err := memory.New(cfg.Storage)
if err != nil {
return err
}
2016-08-09 22:01:14 +02:00
// TODO create Hooks
logic := middleware.NewLogic(cfg.Config, peerStore, nil, nil, nil, nil)
if err != nil {
return err
}
2016-08-20 16:19:29 +02:00
shutdown := make(chan struct{})
2016-08-07 04:41:33 +02:00
errChan := make(chan error)
2016-08-20 16:19:29 +02:00
var httpFrontend *httpfrontend.Frontend
var udpFrontend *udpfrontend.Frontend
2016-08-07 04:41:33 +02:00
2016-08-10 02:08:15 +02:00
if cfg.HTTPConfig.Addr != "" {
2016-08-20 16:19:29 +02:00
httpFrontend = httpfrontend.NewFrontend(logic, cfg.HTTPConfig)
2016-08-07 04:41:33 +02:00
go func() {
2016-08-10 02:08:15 +02:00
log.Println("started serving HTTP on", cfg.HTTPConfig.Addr)
2016-08-20 16:19:29 +02:00
if err := httpFrontend.ListenAndServe(); err != nil {
2016-08-07 04:41:33 +02:00
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error())
}
}()
}
2016-08-10 02:08:15 +02:00
if cfg.UDPConfig.Addr != "" {
2016-08-20 16:19:29 +02:00
udpFrontend = udpfrontend.NewFrontend(logic, cfg.UDPConfig)
2016-08-07 04:41:33 +02:00
go func() {
2016-08-10 02:08:15 +02:00
log.Println("started serving UDP on", cfg.UDPConfig.Addr)
2016-08-20 16:19:29 +02:00
if err := udpFrontend.ListenAndServe(); err != nil {
2016-08-07 04:41:33 +02:00
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error())
}
}()
}
2016-08-20 16:19:29 +02:00
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
2016-08-05 07:47:04 +02:00
go func() {
2016-08-20 16:19:29 +02:00
select {
case <-sigChan:
case <-shutdown:
}
2016-08-07 04:41:33 +02:00
2016-08-20 16:19:29 +02:00
if udpFrontend != nil {
udpFrontend.Stop()
2016-08-07 04:41:33 +02:00
}
2016-08-20 16:19:29 +02:00
if httpFrontend != nil {
httpFrontend.Stop()
2016-08-07 04:41:33 +02:00
}
2016-08-20 16:19:29 +02:00
for err := range peerStore.Stop() {
if err != nil {
errChan <- err
}
}
2016-08-07 04:41:33 +02:00
close(errChan)
2016-08-05 07:47:04 +02:00
}()
2016-08-20 16:19:29 +02:00
closed := false
var bufErr error
for err = range errChan {
2016-08-07 23:20:31 +02:00
if err != nil {
2016-08-20 16:19:29 +02:00
if !closed {
close(shutdown)
closed = true
} else {
log.Println(bufErr)
}
bufErr = err
2016-08-07 23:20:31 +02:00
}
2016-08-05 07:47:04 +02:00
}
2016-08-20 16:19:29 +02:00
return bufErr
2016-08-05 07:47:04 +02:00
}(); err != nil {
log.Fatal(err)
}
},
}
2016-08-17 03:42:08 +02:00
rootCmd.Flags().StringVar(&configFilePath, "config", "/etc/chihaya.yaml", "location of configuration file")
2016-08-05 07:47:04 +02:00
rootCmd.Flags().StringVarP(&cpuProfilePath, "cpuprofile", "", "", "location to save a CPU profile")
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}