Skip to content

refactor(proxy): use context cancellation for tunnel tracking#65

Open
LaurenceJJones wants to merge 5 commits intodevfrom
proxy-context-tunnel-tracking
Open

refactor(proxy): use context cancellation for tunnel tracking#65
LaurenceJJones wants to merge 5 commits intodevfrom
proxy-context-tunnel-tracking

Conversation

@LaurenceJJones
Copy link
Copy Markdown
Member

Community Contribution License Agreement

By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.

Description

  • Replace []net.Conn slice with context + atomic counter in activeTunnel
  • Use errgroup.WithContext for pipe() to handle goroutine lifecycle
  • Use context.AfterFunc to close connections on cancellation
  • Fix race condition by comparing tunnel pointers instead of map lookup
  • UpdateLocalSNIs now cancels tunnel context instead of iterating conns

This eliminates O(n) connection removal, prevents goroutine leaks, and provides cleaner cancellation semantics.

How to test?

Internal proxy usage.

- Replace []net.Conn slice with context + atomic counter in activeTunnel
- Use errgroup.WithContext for pipe() to handle goroutine lifecycle
- Use context.AfterFunc to close connections on cancellation
- Fix race condition by comparing tunnel pointers instead of map lookup
- UpdateLocalSNIs now cancels tunnel context instead of iterating conns

This eliminates O(n) connection removal, prevents goroutine leaks,
and provides cleaner cancellation semantics.
Remove atomic counter in favor of simple int protected by mutex.
Eliminates race condition complexity and recheck logic.
Add pointer check before delete to handle race where UpdateLocalSNIs
removes our tunnel and a new one is created for the same hostname.
@LaurenceJJones LaurenceJJones force-pushed the proxy-context-tunnel-tracking branch from 317c304 to 390b401 Compare April 9, 2026 14:03
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.

1 participant