mirror of https://github.com/go-gost/gost.git
[main] Add -- cmd flag separator to start different Gost instances
parent
84255850b6
commit
9e492143cf
105
cmd/gost/main.go
105
cmd/gost/main.go
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
"strings"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
|
|
@ -16,9 +18,43 @@ import (
|
||||||
xmetrics "github.com/go-gost/x/metrics"
|
xmetrics "github.com/go-gost/x/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type WorkerSync struct {
|
||||||
|
wg sync.WaitGroup
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
log logger.Logger
|
log logger.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log = xlogger.NewLogger()
|
||||||
|
logger.SetDefault(log)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var ws WorkerSync
|
||||||
|
|
||||||
|
ws.wg.Add(1) // Gost must exit if any of the workers exit
|
||||||
|
|
||||||
|
// Split os.Args using -- and create a worker with each slice
|
||||||
|
args := strings.Split(" " + strings.Join(os.Args[1:], " ") + " ", " -- ")
|
||||||
|
if strings.Join(args, "") == "" {
|
||||||
|
// Fix to show gost help if the resulting array is empty
|
||||||
|
args[0] = " "
|
||||||
|
}
|
||||||
|
for wid, wargs := range args {
|
||||||
|
if wargs != "" {
|
||||||
|
go worker(wid, wargs, &ws)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ws.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func worker(id int, args string, ws *WorkerSync) {
|
||||||
|
defer ws.wg.Done()
|
||||||
|
|
||||||
|
var (
|
||||||
cfgFile string
|
cfgFile string
|
||||||
outputFormat string
|
outputFormat string
|
||||||
services stringList
|
services stringList
|
||||||
|
|
@ -26,36 +62,42 @@ var (
|
||||||
debug bool
|
debug bool
|
||||||
apiAddr string
|
apiAddr string
|
||||||
metricsAddr string
|
metricsAddr string
|
||||||
|
|
||||||
|
err error
|
||||||
|
|
||||||
|
cfg = &config.Config{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
init := func () error {
|
||||||
var printVersion bool
|
var printVersion bool
|
||||||
|
|
||||||
flag.Var(&services, "L", "service list")
|
wf := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
flag.Var(&nodes, "F", "chain node list")
|
|
||||||
flag.StringVar(&cfgFile, "C", "", "configure file")
|
wf.Var(&services, "L", "service list")
|
||||||
flag.BoolVar(&printVersion, "V", false, "print version")
|
wf.Var(&nodes, "F", "chain node list")
|
||||||
flag.StringVar(&outputFormat, "O", "", "output format, one of yaml|json format")
|
wf.StringVar(&cfgFile, "C", "", "configure file")
|
||||||
flag.BoolVar(&debug, "D", false, "debug mode")
|
wf.BoolVar(&printVersion, "V", false, "print version")
|
||||||
flag.StringVar(&apiAddr, "api", "", "api service address")
|
wf.StringVar(&outputFormat, "O", "", "output format, one of yaml|json format")
|
||||||
flag.StringVar(&metricsAddr, "metrics", "", "metrics service address")
|
wf.BoolVar(&debug, "D", false, "debug mode")
|
||||||
flag.Parse()
|
wf.StringVar(&apiAddr, "api", "", "api service address")
|
||||||
|
wf.StringVar(&metricsAddr, "metrics", "", "metrics service address")
|
||||||
|
|
||||||
|
wf.Parse(strings.Fields(args))
|
||||||
|
|
||||||
if printVersion {
|
if printVersion {
|
||||||
fmt.Fprintf(os.Stdout, "gost %s (%s %s/%s)\n",
|
fmt.Fprintf(os.Stdout, "gost %s (%s %s/%s)\n",
|
||||||
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
} else if wf.NFlag() == 0 {
|
||||||
|
wf.Usage()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log = xlogger.NewLogger()
|
main := func () error {
|
||||||
logger.SetDefault(log)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cfg := &config.Config{}
|
|
||||||
var err error
|
|
||||||
if len(services) > 0 || apiAddr != "" {
|
if len(services) > 0 || apiAddr != "" {
|
||||||
cfg, err = buildConfigFromCmd(services, nodes)
|
cfg, err = buildConfigFromCmd(id, services, nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -101,7 +143,8 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
addr := cfg.Profiling.Addr
|
addr := cfg.Profiling.Addr
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
addr = ":6060"
|
// Each worker uses a different profiling server
|
||||||
|
addr = fmt.Sprintf(":606%d", id)
|
||||||
}
|
}
|
||||||
log.Info("profiling server on ", addr)
|
log.Info("profiling server on ", addr)
|
||||||
log.Fatal(http.ListenAndServe(addr, nil))
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
|
|
@ -138,16 +181,34 @@ func main() {
|
||||||
|
|
||||||
parsing.BuildDefaultTLSConfig(cfg.TLS)
|
parsing.BuildDefaultTLSConfig(cfg.TLS)
|
||||||
|
|
||||||
services := buildService(cfg)
|
svcs := buildService(cfg)
|
||||||
for _, svc := range services {
|
for _, svc := range svcs {
|
||||||
svc := svc
|
svc := svc
|
||||||
go func() {
|
go func() {
|
||||||
svc.Serve()
|
svc.Serve()
|
||||||
svc.Close()
|
svc.Close()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
config.SetGlobal(cfg)
|
config.SetGlobal(cfg)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using mutex to avoid duplicated service creation race condition
|
||||||
|
ws.mu.Lock()
|
||||||
|
if err := init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := main(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ws.mu.Unlock()
|
||||||
|
|
||||||
|
// Allow local functions to be garbage-collected
|
||||||
|
init = nil
|
||||||
|
main = nil
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue