![]() ![]() ![]() The table contains a map where the key is the file descriptor (FD) and the value is a file pointer. File descriptors are indices to the OS’s file descriptor table. While not SwiftNIO specific, file descriptors play a role in its functionality. The following sections take a deeper look at the SwiftNIO architecture. Now, the thread is always doing work as long as there are events. When the EventLoop is notified of an event such as data ready to be consumed from a socket, it can then process this event. With this approach, there is an EventLoop that runs constantly waiting for new events. Instead, it defers to the OS to notify the application when an event of interest has occurred such as a new socket connection opened. ![]() With non-blocking I/O, the application no longer blocks when reading/writing data. Also, I/O is not a CPU bound task, so while our thread is waiting for data to come from the socket, it’s being wasted since it could be doing CPU bound work. This introduces immense overhead due to excessive context switching and high memory usage since each thread stack has to be stored in memory. If there are 100,000 client connections, 100,000 threads are created to handle the connections. Unfortunately, this solution doesn’t scale. This is known as Multi-threaded Blocking I/O. Each thread blocks to perform I/O on the socket to read and write data to the connections. Every time a client connects to the server, a new thread is created which is responsible for handling that connection. When a new connection is created, it spawns a new thread to handle the connection. In this model, the server has one thread consistently running that blocks by listening on the socket awaiting new connections. When I first learned about networking and building servers, I was introduced to the thread per connection model. I find that the best way that I learn something new is to try to explain it to others, so this is my attempt at making SwiftNIO a little less complex. This is a deep dive into the various pieces of SwiftNIO and is meant to supplement the official documentation. I say low-level because you wouldn’t use SwiftNIO directly to build web applications, instead you’d use a web framework based on SwiftNIO like Vapor or the Smoke Framework instead. SwiftNIO is an elegant low-level networking framework for Swift applications. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |