From f9e6f74753a2157572c9f2d6c60510e529ef2751 Mon Sep 17 00:00:00 2001 From: tidwall Date: Mon, 6 Sep 2021 08:55:13 -0700 Subject: [PATCH] Use struct for server options --- cmd/tile38-server/main.go | 9 ++++++++- internal/server/server.go | 32 ++++++++++++++++++++------------ tests/107/main.go | 9 ++++++++- tests/mock_test.go | 9 ++++++++- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index 4b2e14f4..04beab45 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -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) } } diff --git a/internal/server/server.go b/internal/server/server.go index d4e44c4f..922e52c4 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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)) }() } diff --git a/tests/107/main.go b/tests/107/main.go index 23df4ce1..202db9f0 100644 --- a/tests/107/main.go +++ b/tests/107/main.go @@ -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) } diff --git a/tests/mock_test.go b/tests/mock_test.go index c589e6a9..f3a07cf2 100644 --- a/tests/mock_test.go +++ b/tests/mock_test.go @@ -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) } }()