Skip to content

Commit c36975b

Browse files
committed
too many oprn files fix
1 parent 72238a1 commit c36975b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pkg/proxy/proxy.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Proxy struct {
6060
connections []*hookdecksdk.Connection
6161
webSocketClient *websocket.Client
6262
connectionTimer *time.Timer
63+
httpClient *http.Client
6364
}
6465

6566
func withSIGTERMCancel(ctx context.Context, onCancel func()) context.Context {
@@ -252,21 +253,17 @@ func (p *Proxy) processAttempt(msg websocket.IncomingMessage) {
252253
fmt.Println(webhookEvent.Body.Request.DataString)
253254
} else {
254255
url := p.cfg.URL.Scheme + "://" + p.cfg.URL.Host + p.cfg.URL.Path + webhookEvent.Body.Path
255-
tr := &http.Transport{
256-
TLSClientConfig: &tls.Config{InsecureSkipVerify: p.cfg.Insecure},
257-
}
258256

257+
// Create request with context for timeout control
259258
timeout := webhookEvent.Body.Request.Timeout
260259
if timeout == 0 {
261260
timeout = 1000 * 30
262261
}
263262

264-
client := &http.Client{
265-
Timeout: time.Duration(timeout) * time.Millisecond,
266-
Transport: tr,
267-
}
263+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Millisecond)
264+
defer cancel()
268265

269-
req, err := http.NewRequest(webhookEvent.Body.Request.Method, url, nil)
266+
req, err := http.NewRequestWithContext(ctx, webhookEvent.Body.Request.Method, url, nil)
270267
if err != nil {
271268
fmt.Printf("Error: %s\n", err)
272269
return
@@ -286,8 +283,7 @@ func (p *Proxy) processAttempt(msg websocket.IncomingMessage) {
286283
req.Body = ioutil.NopCloser(strings.NewReader(webhookEvent.Body.Request.DataString))
287284
req.ContentLength = int64(len(webhookEvent.Body.Request.DataString))
288285

289-
res, err := client.Do(req)
290-
286+
res, err := p.httpClient.Do(req)
291287
if err != nil {
292288
color := ansi.Color(os.Stdout)
293289
localTime := time.Now().Format(timeLayout)
@@ -309,6 +305,7 @@ func (p *Proxy) processAttempt(msg websocket.IncomingMessage) {
309305
},
310306
}})
311307
} else {
308+
defer res.Body.Close()
312309
p.processEndpointResponse(webhookEvent, res)
313310
}
314311
}
@@ -366,10 +363,25 @@ func New(cfg *Config, connections []*hookdecksdk.Connection) *Proxy {
366363
cfg.Log = &log.Logger{Out: ioutil.Discard}
367364
}
368365

366+
// Create a shared HTTP transport with connection pooling
367+
tr := &http.Transport{
368+
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.Insecure},
369+
// Connection pool settings for better performance
370+
MaxIdleConns: 100,
371+
MaxIdleConnsPerHost: 10,
372+
IdleConnTimeout: 90 * time.Second,
373+
DisableKeepAlives: false,
374+
}
375+
369376
p := &Proxy{
370377
cfg: cfg,
371378
connections: connections,
372379
connectionTimer: time.NewTimer(0), // Defaults to no delay
380+
httpClient: &http.Client{
381+
Transport: tr,
382+
// Default timeout can be overridden per request
383+
Timeout: 30 * time.Second,
384+
},
373385
}
374386

375387
return p

0 commit comments

Comments
 (0)