Commit 0b8ee21
committed
pipe: add server backlog for concurrent Accept()
Teach `pipe.go:ListenPipe()` to create multiple instances of
the server pipe in the kernel so that client connections are
less likely to receive a `windows.ERROR_PIPE_BUSY` error.
This is conceptually similar to the `backlog` argument of the
Unix `listen(2)` function.
The current `listenerRoutine()` function works sequentially and
in response to calls to `Accept()`, such that there will only
be at most one unbound server pipe present at any time. Even
if the server application calls `Accept()` concurrrently from
a pool of application threads, `listenerRoutine()` will process
them sequentially.
In this model and because there is only one `listenerRoutine()`
instance, there is an interval of time where there are no
available unbound/free server pipes. When `ConnectNamedPipe()`
returns `listenerRoutine()` sends the new pipe handle over a
channel to the caller of `Accept()`. Application code then has
an opportunity to dispatch/process it and then call `Accept()`
again. This causes `listenerRoutine()` to create a new unbound
serer pipe and wait for the next connection. Anytime during
this interval, a client will get a pipe busy error.
Code in `DialPipe()` hides this from GOLANG callers because it
includes a busy retry loop. However, clients written in other
languages without this assistance are likely to see it and deal
with it.
This change introduces an "accept queue" using a buffered channel
and splits `listenerRoutine()` into a pool of listener worker
threads. Each worker creates a new unbound pipe and waits for
a client connection. The NPFS and kernel handle connectioni
delivery to a random listener worker. The resulting connected
pipe is delivered back to the caller `Accept()` as before.
A `PipeConfig.QueueSize` variable controls the number of listener
worker threads and the maximum number of unbound/free pipes server
pipes that will be present at any given time. Note that a
listener worker will normally have an unbound/free pipe except
during that same delivery interval. Having multiple active
workers gives us extra capacity to handle rapidly arriving
connections.
The application is encouraged to call `Accept()` from a pool
of application workers. The size of the application pool should
be the same or larger than the queue size to take full advantage
of the listener queue.
To preserve backwards compatibility, a queue size of 0 or 1
will behave as before.
Also for backwards compatibility, listener workers are required
to wait for an `Accept()` call so that the worker has a return
channel to send the connected pipe and error code. This implies
that the number of unbound pipes will be the smaller of the
queue size and the application pool size.
Finally, a Mutex was added to `l.Close()` to ensure that
concurrent threads do not simultaneously try to shutdown the
pipe.
Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>1 parent 4f41be6 commit 0b8ee21
2 files changed
+370
-41
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
258 | 259 | | |
259 | 260 | | |
260 | 261 | | |
261 | | - | |
262 | | - | |
263 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
264 | 286 | | |
265 | 287 | | |
266 | 288 | | |
| |||
383 | 405 | | |
384 | 406 | | |
385 | 407 | | |
386 | | - | |
| 408 | + | |
387 | 409 | | |
388 | 410 | | |
389 | 411 | | |
| |||
395 | 417 | | |
396 | 418 | | |
397 | 419 | | |
398 | | - | |
399 | | - | |
400 | | - | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
401 | 423 | | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
417 | 428 | | |
418 | | - | |
419 | 429 | | |
420 | 430 | | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
421 | 454 | | |
422 | 455 | | |
423 | 456 | | |
424 | | - | |
| 457 | + | |
425 | 458 | | |
426 | 459 | | |
427 | 460 | | |
| |||
442 | 475 | | |
443 | 476 | | |
444 | 477 | | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
445 | 491 | | |
446 | 492 | | |
447 | 493 | | |
| |||
460 | 506 | | |
461 | 507 | | |
462 | 508 | | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
463 | 519 | | |
464 | 520 | | |
465 | 521 | | |
466 | 522 | | |
467 | 523 | | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
476 | 533 | | |
477 | 534 | | |
478 | 535 | | |
| |||
492 | 549 | | |
493 | 550 | | |
494 | 551 | | |
| 552 | + | |
495 | 553 | | |
| 554 | + | |
496 | 555 | | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
502 | 590 | | |
503 | 591 | | |
504 | 592 | | |
505 | 593 | | |
506 | 594 | | |
507 | 595 | | |
508 | 596 | | |
509 | | - | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
510 | 600 | | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
511 | 607 | | |
512 | 608 | | |
513 | 609 | | |
514 | 610 | | |
| 611 | + | |
515 | 612 | | |
516 | | - | |
517 | | - | |
518 | | - | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
519 | 627 | | |
| 628 | + | |
| 629 | + | |
520 | 630 | | |
521 | 631 | | |
522 | 632 | | |
| |||
0 commit comments