2014-07-06 23:56:54 +02:00
|
|
|
// Copyright 2014 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"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
|
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"
|
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"
|
2014-07-17 06:09:56 +02:00
|
|
|
_ "github.com/chihaya/chihaya/tracker/memory"
|
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() {
|
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(&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.
|
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
|
|
|
|
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 {
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.Fatalf("Failed to create profile file: %s\n", err)
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-06-24 09:59:30 +02:00
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
pprof.StartCPUProfile(f)
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.Info("Started profiling")
|
2014-06-26 23:19:18 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pprof.StopCPUProfile()
|
2014-07-16 17:52:59 +02:00
|
|
|
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 {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Serve(cfg, tkr)
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.Info("Gracefully shut down")
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|