Skip to content

feat(gateway): Fallback to IPv4 when IPv6 is unavailable - #485

Closed
hossain-rayhan wants to merge 1 commit into
documentdb:mainfrom
hossain-rayhan:rayhan/gw-fallback-to-ipv4
Closed

hossain-rayhan wants to merge 1 commit into
documentdb:mainfrom
hossain-rayhan:rayhan/gw-fallback-to-ipv4

Conversation

@hossain-rayhan

Copy link
Copy Markdown

Summary

On systems without IPv6 support (e.g., AKS Edge), the gateway fails to
start because it unconditionally binds to [::]. This change adds
automatic fallback to IPv4 (0.0.0.0) when IPv6 binding fails.

Changes

  • Extract TCP listener binding logic into a standalone create_tcp_listener()
    function for testability.
  • Attempt IPv6 dual-stack ([::]) first; if it fails, log a warning and
    fall back to IPv4 (0.0.0.0).
  • Localhost mode (127.0.0.1) is unchanged.
  • Add unit tests covering:
    • Localhost binds to IPv4 loopback
    • Non-localhost binds to an unspecified address
    • Port assignment (OS-assigned and specific)
    • IPv4 fallback when IPv6 port is occupied
    • Localhost does not bind to all interfaces

Testing

All new unit tests pass: cargo test --lib -p documentdb_gateway_core -- tests::

Extract TCP listener binding into create_tcp_listener() and attempt
IPv6 dual-stack ([::])) first, falling back to IPv4 (0.0.0.0) when
IPv6 is not supported. This enables the gateway to run in environments
without IPv6 support such as AKS Edge. Add unit tests covering all
binding scenarios.

Signed-off-by: Rayhan Hossain <[email protected]>
@hossain-rayhan
hossain-rayhan requested a review from a team as a code owner March 5, 2026 20:20
guanzhousongmicrosoft pushed a commit to guanzhousongmicrosoft/documentdb that referenced this pull request Mar 23, 2026
…s for environments without IPv6

### Does this PR have any customer impact?
Yes, unblock customers to run gateway on environment where with no ipv6 support like AKS Edge.

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Bugfix.

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

## Summary
Implements dual IPv4/IPv6 TCP listener support for the OSS Rust gateway, enabling operation in environments where IPv6 is unavailable (e.g., AKS Edge).

## Changes
- Added `create_tcp_listeners()` function that binds to both IPv4 (`0.0.0.0`) and IPv6 (`[::]`) addresses
- Uses `IPV6_V6ONLY` socket option via `socket2` crate for consistent cross-platform behavior
  - Linux defaults to dual-stack (IPv6 socket accepts IPv4), Windows/macOS default to IPv6-only
  - Explicitly setting `IPV6_V6ONLY=true` ensures we need both listeners on all platforms
- If one address family fails to bind, logs a warning and continues with the other
- Returns an error only if both bindings fail
- Refactored connection accept loop with `spawn_tcp_handler`/`spawn_unix_handler` helpers

## Behavior
| Scenario | Result |
|----------|--------|
| Both IPv4 and IPv6 available | Both listeners active |
| Only IPv4 available (no IPv6) | IPv4 listener active, warning logged |
| Only IPv6 available | IPv6 listener active, warning logged |
| Neither available | Error returned |

## Testing
- Added 7 unit tests covering localhost, non-localhost, port assignment, partial failure, and total failure scenarios
- All 19 existing tests pass

## Related
- Follows pattern from documentdb#485
- Matches C# gateway dual-listener approach (`IPAddress.Any` + `IPAddress.IPv6Any`)

Signed-off-by: Guanzhou Song <[email protected]>
guanzhousongmicrosoft pushed a commit to guanzhousongmicrosoft/documentdb that referenced this pull request Mar 23, 2026
…s for environments without IPv6

### Does this PR have any customer impact?
Yes, unblock customers to run gateway on environment where with no ipv6 support like AKS Edge.

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Bugfix.

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

## Summary
Implements dual IPv4/IPv6 TCP listener support for the OSS Rust gateway, enabling operation in environments where IPv6 is unavailable (e.g., AKS Edge).

## Changes
- Added `create_tcp_listeners()` function that binds to both IPv4 (`0.0.0.0`) and IPv6 (`[::]`) addresses
- Uses `IPV6_V6ONLY` socket option via `socket2` crate for consistent cross-platform behavior
  - Linux defaults to dual-stack (IPv6 socket accepts IPv4), Windows/macOS default to IPv6-only
  - Explicitly setting `IPV6_V6ONLY=true` ensures we need both listeners on all platforms
