Skip to content

Commit 63fb886

Browse files
Refactor listener provider system with platform-specific implementations
• Split provider detection logic into platform-specific files using build tags • Move DetectProvider function from shared provider.go to platform files • Consolidate TCP provider implementation into provider_other.go • Add build constraints for darwin (launchd) and linux (systemd) providers • Remove shared tcp.go file and integrate TCP provider into provider_other.go • Move syscall utilities to appropriate platform-specific files • Improve error handling with specific messages when no valid listener detected Co-authored-by: construct-agent <agent@construct.sh>
1 parent 81a2528 commit 63fb886

5 files changed

Lines changed: 79 additions & 70 deletions

File tree

shared/listener/provider.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,10 @@ package listener
22

33
import (
44
"net"
5-
"runtime"
6-
"syscall"
75
)
86

97
type Provider interface {
108
Create() (net.Listener, error)
119
Close() error
1210
ActivationType() string
1311
}
14-
15-
func DetectProvider(httpAddress, unixSocket string) (Provider, error) {
16-
if unixSocket != "" {
17-
return NewUnixSocketProvider(unixSocket), nil
18-
}
19-
20-
if httpAddress != "" {
21-
return NewTCPListenerProvider(httpAddress), nil
22-
}
23-
24-
switch runtime.GOOS {
25-
case "darwin":
26-
if IsLaunchdSocketActivation() {
27-
return NewLaunchdSocketProvider(), nil
28-
}
29-
case "linux":
30-
if IsSystemdSocketActivation() {
31-
return NewSystemdSocketProvider(), nil
32-
}
33-
}
34-
35-
return NewTCPListenerProvider("localhost:29333"), nil
36-
}
37-
38-
func isSocket(fd uintptr) bool {
39-
var stat syscall.Stat_t
40-
err := syscall.Fstat(int(fd), &stat)
41-
if err != nil {
42-
return false
43-
}
44-
45-
return stat.Mode&syscall.S_IFMT == syscall.S_IFSOCK
46-
}

shared/listener/provider_launchd.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build darwin
2+
13
package listener
24

35
import (
@@ -35,3 +37,19 @@ func (p *LaunchdSocketProvider) ActivationType() string {
3537
func IsLaunchdSocketActivation() bool {
3638
return strings.HasPrefix(os.Getenv("XPC_SERVICE_NAME"), "sh.construct.daemon.")
3739
}
40+
41+
func DetectProvider(httpAddress, unixSocket string) (Provider, error) {
42+
if unixSocket != "" {
43+
return NewUnixSocketProvider(unixSocket), nil
44+
}
45+
46+
if httpAddress != "" {
47+
return NewTCPListenerProvider(httpAddress), nil
48+
}
49+
50+
if IsLaunchdSocketActivation() {
51+
return NewLaunchdSocketProvider(), nil
52+
}
53+
54+
return nil, fmt.Errorf("no valid listener has been detected. Specify either a unix socket, tcp address or use launchd socket activation")
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,34 @@ func (p *UnixSocketProvider) Close() error {
4545
func (p *UnixSocketProvider) ActivationType() string {
4646
return "unix"
4747
}
48+
49+
50+
type TCPProvider struct {
51+
httpAddress string
52+
}
53+
54+
var _ Provider = (*TCPProvider)(nil)
55+
56+
func NewTCPListenerProvider(httpAddress string) *TCPProvider {
57+
return &TCPProvider{
58+
httpAddress: httpAddress,
59+
}
60+
}
61+
62+
func (p *TCPProvider) Create() (net.Listener, error) {
63+
listener, err := net.Listen("tcp", p.httpAddress)
64+
if err != nil {
65+
return nil, fmt.Errorf("failed to listen on tcp: %w", err)
66+
}
67+
68+
return listener, nil
69+
}
70+
71+
func (p *TCPProvider) Close() error {
72+
return nil
73+
}
74+
75+
func (p *TCPProvider) ActivationType() string {
76+
return "tcp"
77+
}
78+

shared/listener/provider_systemd.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
//go:build linux
2+
13
package listener
24

35
import (
46
"fmt"
57
"net"
68
"os"
79
"strconv"
10+
11+
"syscall"
812
)
913

1014
const systemdSocketFD = 3
@@ -66,3 +70,29 @@ func IsSystemdSocketActivation() bool {
6670

6771
return false
6872
}
73+
74+
func isSocket(fd uintptr) bool {
75+
var stat syscall.Stat_t
76+
err := syscall.Fstat(int(fd), &stat)
77+
if err != nil {
78+
return false
79+
}
80+
81+
return stat.Mode&syscall.S_IFMT == syscall.S_IFSOCK
82+
}
83+
84+
func DetectProvider(httpAddress, unixSocket string) (Provider, error) {
85+
if unixSocket != "" {
86+
return NewUnixSocketProvider(unixSocket), nil
87+
}
88+
89+
if httpAddress != "" {
90+
return NewTCPListenerProvider(httpAddress), nil
91+
}
92+
93+
if IsSystemdSocketActivation() {
94+
return NewSystemdSocketProvider(), nil
95+
}
96+
97+
return nil, fmt.Errorf("no valid listener has been detected. Specify either a unix socket, tcp address or use systemd socket activation")
98+
}

shared/listener/tcp.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)