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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
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-15 06:22:04 +02:00
|
|
|
|
|
|
|
// All drivers are imported here.
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/backend/noop"
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/tracker/memory"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-07-16 06:01:10 +02:00
|
|
|
maxprocs int
|
2014-07-16 08:24:00 +02:00
|
|
|
profile string
|
2013-06-23 09:56:28 +02:00
|
|
|
configPath string
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-07-16 08:24:00 +02:00
|
|
|
flag.IntVar(&maxprocs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
|
|
|
|
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-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-16 06:01:10 +02:00
|
|
|
runtime.GOMAXPROCS(maxprocs)
|
2014-07-16 08:24:00 +02:00
|
|
|
glog.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-01 05:21:08 +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-02 03:40:29 +02:00
|
|
|
glog.Info("started profiling")
|
2014-06-26 23:19:18 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pprof.StopCPUProfile()
|
2014-07-02 03:40:29 +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-01 05:21:08 +02:00
|
|
|
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.Info("using default config")
|
|
|
|
} else {
|
|
|
|
glog.Infof("loaded config file: %s", configPath)
|
2013-06-24 06:18:59 +02:00
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-07-02 03:40:29 +02:00
|
|
|
http.Serve(cfg)
|
|
|
|
glog.Info("gracefully shutdown")
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2014-07-16 05:06:07 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
Boot()
|
|
|
|
}
|