Skip to content

feat(http): Add connection timeout - #339

Merged
moshloop merged 1 commit into
masterfrom
feat/http-connection-timeout
Aug 10, 2026
Merged

feat(http): Add connection timeout#339
moshloop merged 1 commit into
masterfrom
feat/http-connection-timeout

Conversation

@moshloop

@moshloop moshloop commented Aug 10, 2026

Copy link
Copy Markdown
Member

What

  • Add a configurable connection-establishment timeout for HTTP requests.

Notes

  • Preserve transport settings and honor earlier request deadlines.

Summary by CodeRabbit

  • New Features

    • Added configurable connection timeouts for HTTP clients.
    • Rejects invalid timeout values and preserves existing client settings.
    • Respects request deadlines that occur before the configured connection timeout.
  • Bug Fixes

    • Prevents connection attempts from hanging beyond the configured timeout.

Bound connection establishment separately from the overall request timeout while preserving transport settings and honoring earlier request deadlines.
@moshloop
moshloop enabled auto-merge (rebase) August 10, 2026 10:08
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 02a70f19-7702-45c5-a7e5-698ca03c927c

📥 Commits

Reviewing files that changed from the base of the PR and between a09cb96 and 9c84c85.

📒 Files selected for processing (2)
  • http/connect.go
  • http/connect_test.go
 ___________________________
< Loving your code to bits. >
 ---------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/http-connection-timeout
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch feat/http-connection-timeout

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

Gavel summary

Source Pass Fail Skip Duration
collections 60 0 0 3.1s
files 41 0 0 73ms
github.com/flanksource/commons/certs 4 0 0 400ms
github.com/flanksource/commons/cmd/hx 8 0 0 -
github.com/flanksource/commons/cmd/hx/parse 26 0 0 -
github.com/flanksource/commons/collections/syncmap 10 0 0 -
github.com/flanksource/commons/context 1 0 0 -
github.com/flanksource/commons/duration 2 0 0 -
github.com/flanksource/commons/files 16 0 0 -
github.com/flanksource/commons/har 52 0 0 10ms
github.com/flanksource/commons/hash 13 0 0 -
github.com/flanksource/commons/http 95 0 2 11.6s
github.com/flanksource/commons/logger 49 0 0 10ms
github.com/flanksource/commons/logger/httpretty/internal/color 15 0 0 -
github.com/flanksource/commons/logger/httpretty/internal/header 1 0 0 -
github.com/flanksource/commons/lookup 7 0 0 10ms
github.com/flanksource/commons/merge 28 0 0 -
github.com/flanksource/commons/test 5 0 1 30ms
github.com/flanksource/commons/text 1 0 0 -
github.com/flanksource/commons/tokenizer 3 0 0 -
help 11 0 0 10ms
http 3 0 0 52ms
logger 41 0 0 1ms
set 7 0 0 460.299µs

Totals: 499 passed · 0 failed · 3 skipped · 15.3s

View full results

@moshloop
moshloop merged commit 03b28f6 into master Aug 10, 2026
10 of 11 checks passed
@moshloop
moshloop deleted the feat/http-connection-timeout branch August 10, 2026 10:11
@flankbot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.56.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@http/connect_test.go`:
- Around line 55-56: Reduce the upper-bound duration assertions around the
connection timing checks in the affected tests to a practical limit relative to
the configured 25-millisecond timeout, such as 250 milliseconds, while
preserving the existing lower-bound assertions and applying the same change to
both assertion pairs.

In `@http/connect.go`:
- Around line 27-35: Update ConnectTimeout’s transport-cloning logic to wrap
configured.DialTLSContext with the same timeout context used by DialContext,
preserving its existing fallback behavior as appropriate. Ensure HTTPS dialing
through a blocking custom DialTLSContext respects ConnectTimeout, and add a test
covering that timeout path.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 02a70f19-7702-45c5-a7e5-698ca03c927c

📥 Commits

Reviewing files that changed from the base of the PR and between a09cb96 and 9c84c85.

📒 Files selected for processing (2)
  • http/connect.go
  • http/connect_test.go

Comment thread http/connect_test.go
Comment on lines +55 to +56
Expect(time.Since(started)).To(BeNumerically(">=", 25*time.Millisecond))
Expect(time.Since(started)).To(BeNumerically("<", time.Second))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Tighten the timeout upper bounds.

Both assertions only require completion before one second. An implementation that delays dialing far beyond the configured 25 milliseconds can pass these tests.

Use a practical upper bound relative to the 25-millisecond timeout, such as 250 milliseconds.

Proposed test change
- Expect(time.Since(started)).To(BeNumerically("<", time.Second))
+ Expect(time.Since(started)).To(BeNumerically("<", 250*time.Millisecond))

Also applies to: 75-76

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@http/connect_test.go` around lines 55 - 56, Reduce the upper-bound duration
assertions around the connection timing checks in the affected tests to a
practical limit relative to the configured 25-millisecond timeout, such as 250
milliseconds, while preserving the existing lower-bound assertions and applying
the same change to both assertion pairs.

