API reference¶
rulfo ¶
Messaging where a request gets zero, one or many replies and always an end.
Peer ¶
The requests to and from one remote party, over one connection after another.
Keep one Peer for each remote party, also across reconnects: it remembers the ids it ran.
A second Peer for the same party runs a repeated request again. A server keeps them in a
dict by the authenticated name of the caller, and can drop one when idle is true and no
connection serves it.
Use it from one event loop.
| PARAMETER | DESCRIPTION |
|---|---|
handlers
|
The handler for each key this side serves. It returns the one reply, or None
after it sent its replies with
TYPE:
|
config
|
The settings.
TYPE:
|
clock
|
Milliseconds that never go back and count suspended time. Processes that send copies of one id need a shared clock (rule 14 in the operations page).
TYPE:
|
name
|
A name for the remote party. Handlers read it as
TYPE:
|
Source code in src/rulfo/_peer.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
idle
property
¶
No open request, running handler or kept id: dropping the Peer loses nothing.
wait_connected
async
¶
Returns when a request can be sent. Limit the wait with asyncio.timeout.
| RAISES | DESCRIPTION |
|---|---|
PeerClosed
|
|
Source code in src/rulfo/_peer.py
serve
async
¶
Uses one connection until it ends, and closes it.
A call replaces the connection of an earlier call, which then returns. The caller decides when to connect again.
| RAISES | DESCRIPTION |
|---|---|
PeerClosed
|
|
ProtocolError
|
The other side broke the wire format, or the handshake took too long. |
Overloaded
|
The other side takes too little of what this side sends. |
Exception
|
What |
Source code in src/rulfo/_peer.py
request
async
¶
Sends a request and returns its outcome with the replies.
Without a connection the outcome is unroutable at once. A request that was sent
stays open across reconnects until timeout. Cancelling the call stops the wait: the
other side is not told, and may run the request to its end.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
Which handler runs it.
TYPE:
|
body
|
The bytes for the handler.
TYPE:
|
timeout
|
Seconds until the request expires. All replies are kept in memory.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
An empty key, or a key and body above the largest length. |
PeerClosed
|
|
Source code in src/rulfo/_peer.py
request_copy
async
¶
request_copy(
request_id: bytes,
key: str,
body: bytes = b"",
*,
deadline_ms: int,
sent_before: bool,
) -> Result
Sends a request whose id and deadline the caller chose, and returns its outcome.
Use it when more than one process may send the same request, for example when a second process takes over from one that stopped. All of them pass the same id, key, body and deadline, and read the same clock (rule 14 in the operations page).
With sent_before false it behaves as request. With sent_before true another
copy may have run, so the outcome is never unroutable or busy: without a
connection the call waits for one. The replier runs the request only if it does not know
the id. A replier that cannot take it (no handler for the key, or a limit reached) does
not answer, and the call ends expired at the deadline.
Do not send an id again after you cancelled its call.
| PARAMETER | DESCRIPTION |
|---|---|
request_id
|
16 random bytes, the same in every copy.
TYPE:
|
key
|
Which handler runs it.
TYPE:
|
body
|
The bytes for the handler.
TYPE:
|
deadline_ms
|
When the request expires, in milliseconds on
TYPE:
|
sent_before
|
Whether a copy may have been sent already. When in doubt, pass true.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
An id that is not 16 bytes, a deadline too far away, an empty key, or a key and body above the largest length. |
CallError
|
This |
PeerClosed
|
|
Source code in src/rulfo/_peer.py
aclose
async
¶
Closes the connection, fails the open requests and cancels the handlers.
Bytes that are not sent yet are dropped, also the end of a handler that just returned.
Source code in src/rulfo/_peer.py
Request ¶
A request that a handler runs.
caller is the name of the Peer it came to: with Server, the name given to
Server.serve.
repeat means that the requester sent it before and this peer does not know the id: a
process that ran earlier may have run it. A handler that must not run twice across a restart
checks its own record when repeat is true.
Source code in src/rulfo/_peer.py
remaining ¶
reply
async
¶
Sends one reply. Waits while too many bytes are not yet sent.
Without a connection the reply is stored and sent when the requester asks again.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
A body longer than |
RuntimeError
|
The handler of this request has finished. |
Source code in src/rulfo/_peer.py
Server ¶
One Peer for each remote party, found by name.
The program accepts the connections, checks who is calling, and passes each one to serve
with that name. The Peer of a name stays across reconnects, so a request that is sent
again does not run twice. A Peer that is idle and has no connection is dropped when the
next connection arrives.
Each Peer allocates memory (about 235 KB with the default max_ids), and the limits of
config count for each party separately.
Use it from one event loop.
| PARAMETER | DESCRIPTION |
|---|---|
handlers
|
The handlers of every party.
TYPE:
|
config
|
The settings of every
TYPE:
|
clock
|
The clock of every
TYPE:
|
Source code in src/rulfo/_server.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
serve
async
¶
Uses one connection from name until it ends, and closes it.
A second connection with the same name replaces the first, whose call returns. Pass a name that you checked: whoever gives a name gets its requests.
| RAISES | DESCRIPTION |
|---|---|
PeerClosed
|
|
ProtocolError
|
The other side broke the wire format, or the handshake took too long. |
Overloaded
|
The other side takes too little of what this side sends. |
Exception
|
What |
Source code in src/rulfo/_server.py
request
async
¶
Sends a request to name. See Peer.request.
The outcome is unroutable when name has no connection. During the handshake of a
connection the call waits.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
An empty key, a timeout out of range, or a key and body above the largest length. |
PeerClosed
|
|
Source code in src/rulfo/_server.py
request_copy
async
¶
request_copy(
name: str,
request_id: bytes,
key: str,
body: bytes = b"",
*,
deadline_ms: int,
sent_before: bool,
) -> Result
Sends to name a request whose id and deadline the caller chose.
See Peer.request_copy. With sent_before true the call also waits for a name that
has not connected yet.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
As |
CallError
|
As |
PeerClosed
|
|
Source code in src/rulfo/_server.py
aclose
async
¶
Closes every Peer. If this call is cancelled, call it again.
Source code in src/rulfo/_server.py
PeerState ¶
The state for one remote party: its open requests, the ids it sent and the bytes to send.
It does no I/O and reads no clock. Every now and deadline is in milliseconds on one clock
that never goes back, does not jump and counts the time the machine is suspended.
For each connection call connected, send what data_to_send returns after every call,
pass what arrives to receive, call poll at next_time, and call disconnected
when the connection ends or after a Closed event. Keep the object for the next one: one
PeerState stands for one remote party, and a second one for the same party would run a
repeated request again.
It sends open requests again after a reconnect, and sends stored replies again when the other side asks, without events.
Source code in src/rulfo/_state.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
idle
property
¶
No open request and no kept id: the object can be dropped without running one twice.
open_bytes
property
¶
Bytes of the keys and bodies kept to send open requests again, with their headers.
connected ¶
disconnected ¶
receive ¶
Reads bytes from the other side, cut anywhere, and returns what happened.
Source code in src/rulfo/_state.py
poll ¶
Returns what is due at now: expired requests and a handshake that took too long.
Source code in src/rulfo/_state.py
next_time ¶
When to call poll next. A time that has passed means now. None: nothing is due.
Source code in src/rulfo/_state.py
data_to_send ¶
request ¶
request(
request_id: bytes,
key: str,
body: bytes,
deadline: int,
now: int,
*,
sent_before: bool = False,
) -> None
Sends a request. Its outcome comes later as Ended, once.
| PARAMETER | DESCRIPTION |
|---|---|
request_id
|
16 bytes that no other request uses.
TYPE:
|
key
|
Which handler runs it. Not empty.
TYPE:
|
body
|
The bytes for the handler.
TYPE:
|
deadline
|
When the request expires.
TYPE:
|
now
|
The time of this call.
TYPE:
|
sent_before
|
An earlier copy of this id was sent, for example by another process, and no end arrived.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
A key or a length that the wire format refuses. |
CallError
|
The connection cannot be used, the deadline has passed, too many requests are open, or the id is open. |
Source code in src/rulfo/_state.py
abandon ¶
Stops waiting for a request: no event is given for it. It may still run.
Does nothing when the request is not open. Do not use the id again.
Source code in src/rulfo/_state.py
reply ¶
Sends one reply to a received request, or only stores it when there is no connection.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
A body longer than the wire format or |
CallError
|
The id is not known, or the request has ended. |
Source code in src/rulfo/_state.py
end ¶
Ends a received request: the requester gets completed.
| RAISES | DESCRIPTION |
|---|---|
CallError
|
The id is not known, or the request has ended. |
Source code in src/rulfo/_state.py
give_up ¶
Closes a received request that did not finish. Nothing is sent, now or on a repeat.
The requester gets expired: the request may have run in part.
| RAISES | DESCRIPTION |
|---|---|
CallError
|
The id is not known, or the request has ended. |
Source code in src/rulfo/_state.py
refuse ¶
Refuses a received request before its first reply, instead of running it.
The refusal is not sent for a request that was sent again, or whose connection ended.
| RAISES | DESCRIPTION |
|---|---|
CallError
|
The id is not known, or a reply or the end was sent. |
Source code in src/rulfo/_state.py
StreamTransport ¶
A connection of asyncio streams: TCP and unix sockets.
Source code in src/rulfo/_transport.py
Transport ¶
Bases: Protocol
One connection. The cuts between chunks have no meaning.
Source code in src/rulfo/_transport.py
Busy ¶
Bases: RulfoError
Raised by a handler, before its first reply, to refuse the request. It did not run.
CallError ¶
Bases: RulfoError
A call that is not valid now. status is the name of the core status.
Source code in src/rulfo/_types.py
Closed
dataclass
¶
Config
dataclass
¶
The settings of one peer. Times are in seconds.
| PARAMETER | DESCRIPTION |
|---|---|
max_length
|
Largest key plus body accepted.
TYPE:
|
handshake
|
Longest wait for the other side after a connection starts.
TYPE:
|
keep
|
How long a request id is kept after its deadline. Not below
TYPE:
|
max_requests
|
Open requests this side can have.
TYPE:
|
max_ids
|
Request ids this side keeps as the replier. New requests above
TYPE:
|
max_deadline
|
A request with a deadline further away is refused as busy.
TYPE:
|
replay_bytes
|
Memory for sent replies, to send them again after a reconnect. 0 stores none.
TYPE:
|
replay_bytes_per_request
|
The same for one request. A request that goes over stores none.
TYPE:
|
max_running
|
Handlers that run at once. Above it a request is refused as busy.
TYPE:
|
send_buffer
|
TYPE:
|
Source code in src/rulfo/_types.py
Ended
dataclass
¶
NotCompleted ¶
Bases: RulfoError
The request has another outcome than completed.
Source code in src/rulfo/_types.py
Outcome ¶
Bases: Enum
How a request ended.
Source code in src/rulfo/_types.py
COMPLETED
class-attribute
instance-attribute
¶
The handler ran and every reply it sent arrived.
EXPIRED
class-attribute
instance-attribute
¶
The deadline passed first. It may have run.
UNROUTABLE
class-attribute
instance-attribute
¶
No handler for the key, or no connection to send it on. It did not run.
Overloaded ¶
Bases: RulfoError
The other side takes too little of what is sent to it: the connection was ended.
PeerClosed ¶
Bases: RulfoError
The peer was closed.
ProtocolError ¶
Bases: RulfoError
The other side broke the wire format. status is the name of the core status.
Source code in src/rulfo/_types.py
Ready
dataclass
¶
RemoteError ¶
Bases: RulfoError
The handler replied with an error.
Reply
dataclass
¶
ReplyReceived
dataclass
¶
RequestReceived
dataclass
¶
A request to run. Each one needs end, refuse or give_up.
deadline is in milliseconds on this side's clock. repeat means that the requester
sent it before and this side does not know the id: a process that ran earlier may have run it.
Source code in src/rulfo/_types.py
Result
dataclass
¶
The outcome of a request and the replies that arrived.
Source code in src/rulfo/_types.py
one ¶
The body of the only reply of a completed request.
| RAISES | DESCRIPTION |
|---|---|
NotCompleted
|
The outcome is not |
RemoteError
|
The reply is an error reply. |
ValueError
|
The request completed with no reply or with more than one. |