Skip to content

Commit

Permalink
daemon: feat: add TCP as a fallback if unix domain sockets not work
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 3, 2024
1 parent a0f8379 commit cfd87e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
DEF_MAX_PARTS = 200
DEF_MAX_CONNS = 24
DEF_TIMEOUT = time.Second * 30
DEF_PORT = 3849
)

const DESCRIPTION = `
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func daemon(ctx *cli.Context) error {
common.PrintRuntimeErr(ctx, "daemon", "new_api", err)
return nil
}
serv := server.NewServer(l)
serv := server.NewServer(l, DEF_PORT)
s.RegisterHandlers(serv)
return serv.Start()
}
17 changes: 14 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ type Server struct {
log *log.Logger
pool *Pool
handler map[common.UpdateType]HandlerFunc
port int
}

func NewServer(l *log.Logger) *Server {
func NewServer(l *log.Logger, port int) *Server {
return &Server{
log: l,
pool: NewPool(l),
handler: make(map[common.UpdateType]HandlerFunc),
port: port,
}
}

Expand All @@ -31,12 +33,21 @@ func (s *Server) RegisterHandler(method common.UpdateType, handler HandlerFunc)
func (s *Server) Start() error {
socketPath := filepath.Join(os.TempDir(), "warpdl.sock")
_ = os.Remove(socketPath)
l, err := net.ListenUnix("unix", &net.UnixAddr{
var (
l net.Listener
err error
)
l, err = net.ListenUnix("unix", &net.UnixAddr{
Name: socketPath,
Net: "unix",
})
if err != nil {
return err
s.log.Println("Error occured while using unix socket: ", err.Error())
s.log.Println("Trying to use tcp socket")
l, err = net.Listen("tcp", fmt.Sprintf("localhost:%d", s.port))
if err != nil {
return fmt.Errorf("error listening: %s", err.Error())
}
}
defer l.Close()
for {
Expand Down

0 comments on commit cfd87e8

Please sign in to comment.