Comment thread http/connect.go
Comment on lines +27 to +35
dialContext := configured.DialContext
if dialContext == nil {
dialContext = (&net.Dialer{}).DialContext
}
configured.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
connectCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return dialContext(connectCtx, network, address)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find existing custom TLS dialers that require timeout coverage.
rg -n -C 4 --type go '\bDialTLSContext\s*:|\bDialTLS\s*:' .

Repository: flanksource/commons

Length of output: 157


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate connect.go =="
fd -a 'connect.go$' . | sed 's#^\./##'

echo "== http/connect.go snippet =="
if [ -f http/connect.go ]; then
  nl -ba http/connect.go | sed -n '1,120p'
fi

echo "== search DialTLSContext usages =="
rg -n --type go 'DialTLSContext|DialTLS|DialContext|ConnectTimeout' . || true

echo "== git diff stat =="
git diff --stat || true

Repository: flanksource/commons

Length of output: 286


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate connect.go =="
fd -a 'connect.go$' . | sed 's#^\./##'

echo "== http/connect.go snippet =="
if [ -f http/connect.go ]; then
  cat -n http/connect.go | sed -n '1,140p'
fi

echo "== search DialTLSContext usages =="
rg -n --type go 'DialTLSContext|DialTLS|DialContext|ConnectTimeout' . || true

echo "== lint availability =='
[ -f Makefile ] && rg -n 'lint|golangci' Makefile || true

Repository: flanksource/commons

Length of output: 2605


🌐 Web query:

Go net/http.Transport DialTLSContext used for HTTPS connections when non-nil documentation

💡 Result:

When net/http.Transport.DialTLSContext is set (non-nil), it is used to create TLS connections for non-proxied HTTPS requests [1][2]. Its behavior and interaction with other Transport settings are as follows: 1. Priority: If DialTLSContext is set, it takes precedence over the deprecated DialTLS hook [1][2]. 2. Bypassing Defaults: When DialTLSContext is provided, the Transport will not use the standard Dial or DialContext hooks for HTTPS requests [1][2]. Additionally, the TLSClientConfig and TLSHandshakeTimeout fields are ignored by the Transport, as the returned net.Conn is assumed to have already completed the TLS handshake [1][2]. 3. Expectation of State: The net.Conn returned by DialTLSContext is expected to be fully established and past the TLS handshake stage [1][2]. For proper ALPN (Application-Layer Protocol Negotiation) support, the returned connection should be a *tls.Conn or otherwise implement the ConnectionState method consistent with *tls.Conn [2]. 4. Fallback: If DialTLSContext is nil, the Transport checks for the deprecated DialTLS [1][2]. If both are nil, the Transport defaults to using DialContext (or Dial) combined with the TLSClientConfig to manage the connection and perform the handshake [1][2]. By using DialTLSContext, developers gain the ability to use the provided context.Context to cancel the dial operation when it is no longer needed, as well as the flexibility to perform custom TLS configuration or validation on a per-request basis [2][3].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== http/connect_test.go =="
cat -n http/connect_test.go | sed -n '1,120p'

echo "== transport dial hook fields =="
go doc net/http.Transport DialTLSContext 2>/tmp/go_doc_err.log || cat /tmp/go_doc_err.log

Repository: flanksource/commons

Length of output: 3395


Apply the timeout to DialTLSContext.

ConnectTimeout copies an *http.Transport but only wraps DialContext. When Transport.DialTLSContext is set, HTTPS connections use it instead of DialContext, so custom TLS dialing can bypass ConnectTimeout. Wrap the cloned DialTLSContext with the same timeout context and add an HTTPS test using a blocking DialTLSContext.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@http/connect.go` around lines 27 - 35, Update ConnectTimeout’s
transport-cloning logic to wrap configured.DialTLSContext with the same timeout
context used by DialContext, preserving its existing fallback behavior as
appropriate. Ensure HTTPS dialing through a blocking custom DialTLSContext
respects ConnectTimeout, and add a test covering that timeout path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants