Skip to content

Commit

Permalink
Add unix socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Sep 7, 2021
1 parent a828f6d commit a737a78
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
65 changes: 38 additions & 27 deletions cmd/tile38-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ Developer Options:
dir string
port int
host string
unixSocket string
verbose bool
veryVerbose bool
quiet bool
Expand All @@ -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`")
Expand All @@ -277,7 +279,9 @@ Developer Options:
if quiet {
logw = ioutil.Discard
}

log.SetOutput(logw)

if quiet {
log.Level = 0
} else if veryVerbose {
Expand Down Expand Up @@ -344,6 +348,10 @@ Developer Options:
}()
}

if unixSocket != "" {
port = 0
}

// pid file
var pidferr error
var pidcleanedup bool
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a737a78

Please sign in to comment.