sok / Sok.Selector / Selector
class Selector
(source)
Platform and version requirements: JVM, Native
The Sok Selector class helps to go from a crappy blocking NIO Selector interface to a nice coroutine based interface. The Selector will take care of registrations to the underlying NIO Selector in a non-blocking and non-suspending way in order to have the best performances possible. In order to do that (and because the NIO Selector is blocking) the Selector have a single thread executor to which we will give “ticks” tasks, when a socket wants to register we will pause the ticking, register and send a resume task to the executor, if the registration task is fast enough the ticking will not be paused.
The Selector uses the SuspentionMap class to get and resume the suspention or execute the callback. There is two kinds of operation for the SuspentionMap, resume the continuation and unregister the interest directly (for single read operations) or execute a callback that will return whether or not we should unregister. The goal of those two “modes” is to reduce the number of (expensive) interest update operations. The counterpart is that the Selector now execute user-code which can greatly slow down the ticking rate, the developer should be careful.
<init> | Selector() The Sok Selector class helps to go from a crappy blocking NIO Selector interface to a nice coroutine based interface. The Selector will take care of registrations to the underlying NIO Selector in a non-blocking and non-suspending way in order to have the best performances possible. In order to do that (and because the NIO Selector is blocking) the Selector have a single thread executor to which we will give “ticks” tasks, when a socket wants to register we will pause the ticking, register and send a resume task to the executor, if the registration task is fast enough the ticking will not be paused. |
isClosed | var isClosed: Boolean is the Selector still running |
isInSelection (Native) |
var isInSelection: Boolean is the selector in a selection loop |
numberOfChannel (JVM) |
var numberOfChannel: Int Used for load balancing by the SelectorPool |
close | fun close(): Unit Close the selector, close all SuspentionMap, cancel the main loop and close the thread |
register (JVM) |
fun register(channel: SelectableChannel , interest: Int , attachment: Any ?): SelectionKey Register a SelectableChannel the NIO Selector and bind the attachment (SuspentionMap ) |
updateInterest (JVM) |
fun updateInterest(sk: SelectionKey , interest: Int ): Unit Update the interest of a SelectionKey with the given interest |
wakeup (JVM) |
fun wakeup(): Unit Wake the NIO selector up if needed to allow registrations |
defaultSelector | val defaultSelector: <ERROR CLASS> default selector |
defaultSelectorPool (JVM) |
val defaultSelectorPool: <ERROR CLASS> default selector pool |
isSelectorPoolInit (JVM) |
var isSelectorPoolInit: Boolean is the selector pool initialized. |
closeSelectorAndWait (Native) |
suspend fun closeSelectorAndWait(): Unit Close the selector |
setDefaultScope (Native) |
fun setDefaultScope(scope: <ERROR CLASS>): Unit Bind the Selector to a particular CoroutineScope, mainly used in order to prevent the event loop from exiting while te selector is still running |