Debian Codegraph @

doc/src/guide/internals.asciidoc

raw
1 == Internals
2
3 This chapter may not apply to embedded Ranch as embedding allows you
4 to use an architecture specific to your application, which may or may
5 not be compatible with the description of the Ranch application.
6
7 Note that for everything related to efficiency and performance,
8 you should perform the benchmarks yourself to get the numbers that
9 matter to you. Generic benchmarks found on the web may or may not
10 be of use to you, you can never know until you benchmark your own
11 system.
12
13 A third party dive into the internals of Ranch is available should
14 you be interested: https://baozi.technology/ranch-under-the-hood/[Ranch: what's under the hood?]
15 We make no claims with regard to its freshness or accuracy but this
16 is a nice document to read along this section.
17
18 === Architecture
19
20 Ranch is an OTP application.
21
22 Like all OTP applications, Ranch has a top supervisor. It is responsible
23 for supervising the `ranch_server` process and all the listeners that
24 will be started.
25
26 The `ranch_server` gen_server is a central process keeping track of the
27 listeners and their acceptors. It does so through the use of a public ets
28 table called `ranch_server`. The table is owned by the top supervisor
29 to improve fault tolerance. This way if the `ranch_server` gen_server
30 fails, it doesn't lose any information and the restarted process can
31 continue as if nothing happened.
32
33 Ranch uses a custom supervisor for managing connections. This supervisor
34 keeps track of the number of connections and handles connection limits
35 directly. While it is heavily optimized to perform the task of creating
36 connection processes for accepted connections, it is still following the
37 OTP principles and the usual `sys` and `supervisor` calls will work on
38 it as expected.
39
40 Listeners are grouped into the `ranch_listener_sup` supervisor and
41 consist of three kinds of processes: the listener gen_server, the
42 acceptor processes and the connection processes, both grouped under
43 their own supervisor. All of these processes are registered to the
44 `ranch_server` gen_server with varying amount of information.
45
46 All socket operations, including listening for connections, go through
47 transport handlers. Accepted connections are given to the protocol handler.
48 Transport handlers are simple callback modules for performing operations on
49 sockets. Protocol handlers start a new process, which receives socket
50 ownership, with no requirements on how the code should be written inside
51 that new process.
52
53 === Number of acceptors
54
55 The second argument to `ranch:start_listener/5` is the number of
56 processes that will be accepting connections. Care should be taken
57 when choosing this number.
58
59 First of all, it should not be confused with the maximum number
60 of connections. Acceptor processes are only used for accepting and
61 have nothing else in common with connection processes. Therefore
62 there is nothing to be gained from setting this number too high,
63 in fact it can slow everything else down.
64
65 Second, this number should be high enough to allow Ranch to accept
66 connections concurrently. But the number of cores available doesn't
67 seem to be the only factor for choosing this number, as we can
68 observe faster accepts if we have more acceptors than cores. It
69 might be entirely dependent on the protocol, however.
70
71 Our observations suggest that using 100 acceptors on modern hardware
72 is a good solution, as it's big enough to always have acceptors ready
73 and it's low enough that it doesn't have a negative impact on the
74 system's performances.
75
76 === Platform-specific TCP features
77
78 Some socket options are platform-specific and not supported by `inet`.
79 They can be of interest because they generally are related to
80 optimizations provided by the underlying OS. They can still be enabled
81 thanks to the `raw` option, for which we will see an example.
82
83 One of these features is `TCP_DEFER_ACCEPT` on Linux. It is a simplified
84 accept mechanism which will wait for application data to come in before
85 handing out the connection to the Erlang process.
86
87 This is especially useful if you expect many connections to be mostly
88 idle, perhaps part of a connection pool. They can be handled by the
89 kernel directly until they send any real data, instead of allocating
90 resources to idle connections.
91
92 To enable this mechanism, the following option can be used.
93
94 .Using raw transport options
95
96 [source,erlang]
97 {raw, 6, 9, << 30:32/native >>}
98
99 It means go on layer 6, turn on option 9 with the given integer parameter.