tracker/chihaya.go

108 lines
2.2 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"
"runtime"
"runtime/pprof"
"sync"
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
profile string
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(&profile, "profile", "", "if non-empty, path to write profiling data")
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
2014-07-16 08:24:00 +02:00
if profile != "" {
f, err := os.Create(profile)
2013-06-22 01:31:32 +02:00
if err != nil {
glog.Fatalf("Failed to create profile file: %s\n", err)
2013-06-22 01:31:32 +02:00
}
defer f.Close()
2013-06-22 01:31:32 +02:00
pprof.StartCPUProfile(f)
glog.Info("Started profiling")
2014-06-26 23:19:18 +02:00
defer func() {
pprof.StopCPUProfile()
glog.Info("Stopped profiling")
2014-06-26 23:19:18 +02:00
}()
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
if cfg.HTTPListenAddr != "" {
wg.Add(1)
go func() {
defer wg.Done()
http.Serve(cfg, tkr)
}()
}
if cfg.UDPListenAddr != "" {
wg.Add(1)
go func() {
defer wg.Done()
udp.Serve(cfg, tkr)
}()
}
wg.Wait()
if err := tkr.Close(); err != nil {
glog.Errorf("Failed to shut down tracker cleanly: %s", err.Error())
}
glog.Info("Gracefully shut down")
2013-06-22 01:31:32 +02:00
}