Skip to content

Commit f0cf2fb

Browse files
authored
[shell-operator] Close resources correctly with dcql -w (#798)
Signed-off-by: kerimov <brileyforbusiness@gmail.com>
1 parent 638e9bc commit f0cf2fb

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

pkg/debug/client.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,58 @@ import (
77
"io"
88
"net"
99
"net/http"
10+
"time"
1011

1112
"github.com/flant/shell-operator/pkg/app"
1213
utils "github.com/flant/shell-operator/pkg/utils/file"
1314
)
1415

1516
type Client struct {
1617
SocketPath string
18+
httpClient *http.Client
1719
}
1820

19-
func NewClient() *Client {
20-
return &Client{}
21-
}
22-
23-
func (c *Client) WithSocketPath(path string) {
24-
c.SocketPath = path
25-
}
26-
27-
func (c *Client) newHttpClient() (http.Client, error) {
28-
exists, err := utils.FileExists(c.SocketPath)
21+
func NewClient(socketPath string) (*Client, error) {
22+
exists, err := utils.FileExists(socketPath)
2923
if err != nil {
30-
return http.Client{}, fmt.Errorf("check debug socket '%s': %s", c.SocketPath, err)
24+
return nil, fmt.Errorf("check debug socket '%s': %s", socketPath, err)
3125
}
3226
if !exists {
33-
return http.Client{}, fmt.Errorf("debug socket '%s' is not exists", c.SocketPath)
27+
return nil, fmt.Errorf("debug socket '%s' is not exists", socketPath)
3428
}
3529

36-
return http.Client{
30+
client := &http.Client{
3731
Transport: &http.Transport{
38-
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
39-
return net.Dial("unix", c.SocketPath)
32+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
33+
dialer := &net.Dialer{
34+
Timeout: 10 * time.Second,
35+
}
36+
return dialer.DialContext(ctx, "unix", socketPath)
4037
},
38+
DisableKeepAlives: true,
4139
},
40+
}
41+
42+
return &Client{
43+
SocketPath: socketPath,
44+
httpClient: client,
4245
}, nil
4346
}
4447

45-
func DefaultClient() *Client {
46-
cl := NewClient()
47-
cl.WithSocketPath(app.DebugUnixSocket)
48-
return cl
48+
func (c *Client) Close() {
49+
if c.httpClient != nil {
50+
if transport, ok := c.httpClient.Transport.(*http.Transport); ok {
51+
transport.CloseIdleConnections()
52+
}
53+
}
4954
}
5055

51-
func (c *Client) Get(url string) ([]byte, error) {
52-
httpc, err := c.newHttpClient()
53-
if err != nil {
54-
return nil, err
55-
}
56+
func DefaultClient() (*Client, error) {
57+
return NewClient(app.DebugUnixSocket)
58+
}
5659

57-
resp, err := httpc.Get(url)
60+
func (c *Client) Get(url string) ([]byte, error) {
61+
resp, err := c.httpClient.Get(url)
5862
if err != nil {
5963
return nil, err
6064
}
@@ -69,12 +73,7 @@ func (c *Client) Get(url string) ([]byte, error) {
6973
}
7074

7175
func (c *Client) Post(targetUrl string, data map[string][]string) ([]byte, error) {
72-
httpc, err := c.newHttpClient()
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
resp, err := httpc.PostForm(targetUrl, data)
76+
resp, err := c.httpClient.PostForm(targetUrl, data)
7877
if err != nil {
7978
return nil, err
8079
}

pkg/debug/debug-cmd.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
2828
var refreshInterval time.Duration
2929
output := termenv.NewOutput(os.Stdout)
3030

31+
client, err := DefaultClient()
32+
if err != nil {
33+
return err
34+
}
35+
defer client.Close()
36+
3137
if watch {
3238
c := make(chan os.Signal, 1)
3339
signal.Notify(c, os.Interrupt)
3440

3541
go func() {
3642
<-c
43+
client.Close()
3744
output.ExitAltScreen()
3845
os.Exit(0)
3946
}()
@@ -49,10 +56,10 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
4956
}
5057
}
5158

52-
client := Queue(DefaultClient())
59+
queueClient := Queue(client)
5360

5461
for {
55-
out, err := client.List(outputFormat, showEmpty)
62+
out, err := queueClient.List(outputFormat, showEmpty)
5663
if err != nil {
5764
return err
5865
}
@@ -85,7 +92,11 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
8592

8693
queueMainCmd := queueCmd.Command("main", "Dump tasks in the main queue.").
8794
Action(func(_ *kingpin.ParseContext) error {
88-
out, err := Queue(DefaultClient()).Main(outputFormat)
95+
client, err := DefaultClient()
96+
if err != nil {
97+
return err
98+
}
99+
out, err := Queue(client).Main(outputFormat)
89100
if err != nil {
90101
return err
91102
}
@@ -100,7 +111,11 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
100111

101112
configListCmd := configCmd.Command("list", "List available runtime parameters.").
102113
Action(func(_ *kingpin.ParseContext) error {
103-
out, err := Config(DefaultClient()).List(outputFormat)
114+
client, err := DefaultClient()
115+
if err != nil {
116+
return err
117+
}
118+
out, err := Config(client).List(outputFormat)
104119
if err != nil {
105120
return err
106121
}
@@ -115,7 +130,11 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
115130
var paramDuration time.Duration
116131
configSetCmd := configCmd.Command("set", "Set runtime parameter.").
117132
Action(func(_ *kingpin.ParseContext) error {
118-
out, err := Config(DefaultClient()).Set(paramName, paramValue, paramDuration)
133+
client, err := DefaultClient()
134+
if err != nil {
135+
return err
136+
}
137+
out, err := Config(client).Set(paramName, paramValue, paramDuration)
119138
if err != nil {
120139
return err
121140
}
@@ -131,8 +150,12 @@ func DefineDebugCommands(kpApp *kingpin.Application) {
131150
var rawUrl string
132151
rawCommand := app.CommandWithDefaultUsageTemplate(kpApp, "raw", "Make a raw request to debug endpoint.").
133152
Action(func(_ *kingpin.ParseContext) error {
153+
client, err := DefaultClient()
154+
if err != nil {
155+
return err
156+
}
134157
url := fmt.Sprintf("http://unix%s", rawUrl)
135-
resp, err := DefaultClient().Get(url)
158+
resp, err := client.Get(url)
136159
if err != nil {
137160
return err
138161
}
@@ -148,7 +171,11 @@ func DefineDebugCommandsSelf(kpApp *kingpin.Application) {
148171
hookCmd := app.CommandWithDefaultUsageTemplate(kpApp, "hook", "Actions for hooks")
149172
hookListCmd := hookCmd.Command("list", "List all hooks.").
150173
Action(func(_ *kingpin.ParseContext) error {
151-
outBytes, err := Hook(DefaultClient()).List(outputFormat)
174+
client, err := DefaultClient()
175+
if err != nil {
176+
return err
177+
}
178+
outBytes, err := Hook(client).List(outputFormat)
152179
if err != nil {
153180
return err
154181
}
@@ -162,7 +189,11 @@ func DefineDebugCommandsSelf(kpApp *kingpin.Application) {
162189
var hookName string
163190
hookSnapshotCmd := hookCmd.Command("snapshot", "Dump hook snapshots.").
164191
Action(func(_ *kingpin.ParseContext) error {
165-
outBytes, err := Hook(DefaultClient()).Name(hookName).Snapshots(outputFormat)
192+
client, err := DefaultClient()
193+
if err != nil {
194+
return err
195+
}
196+
outBytes, err := Hook(client).Name(hookName).Snapshots(outputFormat)
166197
if err != nil {
167198
return err
168199
}

0 commit comments

Comments
 (0)