2014-07-06 17:56:54 -04:00
|
|
|
// Copyright 2014 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 main
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-06-24 03:59:30 -04:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-01 21:40:29 -04:00
|
|
|
"github.com/chihaya/chihaya/http"
|
2014-07-15 00:22:04 -04:00
|
|
|
|
|
|
|
// All drivers are imported here.
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/backend/noop"
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/tracker/memory"
|
2013-06-21 19:31:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-07-16 00:01:10 -04:00
|
|
|
maxprocs int
|
2014-07-16 02:24:00 -04:00
|
|
|
profile string
|
2013-06-23 03:56:28 -04:00
|
|
|
configPath string
|
2013-06-21 19:31:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-07-16 02:24:00 -04: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-21 19:31:32 -04:00
|
|
|
}
|
|
|
|
|
2014-07-15 23:06:07 -04:00
|
|
|
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
|
|
|
|
2014-07-16 00:01:10 -04:00
|
|
|
runtime.GOMAXPROCS(maxprocs)
|
2014-07-16 11:52:59 -04:00
|
|
|
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 {
|
2014-07-16 11:52:59 -04:00
|
|
|
glog.Fatalf("Failed to create profile file: %s\n", err)
|
2013-06-21 19:31:32 -04:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-06-24 03:59:30 -04:00
|
|
|
|
2013-06-21 19:31:32 -04:00
|
|
|
pprof.StartCPUProfile(f)
|
2014-07-16 11:52:59 -04:00
|
|
|
glog.Info("Started profiling")
|
2014-06-26 17:19:18 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pprof.StopCPUProfile()
|
2014-07-16 11:52:59 -04:00
|
|
|
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 {
|
2014-07-16 11:52:59 -04:00
|
|
|
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 {
|
2014-07-16 11:52:59 -04:00
|
|
|
glog.V(1).Info("Using default config")
|
2014-07-01 21:40:29 -04:00
|
|
|
} else {
|
2014-07-16 11:52:59 -04:00
|
|
|
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-01 21:40:29 -04:00
|
|
|
http.Serve(cfg)
|
2014-07-16 11:52:59 -04:00
|
|
|
glog.Info("Gracefully shut down")
|
2013-06-21 19:31:32 -04:00
|
|
|
}
|
2014-07-15 23:06:07 -04:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
Boot()
|
|
|
|
}
|