- If one address family fails to bind, logs a warning and continues with the other
- Returns an error only if both bindings fail
- Refactored connection accept loop with `spawn_tcp_handler`/`spawn_unix_handler` helpers

## Behavior
| Scenario | Result |
|----------|--------|
| Both IPv4 and IPv6 available | Both listeners active |
| Only IPv4 available (no IPv6) | IPv4 listener active, warning logged |
| Only IPv6 available | IPv6 listener active, warning logged |
| Neither available | Error returned |

## Testing
- Added 7 unit tests covering localhost, non-localhost, port assignment, partial failure, and total failure scenarios
- All 19 existing tests pass

## Related
- Follows pattern from documentdb#485
- Matches C# gateway dual-listener approach (`IPAddress.Any` + `IPAddress.IPv6Any`)

Signed-off-by: Guanzhou Song <[email protected]>
guanzhousongmicrosoft pushed a commit to guanzhousongmicrosoft/documentdb that referenced this pull request Mar 24, 2026
…s for environments without IPv6

### Does this PR have any customer impact?
Yes, unblock customers to run gateway on environment where with no ipv6 support like AKS Edge.

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Bugfix.

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

## Summary
Implements dual IPv4/IPv6 TCP listener support for the OSS Rust gateway, enabling operation in environments where IPv6 is unavailable (e.g., AKS Edge).

## Changes
- Added `create_tcp_listeners()` function that binds to both IPv4 (`0.0.0.0`) and IPv6 (`[::]`) addresses
- Uses `IPV6_V6ONLY` socket option via `socket2` crate for consistent cross-platform behavior
  - Linux defaults to dual-stack (IPv6 socket accepts IPv4), Windows/macOS default to IPv6-only
  - Explicitly setting `IPV6_V6ONLY=true` ensures we need both listeners on all platforms
- If one address family fails to bind, logs a warning and continues with the other
- Returns an error only if both bindings fail
- Refactored connection accept loop with `spawn_tcp_handler`/`spawn_unix_handler` helpers

## Behavior
| Scenario | Result |
|----------|--------|
| Both IPv4 and IPv6 available | Both listeners active |
| Only IPv4 available (no IPv6) | IPv4 listener active, warning logged |
| Only IPv6 available | IPv6 listener active, warning logged |
| Neither available | Error returned |

## Testing
- Added 7 unit tests covering localhost, non-localhost, port assignment, partial failure, and total failure scenarios
- All 19 existing tests pass

## Related
- Follows pattern from documentdb#485
- Matches C# gateway dual-listener approach (`IPAddress.Any` + `IPAddress.IPv6Any`)

Signed-off-by: Guanzhou Song <[email protected]>
guanzhousongmicrosoft pushed a commit that referenced this pull request Mar 26, 2026
…s for environments without IPv6

### Does this PR have any customer impact?
Yes, unblock customers to run gateway on environment where with no ipv6 support like AKS Edge.

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Bugfix.

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

## Summary
Implements dual IPv4/IPv6 TCP listener support for the OSS Rust gateway, enabling operation in environments where IPv6 is unavailable (e.g., AKS Edge).

## Changes
- Added `create_tcp_listeners()` function that binds to both IPv4 (`0.0.0.0`) and IPv6 (`[::]`) addresses
- Uses `IPV6_V6ONLY` socket option via `socket2` crate for consistent cross-platform behavior
  - Linux defaults to dual-stack (IPv6 socket accepts IPv4), Windows/macOS default to IPv6-only
  - Explicitly setting `IPV6_V6ONLY=true` ensures we need both listeners on all platforms
- If one address family fails to bind, logs a warning and continues with the other
- Returns an error only if both bindings fail
- Refactored connection accept loop with `spawn_tcp_handler`/`spawn_unix_handler` helpers

## Behavior
| Scenario | Result |
|----------|--------|
| Both IPv4 and IPv6 available | Both listeners active |
| Only IPv4 available (no IPv6) | IPv4 listener active, warning logged |
| Only IPv6 available | IPv6 listener active, warning logged |
| Neither available | Error returned |

## Testing
- Added 7 unit tests covering localhost, non-localhost, port assignment, partial failure, and total failure scenarios
- All 19 existing tests pass

## Related
- Follows pattern from #485
- Matches C# gateway dual-listener approach (`IPAddress.Any` + `IPAddress.IPv6Any`)

Signed-off-by: Guanzhou Song <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants