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 {
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)
}
}

View File

@ -134,21 +134,29 @@ type Server struct {
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
func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string) error {
func Serve(opts Options) error {
if core.AppendFileName == "" {
core.AppendFileName = path.Join(dir, "appendonly.aof")
core.AppendFileName = path.Join(opts.Dir, "appendonly.aof")
}
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)
// Initialize the server
server := &Server{
host: host,
port: port,
dir: dir,
host: opts.Host,
port: opts.Port,
dir: opts.Dir,
follows: make(map[*bytes.Buffer]bool),
fcond: sync.NewCond(&sync.Mutex{}),
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),
started: time.Now(),
conns: make(map[int]*Client),
http: useHTTP,
http: opts.UseHTTP,
pubsub: newPubsub(),
monconns: make(map[net.Conn]bool),
cols: btree.NewNonConcurrent(byCollectionKey),
@ -180,11 +188,11 @@ func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string)
server.luapool = server.newPool()
defer server.luapool.Shutdown()
if err := os.MkdirAll(dir, 0700); err != nil {
if err := os.MkdirAll(opts.Dir, 0700); err != nil {
return err
}
var err error
server.config, err = loadConfig(filepath.Join(dir, "config"))
server.config, err = loadConfig(filepath.Join(opts.Dir, "config"))
if err != nil {
return err
}
@ -290,12 +298,12 @@ func Serve(host string, port int, dir string, useHTTP bool, metricsAddr string)
server.followc.get())
}
if metricsAddr != "" {
log.Infof("Listening for metrics at: %s", metricsAddr)
if opts.MetricsAddr != "" {
log.Infof("Listening for metrics at: %s", opts.MetricsAddr)
go func() {
http.HandleFunc("/", server.MetricsIndexHandler)
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() {
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 {
log.Fatal(err)
}

View File

@ -58,7 +58,14 @@ func mockOpenServer(silent bool) (*mockServer, error) {
s := &mockServer{port: port}
tlog.SetOutput(logOutput)
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)
}
}()