Use struct for server options

This commit is contained in:
tidwall 2021-09-06 08:55:13 -07:00
parent c87fd4836e
commit f9e6f74753
4 changed files with 44 additions and 15 deletions

View File

@ -419,7 +419,14 @@ Developer Options:
if showThreadsDisabled { if showThreadsDisabled {
log.Warnf("thread flag is deprecated use GOMAXPROCS to set number of threads instead") log.Warnf("thread flag is deprecated use GOMAXPROCS to set number of threads instead")
} }
if err := server.Serve(host, port, dir, httpTransport, *metricsAddr); err != nil { opts := server.Options{
Host: host,
Port: port,
Dir: dir,
UseHTTP: httpTransport,
MetricsAddr: *metricsAddr,
}
if err := server.Serve(opts); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -134,21 +134,29 @@ type Server struct {
monconns map[net.Conn]bool // monitor connections monconns map[net.Conn]bool // monitor connections
} }
type Options struct {
Host string
Port int
Dir string
UseHTTP bool
MetricsAddr string
}
// Serve starts a new tile38 server // Serve starts a new tile38 server
func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string) error { func Serve(opts Options) error {
if core.AppendFileName == "" { if core.AppendFileName == "" {
core.AppendFileName = path.Join(dir, "appendonly.aof") core.AppendFileName = path.Join(opts.Dir, "appendonly.aof")
} }
if core.QueueFileName == "" { if core.QueueFileName == "" {
core.QueueFileName = path.Join(dir, "queue.db") core.QueueFileName = path.Join(opts.Dir, "queue.db")
} }
log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA) log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
// Initialize the server // Initialize the server
server := &Server{ server := &Server{
host: host, host: opts.Host,
port: port, port: opts.Port,
dir: dir, dir: opts.Dir,
follows: make(map[*bytes.Buffer]bool), follows: make(map[*bytes.Buffer]bool),
fcond: sync.NewCond(&sync.Mutex{}), fcond: sync.NewCond(&sync.Mutex{}),
lives: make(map[*liveBuffer]bool), lives: make(map[*liveBuffer]bool),
@ -160,7 +168,7 @@ func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string)
aofconnM: make(map[net.Conn]io.Closer), aofconnM: make(map[net.Conn]io.Closer),
started: time.Now(), started: time.Now(),
conns: make(map[int]*Client), conns: make(map[int]*Client),
http: useHTTP, http: opts.UseHTTP,
pubsub: newPubsub(), pubsub: newPubsub(),
monconns: make(map[net.Conn]bool), monconns: make(map[net.Conn]bool),
cols: btree.NewNonConcurrent(byCollectionKey), cols: btree.NewNonConcurrent(byCollectionKey),
@ -180,11 +188,11 @@ func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string)
server.luapool = server.newPool() server.luapool = server.newPool()
defer server.luapool.Shutdown() defer server.luapool.Shutdown()
if err := os.MkdirAll(dir, 0700); err != nil { if err := os.MkdirAll(opts.Dir, 0700); err != nil {
return err return err
} }
var err error var err error
server.config, err = loadConfig(filepath.Join(dir, "config")) server.config, err = loadConfig(filepath.Join(opts.Dir, "config"))
if err != nil { if err != nil {
return err return err
} }
@ -290,12 +298,12 @@ func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string)
server.followc.get()) server.followc.get())
} }
if metricsAddr != "" { if opts.MetricsAddr != "" {
log.Infof("Listening for metrics at: %s", metricsAddr) log.Infof("Listening for metrics at: %s", opts.MetricsAddr)
go func() { go func() {
http.HandleFunc("/", server.MetricsIndexHandler) http.HandleFunc("/", server.MetricsIndexHandler)
http.HandleFunc("/metrics", server.MetricsHandler) http.HandleFunc("/metrics", server.MetricsHandler)
log.Fatal(http.ListenAndServe(metricsAddr, nil)) log.Fatal(http.ListenAndServe(opts.MetricsAddr, nil))
}() }()
} }

View File

@ -100,7 +100,14 @@ func main() {
func startTile38Server() { func startTile38Server() {
log.Println("start tile38 server") log.Println("start tile38 server")
err := server.Serve("localhost", tile38Port, "data", false, "") opts := server.Options{
Host: "localhost",
Port: tile38Port,
Dir: "data",
UseHTTP: false,
MetricsAddr: "",
}
err := server.Serve(opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -58,7 +58,14 @@ func mockOpenServer(silent bool) (*mockServer, error) {
s := &mockServer{port: port} s := &mockServer{port: port}
tlog.SetOutput(logOutput) tlog.SetOutput(logOutput)
go func() { go func() {
if err := server.Serve("localhost", port, dir, true, ":4321"); err != nil { opts := server.Options{
Host: "localhost",
Port: port,
Dir: dir,
UseHTTP: true,
MetricsAddr: ":4321",
}
if err := server.Serve(opts); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}() }()