Article View: comp.lang.c
Article #1624Re: Inversion-of-control and dependency-injection in C
From: Ross Finlayson
Date: Wed, 18 Nov 2026 10:30
Date: Wed, 18 Nov 2026 10:30
186 lines
10630 bytes
10630 bytes
On 11/17/2026 03:46 PM, Chris M. Thomasson wrote: > On 11/17/2026 1:00 AM, Ross Finlayson wrote: >> On 11/16/2026 01:10 PM, Chris M. Thomasson wrote: >>> On 11/16/2026 12:04 PM, Ross Finlayson wrote: >>>> On 11/16/2026 10:09 AM, Bonita Montero wrote: >>>>> Am 13.11.2026 um 23:50 schrieb Ross Finlayson: >>>>> >>>>>> So, here's one. Say you got a server process for sockets of the >>>>>> connection-oriented or datagram-wise or association-oriented like >>>>>> TCP, UDP, or SCTP, and it manages the connecting and the sending and >>>>>> receiving about a million of those and is sort of fixed-size and >>>>>> stable in a usual runtime. So, how do you design the interface, >>>>>> about stuff like the state-machine of _protocols_, all about >>>>>> _protocols_, in the application-layer and above, say, for example >>>>>> the incremental-parsing and the state-machine of the protocol >>>>>> and sub-protocols and upgrade-protocols and the like, what sort >>>>>> of abstraction with suitable-defaults and reasonable-opinions >>>>>> makes for having any sort protocol as a composable library. >>>>> Use C++20 coroutines with Bost.ASIO and io_uring. This gives >>>>> as less code as possible with nearly optimal I/O performance. >>>> >>>> Thanks for writing Bonita. I'd infer from "schrieb" that >>>> your user-agent locale country/language is DE/de or as >>>> variously for example about 2-letter and 3-letter country >>>> codes and accounts of language or ISO 601 or what I forget. >>>> Mann kann nur kleine Deutsch gesprechern, nicht viel das >>>> hier doch, aber los. Mein Wortschatz, es ist kaput. >>>> >>>> >>>> That said, _why_ I specifically am not using epoll/kqueue >>>> or io_uring instead pselect not select or poll, is: >>>> if you overload the epoll red/black tree or io_iuring queues >>>> respectively, they drop messages. Now, most of the time, >>>> that's accommodated by some absurdly simple back-end like >>>> "send" that tolerates drops itself, in things like message-bus. >>>> Then, epoll for example, on a wake-up or return from the blocking >>>> syscall, doesn't notify twice when there remains data to be read. >>>> Then, pselect() may be blocking and furthermore with the impact >>>> that the kernel polls the sockets busily, yet, pselect() can >>>> timeout itself, for example, and register interest on active >>>> selectors and only periodically eg 5ms register interest on >>>> active + inactive selectors (file-descriptors). >>>> >>>> Then, there's a fact that POSIX runtimes have signals, >>>> then, that pselect() behaves in a more defined manner. >>>> >>>> Another key distinction is that: buffers for use with >>>> epoll and I believe also kqueue, or io_uring, must >>>> be "pinned" with mlock() or what, to physical memory, >>>> for the kernel to treat them, instead of copying from >>>> user-space to kernel-space or vice-versa or however >>>> the runtime may care to swap pages under addresses >>>> with making copies, that for accounts of vector I/O, >>>> scatter/gather yet mostly vector I/O for the bus boss >>>> the DMA, it's just plain old heap RAM. >>>> >>>> >>>> Then, also pselect() is POSIX and so it's portable. >>>> It's less code if it doesn't need multiple back-ends >>>> for various platforms' implementations of I/O multiplexing. >>>> >>>> >>> >>> Huh? Do you have a clue at all about this? From what you wrote, perhaps >>> not. Sigh. >>> >> >> >> About epoll and dropping reads, it's not the same as io_uring >> which has fixed-sized queues and if it gets full drops those >> in the kernel, it's about the edge-triggered case or "e"-poll >> for edge-triggered poll, vis-a-vis the usual account of poll >> or select or pselect as level-triggered, that there's perceived >> to be an edge case where epoll would lose notice of a notification. >> The idea is that while blocking in epoll_wait, that the process >> gets a signal, then, the kernel interrupts and runs the signal-handler >> and returning from epoll_wait it gets EINTR, _or_, according to >> a policy of whether to re-try blocking sys-calls the kernel interrupts >> and runs the signal-handler and the process enters epoll_wait again. >> >> So, I'm under the impression that there's a race-condition where >> the process gets a signal while blocked in epoll_wait, then according >> to one of two policies, EAGAIN error-handling or re-try, that it >> loses the notification, not the packet. The packet is still on >> the socket to be read, the notification of it was lost. >> >> The pselect simply doesn't have that problem, it's level-triggered, >> and however it re-enters pselect after getting interrupted, >> as long as there's data yet to read, it will get notified. >> >> >> Then also epoll might not need mlock'ed memory like io_uring does. >> >> >> >> That's about it. >> >> >> >> > > pselect() is simple, portable, and for many workloads fast enough. Those > are perfectly valid engineering arguments. > > For a high-performance server, though, well, shit. I personally wouldn't > be reaching for pselect() at all. Uggg... > > One thing I like about mechanisms such as io_uring and IOCP is that they > encourage thinking about capacity up front. You tend to preallocate the > resources the system needs to operate, size your queues and pools > appropriately, and define behavior for resource pressure. Thats > generally preferable to discovering in the god damn hot path that yet > another allocation is required! Fuck! > > On Windows, large scale IOCP servers have long used techniques such as > so called, zero byte receives, and preallocated connection state to > reduce memory pressure and avoid per-operation allocations. Similar > ideas apply elsewhere, for sure. The goal is usually to enter a pressure > mode before critical resources are exhausted, not to keep allocating > indefinitely and hope for the best. > > When resources start getting tight, I would rather see the server shed > load, evict caches, shorten timeouts, disconnect stale clients, or > refuse new work than continue consuming memory until the operating > system is in trouble. Running out of capacity should be a planned state, > not a surprise. > > That's one of the reasons fixed pools and preallocated state appeal to > me for systems under heavy load. > Preallocated state is another matter altogether, this sort of approach mentioned presumes fixed resources, for example when a memory-pool is in-use, to stop reads and then TCP/IP itself closes the receive window. The "pressure" closes the receive-side, and clients wait (the network buffers). These aren't necessarily huge nodes with vertical scaling, they're mid-sized boxen that have to share all the resources with many other processes. So, we may entirely separate fixed-size allocations and notification/polling subsystems as concerns, about the box: and the network. For example, then often these are virtualized/containerized mid-size boxen, where it's not only that processes shouldn't contend/thrash with the allocator: also when there's memory-pressure that the biggest one gets booted, or "OOMKiller". Then, for example, since the box already has connection limits, and, for example, these get exhausted before this sort of "I/O-handle-bound process-model", it naturally throttles itself. Where pselect() has a limit of FD_SETSIZE handles, of open connections, and furthermore the numbered file-descriptors must be less than FD_SETSIZE, then scaling out in child-processes or workers/laborers, is old-fashioned and stable. launcher <- eg, systemd, privileged port leader <- 1,000-many potential laborers (child processes), fixed-heap listener <- accepts TCP or 1,000-many potential UDP "connections" laborer(s) <- 1,000-many potential connections logger <- funnels the logs logmet <- funnels the metrics logdmp <- funnels dumps loconsole <- optional REPL This is "1,000-many file-descriptors for connections, a dozen for a 'process-protocol' among the 'process-tree' the 'process-group', and 0/1/2 being stdin/stdout/stderr and 4/5/6 being 'stdlog/stdmet/stddmp'", that being an "I/O-handle-bounde process-model". It makes sense that a hog with the flops on dedicated hardware can use all the resources for itself, that not usually being the case. The "thinking about capacity up-front" is a great idea, here that nobody knows what the front-end and back-end capacity will eventually be, for protocols like TCP/IP that "surface the pressure" and a box that doesn't burst under the burst-y. It's appreciated these sorts of accounts.
Technical Details
Message-ID:
<I7ucnZQf_KTt42D3nZ2dnZfqn_idnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org> <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com> <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me> <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me> <nrecnTdvuvbACWr3nZ2dnZfqn_adnZ2d@giganews.com> <11dfgtf$3pj6i$1@raubtier-asyl.eternal-september.org> <dmKdnV8-pPdQ_Gb3nZ2dnZfqnPadnZ2d@giganews.com> <11dfrgp$3te4n$3@dont-email.me> <45-dnT0eC7C5hWH3nZ2dnZfqnPadnZ2d@giganews.com> <11dip1b$t2p6$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 18 Nov 2026 10:31:12 +0000 Subject: Re: Inversion-of-control and dependency-injection in C Newsgroups: comp.lang.c References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org> <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com> <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me> <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me> <nrecnTdvuvbACWr3nZ2dnZfqn_adnZ2d@giganews.com> <11dfgtf$3pj6i$1@raubtier-asyl.eternal-september.org> <dmKdnV8-pPdQ_Gb3nZ2dnZfqnPadnZ2d@giganews.com> <11dfrgp$3te4n$3@dont-email.me> <45-dnT0eC7C5hWH3nZ2dnZfqnPadnZ2d@giganews.com> <11dip1b$t2p6$1@dont-email.me> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Wed, 18 Nov 2026 02:30:51 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <11dip1b$t2p6$1@dont-email.me> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <I7ucnZQf_KTt42D3nZ2dnZfqn_idnZ2d@giganews.com> Lines: 186 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-TrAtIMhNZAhfCSBXvgf/3vckBayk3YhyvMFifJ5Oo4VB1uDoQij6MjzD48HJxc1l+SpoE/wqrTCvR4M!hgDdrL2/pmJjn/CHr+37SxAMW5OYNuCsYQLlys5gEPTinh53l7TXSbgNAGzS1R8LECKmwX9q7gQU X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40