xref: /aosp_15_r20/external/libwebsockets/READMEs/README.lws_retry.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker# `lws_retry_bo_t` client connection management
2*1c60b9acSAndroid Build Coastguard Worker
3*1c60b9acSAndroid Build Coastguard WorkerThis struct sets the policy for delays between retries, and for
4*1c60b9acSAndroid Build Coastguard Workerhow long a connection may be 'idle' before it first tries to
5*1c60b9acSAndroid Build Coastguard Workerping / pong on it to confirm it's up, or drops the connection
6*1c60b9acSAndroid Build Coastguard Workerif still idle.
7*1c60b9acSAndroid Build Coastguard Worker
8*1c60b9acSAndroid Build Coastguard Worker## Retry rate limiting
9*1c60b9acSAndroid Build Coastguard Worker
10*1c60b9acSAndroid Build Coastguard WorkerYou can define a table of ms-resolution delays indexed by which
11*1c60b9acSAndroid Build Coastguard Workerconnection attempt number is ongoing, this is pointed to by
12*1c60b9acSAndroid Build Coastguard Worker`.retry_ms_table` with `.retry_ms_table_count` containing the
13*1c60b9acSAndroid Build Coastguard Workercount of table entries.
14*1c60b9acSAndroid Build Coastguard Worker
15*1c60b9acSAndroid Build Coastguard Worker`.conceal_count` is the number of retries that should be allowed
16*1c60b9acSAndroid Build Coastguard Workerbefore informing the parent that the connection has failed.  If it's
17*1c60b9acSAndroid Build Coastguard Workergreater than the number of entries in the table, the last entry is
18*1c60b9acSAndroid Build Coastguard Workerreused for the additional attempts.
19*1c60b9acSAndroid Build Coastguard Worker
20*1c60b9acSAndroid Build Coastguard Worker`.jitter_percent` controls how much additional random delay is
21*1c60b9acSAndroid Build Coastguard Workeradded to the actual interval to be used... this stops a lot of
22*1c60b9acSAndroid Build Coastguard Workerdevices all synchronizing when they try to connect after a single
23*1c60b9acSAndroid Build Coastguard Workertrigger event and DDoS-ing the server.
24*1c60b9acSAndroid Build Coastguard Worker
25*1c60b9acSAndroid Build Coastguard WorkerThe struct and apis are provided for user implementations, lws does
26*1c60b9acSAndroid Build Coastguard Workernot offer reconnection itself.
27*1c60b9acSAndroid Build Coastguard Worker
28*1c60b9acSAndroid Build Coastguard Worker## Connection validity management
29*1c60b9acSAndroid Build Coastguard Worker
30*1c60b9acSAndroid Build Coastguard WorkerLws has a sophisticated idea of connection validity and the need to
31*1c60b9acSAndroid Build Coastguard Workerreconfirm that a connection is still operable if proof of validity
32*1c60b9acSAndroid Build Coastguard Workerhas not been seen for some time.  It concerns itself only with network
33*1c60b9acSAndroid Build Coastguard Workerconnections rather than streams, for example, it only checks h2
34*1c60b9acSAndroid Build Coastguard Workernetwork connections rather than the individual streams inside (which
35*1c60b9acSAndroid Build Coastguard Workeris consistent with h2 PING frames only working at the network stream
36*1c60b9acSAndroid Build Coastguard Workerlevel itself).
37*1c60b9acSAndroid Build Coastguard Worker
38*1c60b9acSAndroid Build Coastguard WorkerConnections may fail in a variety of ways, these include that no traffic
39*1c60b9acSAndroid Build Coastguard Workerat all is passing, or, eg, incoming traffic may be received but no
40*1c60b9acSAndroid Build Coastguard Workeroutbound traffic is making it to the network, and vice versa.  In the
41*1c60b9acSAndroid Build Coastguard Workercase that tx is not failing at any point but just isn't getting sent,
42*1c60b9acSAndroid Build Coastguard Workerendpoints can potentially kid themselves that since "they are sending"
43*1c60b9acSAndroid Build Coastguard Workerand they are seeing RX, the combination means the connection is valid.
44*1c60b9acSAndroid Build Coastguard WorkerThis can potentially continue for a long time if the peer is not
45*1c60b9acSAndroid Build Coastguard Workerperforming keepalives.
46*1c60b9acSAndroid Build Coastguard Worker
47*1c60b9acSAndroid Build Coastguard Worker"Connection validity" is proven when one side sends something and later
48*1c60b9acSAndroid Build Coastguard Workerreceives a response that can only have been generated by the peer
49*1c60b9acSAndroid Build Coastguard Workerreceiving what was just sent.  This can happen for some kinds of user
50*1c60b9acSAndroid Build Coastguard Workertransactions on any stream using the connection, or by sending PING /
51*1c60b9acSAndroid Build Coastguard WorkerPONG protocol packets where the PONG is only returned for a received PING.
52*1c60b9acSAndroid Build Coastguard Worker
53*1c60b9acSAndroid Build Coastguard WorkerTo ensure that the generated traffic is only sent when necessary, user
54*1c60b9acSAndroid Build Coastguard Workercode can report for any stream that it has observed a transaction amounting
55*1c60b9acSAndroid Build Coastguard Workerto a proof of connection validity using an api.  This resets the timer for
56*1c60b9acSAndroid Build Coastguard Workerthe associated network connection before the validity is considered
57*1c60b9acSAndroid Build Coastguard Workerexpired.
58*1c60b9acSAndroid Build Coastguard Worker
59*1c60b9acSAndroid Build Coastguard Worker`.secs_since_valid_ping` in the retry struct sets the number of seconds since
60*1c60b9acSAndroid Build Coastguard Workerthe last validity after which lws will issue a protocol-specific PING of some
61*1c60b9acSAndroid Build Coastguard Workerkind on the connection.  `.secs_since_valid_hangup` specifies how long lws
62*1c60b9acSAndroid Build Coastguard Workerwill allow the connection to go without a confirmation of validity before
63*1c60b9acSAndroid Build Coastguard Workersimply hanging up on it.
64*1c60b9acSAndroid Build Coastguard Worker
65*1c60b9acSAndroid Build Coastguard Worker## Defaults
66*1c60b9acSAndroid Build Coastguard Worker
67*1c60b9acSAndroid Build Coastguard WorkerThe context defaults to having a 5m valid ping interval and 5m10s hangup interval,
68*1c60b9acSAndroid Build Coastguard Workerie, it'll send a ping at 5m idle if the protocol supports it, and if no response
69*1c60b9acSAndroid Build Coastguard Workervalidating the connection arrives in another 10s, hang up the connection.
70*1c60b9acSAndroid Build Coastguard Worker
71*1c60b9acSAndroid Build Coastguard WorkerUser code can set this in the context creation info and can individually set the
72*1c60b9acSAndroid Build Coastguard Workerretry policy per vhost for server connections.  Client connections can set it
73*1c60b9acSAndroid Build Coastguard Workerper connection in the client creation info `.retry_and_idle_policy`.
74*1c60b9acSAndroid Build Coastguard Worker
75*1c60b9acSAndroid Build Coastguard Worker## Checking for h2 and ws
76*1c60b9acSAndroid Build Coastguard Worker
77*1c60b9acSAndroid Build Coastguard WorkerCheck using paired minimal examples with the -v flag on one or both sides to get a
78*1c60b9acSAndroid Build Coastguard Workersmall validity check period set of 3s / 10s
79*1c60b9acSAndroid Build Coastguard Worker
80*1c60b9acSAndroid Build Coastguard WorkerAlso give, eg, -d1039 to see info level debug logging
81*1c60b9acSAndroid Build Coastguard Worker
82*1c60b9acSAndroid Build Coastguard Worker### h2
83*1c60b9acSAndroid Build Coastguard Worker
84*1c60b9acSAndroid Build Coastguard Worker```
85*1c60b9acSAndroid Build Coastguard Worker$ lws-minimal-http-server-h2-long-poll -v
86*1c60b9acSAndroid Build Coastguard Worker
87*1c60b9acSAndroid Build Coastguard Worker$ lws-minimal-http-client -l -v
88*1c60b9acSAndroid Build Coastguard Worker```
89*1c60b9acSAndroid Build Coastguard Worker
90*1c60b9acSAndroid Build Coastguard Worker### ws
91*1c60b9acSAndroid Build Coastguard Worker
92*1c60b9acSAndroid Build Coastguard Worker```
93*1c60b9acSAndroid Build Coastguard Worker$ lws-minimal-ws-server-h2 -s -v
94*1c60b9acSAndroid Build Coastguard Worker
95*1c60b9acSAndroid Build Coastguard Worker$ lws-minimal-ws-client-ping -n --server 127.0.0.1 --port 7681 -v
96*1c60b9acSAndroid Build Coastguard Worker```
97*1c60b9acSAndroid Build Coastguard Worker
98*1c60b9acSAndroid Build Coastguard Worker
99