tracker/chihaya.go

83 lines
1.8 KiB
Go
Raw Normal View History

2015-01-01 12:02:25 -05:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2013-06-21 19:31:32 -04: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-21 19:31:32 -04:00
import (
"flag"
"os"
"runtime"
"runtime/pprof"
2014-06-30 23:21:08 -04:00
"github.com/golang/glog"
2014-06-19 12:34:47 -04:00
"github.com/chihaya/chihaya/config"
2014-07-01 21:40:29 -04:00
"github.com/chihaya/chihaya/http"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
// See the README for how to import custom drivers.
_ "github.com/chihaya/chihaya/backend/noop"
2013-06-21 19:31:32 -04:00
)
var (
2014-07-25 16:58:26 -04:00
maxProcs int
profile string
configPath string
2013-06-21 19:31:32 -04:00
)
func init() {
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
2014-07-16 02:24:00 -04:00
flag.StringVar(&profile, "profile", "", "if non-empty, path to write profiling data")
flag.StringVar(&configPath, "config", "", "path to the configuration file")
2013-06-21 19:31:32 -04:00
}
2014-07-18 19:09:06 -04: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-13 21:27:20 -04:00
defer glog.Flush()
2013-06-21 19:31:32 -04:00
flag.Parse()
2014-07-02 00:45:17 -04:00
runtime.GOMAXPROCS(maxProcs)
glog.V(1).Info("Set max threads to ", maxProcs)
2013-06-21 19:31:32 -04:00
2014-07-16 02:24:00 -04:00
if profile != "" {
f, err := os.Create(profile)
2013-06-21 19:31:32 -04:00
if err != nil {
glog.Fatalf("Failed to create profile file: %s\n", err)
2013-06-21 19:31:32 -04:00
}
defer f.Close()
2013-06-21 19:31:32 -04:00
pprof.StartCPUProfile(f)
glog.Info("Started profiling")
2014-06-26 17:19:18 -04:00
defer func() {
pprof.StopCPUProfile()
glog.Info("Stopped profiling")
2014-06-26 17:19:18 -04:00
}()
2013-06-21 19:31:32 -04:00
}
2014-07-01 21:40:29 -04:00
cfg, err := config.Open(configPath)
2013-06-23 03:56:28 -04:00
if err != nil {
glog.Fatalf("Failed to parse configuration file: %s\n", err)
2013-06-23 03:56:28 -04:00
}
2014-07-01 21:40:29 -04:00
if cfg == &config.DefaultConfig {
glog.V(1).Info("Using default config")
2014-07-01 21:40:29 -04:00
} else {
glog.V(1).Infof("Loaded config file: %s", configPath)
2013-06-24 00:18:59 -04:00
}
2013-06-21 19:31:32 -04:00
2014-07-23 17:25:01 -04:00
stats.DefaultStats = stats.New(cfg.StatsConfig)
tkr, err := tracker.New(cfg)
if err != nil {
glog.Fatal("New: ", err)
}
http.Serve(cfg, tkr)
glog.Info("Gracefully shut down")
2013-06-21 19:31:32 -04:00
}