Add unix socket support

This commit is contained in:
tidwall 2021-09-07 05:51:15 -07:00
parent a828f6db4d
commit a737a78d6f
2 changed files with 56 additions and 33 deletions

View File

@ -252,6 +252,7 @@ Developer Options:
dir string dir string
port int port int
host string host string
unixSocket string
verbose bool verbose bool
veryVerbose bool veryVerbose bool
quiet bool quiet bool
@ -261,13 +262,14 @@ Developer Options:
pprofport int 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(&pidfile, "pidfile", "", "A file that contains the pid")
flag.StringVar(&host, "h", "", "The listening host.") flag.StringVar(&host, "h", "", "The listening host")
flag.StringVar(&dir, "d", "data", "The data directory.") flag.StringVar(&unixSocket, "s", "", "Listen on a unix socket")
flag.BoolVar(&verbose, "v", false, "Enable verbose logging.") flag.StringVar(&dir, "d", "data", "The data directory")
flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.") flag.BoolVar(&verbose, "v", false, "Enable verbose logging")
flag.BoolVar(&veryVerbose, "vv", false, "Enable very 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.IntVar(&pprofport, "pprofport", 0, "pprofport http at port")
flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`") flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`") flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`")
@ -277,7 +279,9 @@ Developer Options:
if quiet { if quiet {
logw = ioutil.Discard logw = ioutil.Discard
} }
log.SetOutput(logw) log.SetOutput(logw)
if quiet { if quiet {
log.Level = 0 log.Level = 0
} else if veryVerbose { } else if veryVerbose {
@ -344,6 +348,10 @@ Developer Options:
}() }()
} }
if unixSocket != "" {
port = 0
}
// pid file // pid file
var pidferr error var pidferr error
var pidcleanedup bool var pidcleanedup bool
@ -364,9 +372,7 @@ Developer Options:
} }
defer pidcleanup() defer pidcleanup()
if pidfile != "" { if pidfile != "" {
pidferr := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
if pidferr == nil {
}
} }
c := make(chan os.Signal, 1) 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, ` fmt.Fprintf(logw, `
_______ _______ _____ _ _ ___ ___
| | | |_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s)
|____ | _ | Tile38 %s%s %d bit (%s/%s) | | | | | -_|_ | . | %s%s, PID: %d
| | | %sPort: %d, PID: %d |_| |_|_|___|___|___| tile38.com
|____ | _ |
| | | tile38.com Please consider sponsoring Tile38 development, especially if your company
|_______|_______| benefits from this software. Visit tile38.com/sponsor today to learn more.
`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid())
`, core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd,
saddr, os.Getpid())
if pidferr != nil { if pidferr != nil {
log.Warnf("pidfile: %v", pidferr) log.Warnf("pidfile: %v", pidferr)
} }
if showEvioDisabled { if showEvioDisabled {
// we don't currently support evio in Tile38
log.Warnf("evio is not currently supported") log.Warnf("evio is not currently supported")
} }
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")
} }
opts := server.Options{ opts := server.Options{
Host: host, Host: host,
Port: port, Port: port,
Dir: dir, Dir: dir,
UseHTTP: httpTransport, UseHTTP: httpTransport,
MetricsAddr: *metricsAddr, MetricsAddr: *metricsAddr,
UnixSocketPath: unixSocket,
} }
if err := server.Serve(opts); err != nil { if err := server.Serve(opts); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -72,6 +72,7 @@ type commandDetails struct {
// Server is a tile38 controller // Server is a tile38 controller
type Server struct { type Server struct {
// static values // static values
unix string
host string host string
port int port int
http bool http bool
@ -134,12 +135,14 @@ type Server struct {
monconns map[net.Conn]bool // monitor connections monconns map[net.Conn]bool // monitor connections
} }
// Options for Serve()
type Options struct { type Options struct {
Host string Host string
Port int Port int
Dir string Dir string
UseHTTP bool UseHTTP bool
MetricsAddr string MetricsAddr string
UnixSocketPath string // path for unix socket
} }
// Serve starts a new tile38 server // Serve starts a new tile38 server
@ -154,6 +157,7 @@ func Serve(opts Options) error {
// Initialize the server // Initialize the server
server := &Server{ server := &Server{
unix: opts.UnixSocketPath,
host: opts.Host, host: opts.Host,
port: opts.Port, port: opts.Port,
dir: opts.Dir, dir: opts.Dir,
@ -343,7 +347,15 @@ func (server *Server) isProtected() bool {
} }
func (server *Server) netServe() error { 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 { if err != nil {
return err return err
} }