Skip to content

Commit

Permalink
Enhancements about unix network listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Sep 24, 2019
1 parent b0cfc42 commit 747b673
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func New(cfg *Config) *Atreugo {
if cfg.GracefulShutdown && cfg.ReadTimeout <= 0 {
cfg.ReadTimeout = defaultReadTimeout
}
cfg.socketFileMode = 0666

log := logger.New(cfg.LogName, cfg.LogLevel, os.Stderr)

Expand Down
34 changes: 34 additions & 0 deletions listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ func TestAtreugo_getListener(t *testing.T) {
err: false,
},
},
{
name: "Unix",
args: args{
addr: "/tmp/test.sock",
network: "unix",
},
want: want{
addr: "/tmp/test.sock",
network: "unix",
err: false,
},
},
{
name: "UnixRemoveError",
args: args{
addr: "/tmp/data.sock",
network: "unix",
},
want: want{
addr: "/tmp/data.sock",
network: "unix",
err: true,
},
},
{
name: "UnixChmodError",
args: args{
addr: "345&%·%&%&/%&(",
network: "unix",
},
want: want{
err: true,
},
},
{
name: "Error",
args: args{
Expand Down
18 changes: 17 additions & 1 deletion listener_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package atreugo

import (
"fmt"
"net"
"os"

"github.com/valyala/fasthttp/reuseport"
)
Expand All @@ -13,5 +15,19 @@ func (s *Atreugo) getListener() (net.Listener, error) {
return reuseport.Listen(s.cfg.Network, s.cfg.Addr)
}

return net.Listen(s.cfg.Network, s.cfg.Addr)
if s.cfg.Network == "unix" {
if err := os.Remove(s.cfg.Addr); err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("unexpected error when trying to remove unix socket file %q: %s", s.cfg.Addr, err)
}
}

ln, err := net.Listen(s.cfg.Network, s.cfg.Addr)

if s.cfg.Network == "unix" {
if err := os.Chmod(s.cfg.Addr, s.cfg.socketFileMode); err != nil {
return nil, fmt.Errorf("cannot chmod %#o for %q: %s", s.cfg.socketFileMode, s.cfg.Addr, err)
}
}

return ln, err
}
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package atreugo

import (
"net"
"os"
"time"

fastrouter "github.com/fasthttp/router"
Expand Down Expand Up @@ -261,6 +262,8 @@ type Config struct {
// http connections to WS and connection goes to another handler,
// which will close it when needed.
KeepHijackedConns bool

socketFileMode os.FileMode
}

// StaticFS represents settings for serving static files
Expand Down

0 comments on commit 747b673

Please sign in to comment.