tracker/chihaya.go

115 lines
2.3 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2013-06-22 01:31:32 +02:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package chihaya
2013-06-22 01:31:32 +02:00
import (
"flag"
"os"
"os/signal"
2013-06-22 01:31:32 +02:00
"runtime"
"sync"
"syscall"
2013-06-22 01:31:32 +02:00
2014-07-01 05:21:08 +02:00
"github.com/golang/glog"
2014-06-19 18:34:47 +02:00
"github.com/chihaya/chihaya/config"
2014-07-02 03:40:29 +02:00
"github.com/chihaya/chihaya/http"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/udp"
// See the README for how to import custom drivers.
_ "github.com/chihaya/chihaya/backend/noop"
2013-06-22 01:31:32 +02:00
)
var (
2014-07-25 22:58:26 +02:00
maxProcs int
configPath string
2013-06-22 01:31:32 +02:00
)
func init() {
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
2014-07-16 08:24:00 +02:00
flag.StringVar(&configPath, "config", "", "path to the configuration file")
2013-06-22 01:31:32 +02:00
}
2014-07-19 01:09:06 +02:00
// Boot starts Chihaya. By exporting this function, anyone can import their own
// custom drivers into their own package main and then call chihaya.Boot.
func Boot() {
2014-07-14 03:27:20 +02:00
defer glog.Flush()
2013-06-22 01:31:32 +02:00
flag.Parse()
2014-07-02 06:45:17 +02:00
runtime.GOMAXPROCS(maxProcs)
glog.V(1).Info("Set max threads to ", maxProcs)
2013-06-22 01:31:32 +02:00
2015-06-26 20:05:55 +02:00
debugBoot()
defer debugShutdown()
2013-06-22 01:31:32 +02:00
2014-07-02 03:40:29 +02:00
cfg, err := config.Open(configPath)
2013-06-23 09:56:28 +02:00
if err != nil {
glog.Fatalf("Failed to parse configuration file: %s\n", err)
2013-06-23 09:56:28 +02:00
}
2014-07-02 03:40:29 +02:00
if cfg == &config.DefaultConfig {
glog.V(1).Info("Using default config")
2014-07-02 03:40:29 +02:00
} else {
glog.V(1).Infof("Loaded config file: %s", configPath)
2013-06-24 06:18:59 +02:00
}
2013-06-22 01:31:32 +02:00
2014-07-23 23:25:01 +02:00
stats.DefaultStats = stats.New(cfg.StatsConfig)
tkr, err := tracker.New(cfg)
if err != nil {
glog.Fatal("New: ", err)
}
var wg sync.WaitGroup
var servers []tracker.Server
if cfg.HTTPListenAddr != "" {
wg.Add(1)
srv := http.NewServer(cfg, tkr)
servers = append(servers, srv)
go func() {
defer wg.Done()
srv.Serve(cfg.HTTPListenAddr)
}()
}
if cfg.UDPListenAddr != "" {
wg.Add(1)
srv := udp.NewServer(cfg, tkr)
servers = append(servers, srv)
go func() {
defer wg.Done()
srv.Serve(cfg.UDPListenAddr)
}()
}
shutdown := make(chan os.Signal)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
go func() {
wg.Wait()
signal.Stop(shutdown)
close(shutdown)
}()
<-shutdown
glog.Info("Shutting down...")
for _, srv := range servers {
srv.Stop()
}
<-shutdown
if err := tkr.Close(); err != nil {
glog.Errorf("Failed to shut down tracker cleanly: %s", err.Error())
}
2013-06-22 01:31:32 +02:00
}