Optimize DialStream with route caching, latency sorting, and entry ca…#361
Merged
0pcom merged 8 commits intoskycoin:developfrom Mar 31, 2026
Merged
Conversation
…ching - Add route cache: remember which server successfully reached a destination, try it first on subsequent dials, evict on failure - Sort sessions by measured ping latency so lowest-latency server is tried first instead of random map iteration order - Cache discovery entry lookups with 30s TTL to avoid re-querying HTTP discovery on every request - Background ping loop measures all session RTTs every 30s
- Use http.NewRequestWithContext to propagate browser request context to dmsg dial, so cancellations stop the stream dial immediately instead of waiting for the full 20s HandshakeTimeout - Remove impossible c.String(500) after c.Status() was already written, which caused "Headers were already written" warnings in gin
Replace manual stream-per-request dial/write/read pattern with Go's http.Transport using a custom DialContext. Keep-alives are disabled because dmsg streams use noise-encrypted per-stream handshakes that make connection reuse unreliable (server ReadTimeout can expire between requests, and POST requests cannot be retried on stale connections). Benefits: - Proper request context propagation through the transport - Standard error handling and timeout support - Removes manual wrappedBody response draining hack - Normalizes dmsg:// URLs to http:// for Go's transport - Cleans up idle connections on context cancellation
…timeouts - Fix TCP proxy io.Copy race: close both connections after first copy returns to unblock the second, preventing goroutine leak - Replace dlog.Fatal with error return on port overflow (was killing process) - Replace gin r.Run() with http.Server and graceful Shutdown on context cancel, preventing goroutine leak on shutdown - Pass context to proxyTCPConn/proxyHTTPConn for proper cancellation - Fix silent ReverseProxy Director failure: parse URL before creating proxy, return 500 on parse error instead of forwarding to wrong URL - Add 30s timeout to HTTP clients in dmsghttp/util.go to prevent hanging
- Add connection semaphore (max 256) to server-side TCP proxy to prevent unbounded goroutine growth from many simultaneous connections - Fix server-side TCP proxy io.Copy race: close both connections after first copy returns, wait for goroutine with done channel - Add 10MB request body limit via http.MaxBytesReader in HTTP proxy - Log close errors at debug level instead of silently ignoring them
- Fix G104 (gosec): handle Close() errors with debug logging instead of ignoring them in TCP proxy - Fix G112 (gosec): add ReadHeaderTimeout to HTTP server to prevent Slowloris attacks - Fix G118 (gosec): use parent context for DialStream instead of context.Background(); add nolint for intentional Background in graceful shutdown - Fix misspell: cancelled -> canceled in comment
The http.Transport wrapper with DisableKeepAlives caused timeouts on Windows CI and hangs on Linux CI due to Go's transport adding overhead (Connection: close headers, persistConn goroutines) that interacts poorly with noise-encrypted streams under concurrent load. Revert to the proven direct approach: dial stream, write request, read response, wrap body to close stream. Keep the dmsg:// URL normalization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…ching