From a737a78d6f3fe00509bddd7094112e49061e1db3 Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 7 Sep 2021 05:51:15 -0700 Subject: [PATCH] Add unix socket support --- cmd/tile38-server/main.go | 65 +++++++++++++++++++++++---------------- internal/server/server.go | 24 +++++++++++---- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index 04beab45..1fc2a9bd 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -252,6 +252,7 @@ Developer Options: dir string port int host string + unixSocket string verbose bool veryVerbose bool quiet bool @@ -261,13 +262,14 @@ Developer Options: pprofport int ) - flag.IntVar(&port, "p", 9851, "The listening port.") + flag.IntVar(&port, "p", 9851, "The listening port") flag.StringVar(&pidfile, "pidfile", "", "A file that contains the pid") - flag.StringVar(&host, "h", "", "The listening host.") - flag.StringVar(&dir, "d", "data", "The data directory.") - flag.BoolVar(&verbose, "v", false, "Enable verbose logging.") - flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.") - flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.") + flag.StringVar(&host, "h", "", "The listening host") + flag.StringVar(&unixSocket, "s", "", "Listen on a unix socket") + flag.StringVar(&dir, "d", "data", "The data directory") + flag.BoolVar(&verbose, "v", false, "Enable verbose logging") + flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent") + flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging") flag.IntVar(&pprofport, "pprofport", 0, "pprofport http at port") flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`") flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`") @@ -277,7 +279,9 @@ Developer Options: if quiet { logw = ioutil.Discard } + log.SetOutput(logw) + if quiet { log.Level = 0 } else if veryVerbose { @@ -344,6 +348,10 @@ Developer Options: }() } + if unixSocket != "" { + port = 0 + } + // pid file var pidferr error var pidcleanedup bool @@ -364,9 +372,7 @@ Developer Options: } defer pidcleanup() if pidfile != "" { - pidferr := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) - if pidferr == nil { - } + ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) } c := make(chan os.Signal, 1) @@ -395,36 +401,41 @@ Developer Options: } }() - // _____ _ _ ___ ___ - // |_ _|_| |___|_ | . | - // | | | | | -_|_ | . | - // |_| |_|_|___|___|___| + var saddr string + if unixSocket != "" { + saddr = fmt.Sprintf("Socket: %s", unixSocket) + } else { + saddr = fmt.Sprintf("Port: %d", port) + } + fmt.Fprintf(logw, ` - _______ _______ - | | | - |____ | _ | Tile38 %s%s %d bit (%s/%s) - | | | %sPort: %d, PID: %d - |____ | _ | - | | | tile38.com - |_______|_______| -`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid()) + _____ _ _ ___ ___ + |_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s) + | | | | | -_|_ | . | %s%s, PID: %d + |_| |_|_|___|___|___| tile38.com + +Please consider sponsoring Tile38 development, especially if your company +benefits from this software. Visit tile38.com/sponsor today to learn more. + +`, core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, + saddr, os.Getpid()) if pidferr != nil { log.Warnf("pidfile: %v", pidferr) } if showEvioDisabled { - // we don't currently support evio in Tile38 log.Warnf("evio is not currently supported") } if showThreadsDisabled { log.Warnf("thread flag is deprecated use GOMAXPROCS to set number of threads instead") } opts := server.Options{ - Host: host, - Port: port, - Dir: dir, - UseHTTP: httpTransport, - MetricsAddr: *metricsAddr, + Host: host, + Port: port, + Dir: dir, + UseHTTP: httpTransport, + MetricsAddr: *metricsAddr, + UnixSocketPath: unixSocket, } if err := server.Serve(opts); err != nil { log.Fatal(err) diff --git a/internal/server/server.go b/internal/server/server.go index 922e52c4..98df3f84 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -72,6 +72,7 @@ type commandDetails struct { // Server is a tile38 controller type Server struct { // static values + unix string host string port int http bool @@ -134,12 +135,14 @@ type Server struct { monconns map[net.Conn]bool // monitor connections } +// Options for Serve() type Options struct { - Host string - Port int - Dir string - UseHTTP bool - MetricsAddr string + Host string + Port int + Dir string + UseHTTP bool + MetricsAddr string + UnixSocketPath string // path for unix socket } // Serve starts a new tile38 server @@ -154,6 +157,7 @@ func Serve(opts Options) error { // Initialize the server server := &Server{ + unix: opts.UnixSocketPath, host: opts.Host, port: opts.Port, dir: opts.Dir, @@ -343,7 +347,15 @@ func (server *Server) isProtected() bool { } func (server *Server) netServe() error { - ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", server.host, server.port)) + var ln net.Listener + var err error + if server.unix != "" { + os.RemoveAll(server.unix) + ln, err = net.Listen("unix", server.unix) + } else { + tcpAddr := fmt.Sprintf("%s:%d", server.host, server.port) + ln, err = net.Listen("tcp", tcpAddr) + } if err != nil { return err }