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.
|
|
|
|
|
2014-07-17 02:33:38 +02:00
|
|
|
package chihaya
|
2013-06-22 01:31:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2015-02-20 19:39:19 +01:00
|
|
|
"os/signal"
|
2013-06-22 01:31:32 +02:00
|
|
|
"runtime"
|
2015-02-20 07:12:47 +01:00
|
|
|
"sync"
|
2015-02-20 19:39:19 +01:00
|
|
|
"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
|
|
|
|
2014-06-24 09:59:30 +02:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-02 03:40:29 +02:00
|
|
|
"github.com/chihaya/chihaya/http"
|
2014-07-23 22:14:50 +02:00
|
|
|
"github.com/chihaya/chihaya/stats"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker"
|
2015-02-20 07:12:47 +01:00
|
|
|
"github.com/chihaya/chihaya/udp"
|
2014-07-15 06:22:04 +02:00
|
|
|
|
2014-07-17 01:46:50 +02:00
|
|
|
// See the README for how to import custom drivers.
|
2014-07-17 07:26:34 +02:00
|
|
|
_ "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() {
|
2014-07-17 02:33:38 +02:00
|
|
|
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.
|
2014-07-16 05:06:07 +02:00
|
|
|
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
|
|
|
|
2014-07-17 02:33:38 +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 {
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.Fatalf("Failed to parse configuration file: %s\n", err)
|
2013-06-23 09:56:28 +02:00
|
|
|
}
|
2014-07-17 02:17:10 +02:00
|
|
|
|
2014-07-02 03:40:29 +02:00
|
|
|
if cfg == &config.DefaultConfig {
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.V(1).Info("Using default config")
|
2014-07-02 03:40:29 +02:00
|
|
|
} else {
|
2014-07-16 17:52:59 +02:00
|
|
|
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)
|
2014-07-23 22:14:50 +02:00
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
tkr, err := tracker.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatal("New: ", err)
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
var wg sync.WaitGroup
|
2015-02-20 19:39:19 +01:00
|
|
|
var servers []tracker.Server
|
2015-02-20 07:12:47 +01:00
|
|
|
|
|
|
|
if cfg.HTTPListenAddr != "" {
|
|
|
|
wg.Add(1)
|
2015-02-20 19:39:19 +01:00
|
|
|
srv := http.NewServer(cfg, tkr)
|
|
|
|
servers = append(servers, srv)
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2015-02-21 19:16:21 +01:00
|
|
|
srv.Serve(cfg.HTTPListenAddr)
|
2015-02-20 07:12:47 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.UDPListenAddr != "" {
|
|
|
|
wg.Add(1)
|
2015-02-20 19:39:19 +01:00
|
|
|
srv := udp.NewServer(cfg, tkr)
|
|
|
|
servers = append(servers, srv)
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2015-02-21 19:16:21 +01:00
|
|
|
srv.Serve(cfg.UDPListenAddr)
|
2015-02-20 07:12:47 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:39:19 +01:00
|
|
|
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
|
2015-02-20 05:46:28 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|