Commit c2b7a54
authored
Optimize DialStream with route caching, latency sorting, and entry ca… (skycoin#361)
* Optimize DialStream with route caching, latency sorting, and entry caching
- 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
* Fix dmsgweb proxy: propagate request context and fix error handling
- 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
* Refactor HTTPTransport to use http.Transport with dmsg DialContext
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
* Fix TCP proxy race, gin server leak, ReverseProxy Director, and HTTP 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
* Harden dmsgweb: connection limits, body limits, close error logging
- 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 CI lint errors: gosec, misspell, and unhandled errors
- 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
* Revert HTTPTransport to direct stream approach for CI compatibility
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.
* Remove unnecessary nolint:govet directive1 parent 8206a02 commit c2b7a54
7 files changed
Lines changed: 333 additions & 79 deletions
File tree
- cmd/dmsgweb/commands
- pkg
- dmsghttp
- dmsg
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
292 | 292 | | |
293 | 293 | | |
294 | | - | |
| 294 | + | |
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
301 | | - | |
| 301 | + | |
302 | 302 | | |
303 | 303 | | |
304 | | - | |
| 304 | + | |
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
312 | | - | |
| 312 | + | |
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
| |||
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
340 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
341 | 343 | | |
342 | 344 | | |
343 | | - | |
| 345 | + | |
344 | 346 | | |
345 | 347 | | |
346 | 348 | | |
347 | 349 | | |
348 | | - | |
349 | 350 | | |
350 | 351 | | |
351 | | - | |
352 | | - | |
353 | | - | |
| 352 | + | |
354 | 353 | | |
355 | | - | |
| 354 | + | |
356 | 355 | | |
357 | 356 | | |
358 | | - | |
| 357 | + | |
359 | 358 | | |
360 | 359 | | |
361 | 360 | | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
369 | 365 | | |
370 | | - | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
371 | 374 | | |
372 | 375 | | |
373 | 376 | | |
374 | 377 | | |
375 | | - | |
| 378 | + | |
376 | 379 | | |
377 | 380 | | |
378 | 381 | | |
379 | 382 | | |
380 | 383 | | |
381 | 384 | | |
382 | 385 | | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
383 | 390 | | |
384 | 391 | | |
385 | 392 | | |
| |||
401 | 408 | | |
402 | 409 | | |
403 | 410 | | |
404 | | - | |
| 411 | + | |
405 | 412 | | |
406 | 413 | | |
407 | 414 | | |
| |||
430 | 437 | | |
431 | 438 | | |
432 | 439 | | |
433 | | - | |
| 440 | + | |
| 441 | + | |
434 | 442 | | |
435 | 443 | | |
436 | 444 | | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
437 | 459 | | |
438 | 460 | | |
439 | 461 | | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | 462 | | |
447 | | - | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
448 | 466 | | |
449 | 467 | | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
450 | 478 | | |
451 | 479 | | |
452 | 480 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
180 | 186 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | 187 | | |
187 | 188 | | |
188 | 189 | | |
| |||
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
214 | 218 | | |
215 | 219 | | |
216 | 220 | | |
217 | 221 | | |
218 | 222 | | |
219 | 223 | | |
| 224 | + | |
220 | 225 | | |
221 | 226 | | |
222 | 227 | | |
| |||
241 | 246 | | |
242 | 247 | | |
243 | 248 | | |
244 | | - | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
245 | 252 | | |
246 | 253 | | |
247 | 254 | | |
248 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
249 | 258 | | |
250 | 259 | | |
251 | 260 | | |
| |||
257 | 266 | | |
258 | 267 | | |
259 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
260 | 280 | | |
261 | 281 | | |
262 | 282 | | |
263 | 283 | | |
264 | 284 | | |
265 | 285 | | |
| 286 | + | |
266 | 287 | | |
267 | | - | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
268 | 293 | | |
269 | 294 | | |
270 | 295 | | |
| |||
276 | 301 | | |
277 | 302 | | |
278 | 303 | | |
279 | | - | |
280 | 304 | | |
| 305 | + | |
281 | 306 | | |
| 307 | + | |
282 | 308 | | |
283 | 309 | | |
284 | | - | |
| 310 | + | |
285 | 311 | | |
286 | 312 | | |
287 | 313 | | |
288 | 314 | | |
289 | | - | |
| 315 | + | |
290 | 316 | | |
291 | 317 | | |
292 | | - | |
293 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
294 | 325 | | |
295 | 326 | | |
296 | 327 | | |
| |||
0 commit comments