packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// A pool of connections (issue gabla108/louise#145 §3): the pool actor hands out |
| 2 | /// connections, one borrower at a time, and watches everyone it talks to |
| 3 | /// — a dead borrower frees its connection (closed, not reused: its |
| 4 | /// transaction state is unknown), a dead connection frees its slot. |
| 5 | /// |
| 6 | /// Waiting callers queue; the acquire timeout is the caller's |
| 7 | /// (`acquire`'s), and a `CancelAcquire` best-effort message drops a |
| 8 | /// request whose waiter gave up. |
| 9 | /// |
| 10 | /// The knobs (`PoolSettings`): `min_idle` keeps a floor of connections |
| 11 | /// warm, `idle_timeout` closes connections that sit idle (down to the |
| 12 | /// floor), `max_lifetime` recycles old connections when they come back, |
| 13 | /// and a connection that sat idle past `check_after` is pinged (a bare |
| 14 | /// `Sync`, the cheapest round trip the protocol has) before it is handed |
| 15 | /// out, so a stale connection never reaches a borrower. Durations are in |
| 16 | /// milliseconds, evaluated with `tick` granularity: the pool wakes every |
| 17 | /// `tick` to maintain itself. Durations are approximate by a tick, and |
| 18 | /// ticks only fire while the pool is quiet — which is when reaping |
| 19 | /// matters; handouts and returns are checked per message. |
| 20 | |
| 21 | import std/actor.{Continue, Stop} |
| 22 | import std/list |
| 23 | import std/process.{type Down, type Pid, type Subject, type proc, Down} |
| 24 | import std/random.{type random} |
| 25 | import std/result |
| 26 | |
| 27 | import client |
| 28 | import config.{type Config} |
| 29 | |
| 30 | /// A pool of connections. |
| 31 | pub opaque type Pool { Pool(Subject<Msg>) } |
| 32 | |
| 33 | /// The pool's knobs. `pool.defaults(size)` is a good start; adjust with |
| 34 | /// a record update. A `0` duration disables its check. |
| 35 | pub type PoolSettings |
| 36 | { PoolSettings( /// The most connections the pool holds, idle or out. |
| 37 | size: Int |
| 38 | /// The floor of idle connections the tick keeps warm (clamped to |
| 39 | /// `size`). |
| 40 | , min_idle: Int |
| 41 | /// Close a connection idle this long, down to `min_idle` (0: never). |
| 42 | , idle_timeout: Int |
| 43 | /// Close a connection this old when it comes back, instead of |
| 44 | /// reusing it (0: never). |
| 45 | , max_lifetime: Int |
| 46 | /// Ping a connection that sat idle this long before handing it out |
| 47 | /// (0: never). On by default: a stale connection fails the ping and |
| 48 | /// is replaced, instead of failing a borrower's first query. |
| 49 | , check_after: Int |
| 50 | /// The maintenance period, in milliseconds. |
| 51 | , tick: Int |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | /// `size` connections, no reaping, a liveness check past 30 s idle, a |
| 56 | /// one-second tick. |
| 57 | pub fn defaults :: size: Int -> PoolSettings |
| 58 | { |
| 59 | PoolSettings( size: size |
| 60 | , min_idle: 0 |
| 61 | , idle_timeout: 0 |
| 62 | , max_lifetime: 0 |
| 63 | , check_after: 30000 |
| 64 | , tick: 1000 |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | /// The pool's messages. |
| 69 | pub type Msg |
| 70 | { /// Check a connection out; the answer is the connection's subject (or |
| 71 | /// why not — connecting one failed). |
| 72 | Acquire( |
| 73 | borrower: Pid |
| 74 | , reply: Subject<Result<Subject<client.Msg>, client.Error>> |
| 75 | ) |
| 76 | /// A waiter that gave up; drop its queued request. |
| 77 | , CancelAcquire(borrower: Pid) |
| 78 | /// Return a connection. |
| 79 | , Release(conn: Subject<client.Msg>) |
| 80 | /// Relinquish a connection whose state cannot be trusted; close, never |
| 81 | /// reuse it. |
| 82 | , Discard(conn: Subject<client.Msg>) |
| 83 | } |
| 84 | |
| 85 | /// What the pool's mailbox carries: its messages, monitor reports, and |
| 86 | /// ping answers. |
| 87 | type Event |
| 88 | { Ev(Msg) |
| 89 | , DownEv(Down) |
| 90 | , PongEv(conn: Subject<client.Msg>, result: Result<(), client.Error>) |
| 91 | } |
| 92 | |
| 93 | /// A connection the pool tracks: its subject and its age in ticks. |
| 94 | type ConnInfo { ConnInfo(conn: Subject<client.Msg>, age: Int) } |
| 95 | |
| 96 | /// A handout waiting on a ping: the borrower, its answer subject, the |
| 97 | /// connection, the ping's reply subject, and ticks since the ping. |
| 98 | type Checking |
| 99 | { Checking( |
| 100 | borrower: Pid |
| 101 | , reply: Subject<Result<Subject<client.Msg>, client.Error>> |
| 102 | , info: ConnInfo |
| 103 | , pong: Subject<Result<(), client.Error>> |
| 104 | , ticks: Int |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | type PoolState |
| 109 | { PoolState( |
| 110 | config: Config |
| 111 | , settings: PoolSettings |
| 112 | /// The pool's own subject. |
| 113 | , inbox: Subject<Msg> |
| 114 | /// Connections ready to hand out, with their idle time in ticks. |
| 115 | , idle: List<(ConnInfo, Int)> |
| 116 | /// (borrower, connection) pairs. |
| 117 | , checked_out: List<(Pid, ConnInfo)> |
| 118 | /// Handouts waiting on a ping. |
| 119 | , checking: List<Checking> |
| 120 | /// Waiting borrowers, in order: (pid, its reply subject). |
| 121 | , waiting: List<(Pid, Subject<Result<Subject<client.Msg>, client.Error>>)> |
| 122 | /// Everyone watched: connection actors and borrowers. |
| 123 | , monitored: List<(Pid, Subject<Down>)> |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | /// A pool of `size` connections, made now, with the default settings. |
| 128 | /// The pool replaces dead connections as they're needed. |
| 129 | pub fn start :: |
| 130 | cfg: Config |
| 131 | size: Int |
| 132 | -> Result<Pool, client.Error> / <io, proc, random> |
| 133 | { |
| 134 | start_with cfg (defaults size) |
| 135 | } |
| 136 | |
| 137 | /// A pool with explicit settings, its connections made now. |
| 138 | pub fn start_with :: |
| 139 | cfg: Config |
| 140 | settings: PoolSettings |
| 141 | -> Result<Pool, client.Error> / <io, proc, random> |
| 142 | { |
| 143 | let settings = sane settings |
| 144 | use conns <- result.try (connect_all cfg settings.size []) |
| 145 | let ready = process.subject () |
| 146 | { |
| 147 | use <- process.spawn |
| 148 | let inbox = process.subject () |
| 149 | process.send ready inbox |
| 150 | serve |
| 151 | PoolState( cfg |
| 152 | , settings |
| 153 | , inbox |
| 154 | , list.map conns fn c { (ConnInfo(c, 0), 0) } |
| 155 | , [] |
| 156 | , [] |
| 157 | , [] |
| 158 | , monitor_all conns [] |
| 159 | ) |
| 160 | } |
| 161 | Ok(Pool(process.receive_forever ready)) |
| 162 | } |
| 163 | |
| 164 | /// Settings with the clamps applied: a positive tick, `min_idle` within |
| 165 | /// `size`. |
| 166 | fn sane :: settings: PoolSettings -> PoolSettings |
| 167 | { |
| 168 | let tick = if settings.tick <= 0 { 1000 } else { settings.tick } |
| 169 | let min_idle = if settings.min_idle < 0 |
| 170 | { |
| 171 | 0 |
| 172 | } |
| 173 | else if settings.min_idle > settings.size |
| 174 | { |
| 175 | settings.size |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | settings.min_idle |
| 180 | } |
| 181 | PoolSettings(..settings, tick: tick, min_idle: min_idle) |
| 182 | } |
| 183 | |
| 184 | /// `size` connections, or the first failure. |
| 185 | fn connect_all :: |
| 186 | cfg: Config |
| 187 | size: Int |
| 188 | acc: List<Subject<client.Msg>> |
| 189 | -> Result<List<Subject<client.Msg>>, client.Error> / <io, proc, random> |
| 190 | { |
| 191 | if size <= 0 |
| 192 | { |
| 193 | Ok(list.reverse acc) |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | use conn <- result.try (client.connect cfg) |
| 198 | connect_all cfg (size - 1) [conn, ..acc] |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // ---- the protocol (used by lib.lse) ------------------------------------------- |
| 203 | |
| 204 | /// A connection from the pool, or why not (nothing within the timeout). |
| 205 | /// A grant that races a cancel is returned at once, never leaked. |
| 206 | pub fn acquire :: |
| 207 | pool: Pool |
| 208 | timeout: Int |
| 209 | -> Result<Subject<client.Msg>, client.Error> / <proc> |
| 210 | { |
| 211 | let Pool(subject) = pool |
| 212 | let me = process.self () |
| 213 | let reply = process.subject () |
| 214 | process.send subject Acquire(me, reply) |
| 215 | match process.receive reply timeout |
| 216 | { Some(answer) -> answer |
| 217 | , None -> |
| 218 | { |
| 219 | actor.cast subject CancelAcquire(me) |
| 220 | match process.receive reply 0 |
| 221 | { Some(Ok(conn)) -> |
| 222 | { |
| 223 | actor.cast subject Release(conn) |
| 224 | Error(client.Driver("no connection within the timeout")) |
| 225 | } |
| 226 | , _ -> Error(client.Driver("no connection within the timeout")) |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// Return a connection to the pool. |
| 233 | pub fn release :: pool: Pool conn: Subject<client.Msg> -> () / <proc> |
| 234 | { |
| 235 | let Pool(subject) = pool |
| 236 | actor.cast subject Release(conn) |
| 237 | } |
| 238 | |
| 239 | /// Relinquish a checked-out connection whose state cannot be trusted. The |
| 240 | /// pool closes it and frees its slot instead of handing it to another caller. |
| 241 | pub fn discard :: pool: Pool conn: Subject<client.Msg> -> () / <proc> |
| 242 | { |
| 243 | let Pool(subject) = pool |
| 244 | actor.cast subject Discard(conn) |
| 245 | } |
| 246 | |
| 247 | // ---- the actor ------------------------------------------------------------------ |
| 248 | |
| 249 | fn serve :: state: PoolState -> () / <proc, io, random> |
| 250 | { |
| 251 | match process.select (selector_of state) state.settings.tick |
| 252 | { Some(event) -> match handle_event state event |
| 253 | { Continue(next) -> serve next, Stop -> () } |
| 254 | // A quiet pool: the tick fires, and maintenance runs. |
| 255 | , None -> serve (maintain state) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /// A selector over the pool's messages, every monitor report, and every |
| 260 | /// outstanding ping. |
| 261 | fn selector_of :: state: PoolState -> process.Selector<Event> |
| 262 | { |
| 263 | let base = process.new_selector () |> process.selecting state.inbox Ev |
| 264 | let with_downs = |
| 265 | list.fold |
| 266 | state.monitored |
| 267 | base |
| 268 | fn sel entry { process.selecting sel entry.1 DownEv } |
| 269 | list.fold |
| 270 | state.checking |
| 271 | with_downs |
| 272 | fn sel entry |
| 273 | { |
| 274 | let Checking(_, _, info, pong, _) = entry |
| 275 | process.selecting sel pong fn res { PongEv(info.conn, res) } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | fn handle_event :: |
| 280 | state: PoolState |
| 281 | event: Event |
| 282 | -> actor.Next<PoolState> / <io, proc, random> |
| 283 | { |
| 284 | match event |
| 285 | { Ev(Acquire(borrower, reply)) -> |
| 286 | Continue(grant_or_queue state borrower reply) |
| 287 | , Ev(CancelAcquire(borrower)) -> Continue(drop_waiter state borrower) |
| 288 | , Ev(Release(conn)) -> Continue(returned state conn) |
| 289 | , Ev(Discard(conn)) -> Continue(discarded state conn) |
| 290 | , DownEv(down) -> Continue(stopped state down) |
| 291 | , PongEv(conn, res) -> Continue(ponged state conn res) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Answer an acquire: from the idle list (pinging first when the |
| 296 | /// connection sat long enough), from a new connection (while under |
| 297 | /// size), else queue the waiter. |
| 298 | fn grant_or_queue :: |
| 299 | state: PoolState |
| 300 | borrower: Pid |
| 301 | reply: Subject<Result<Subject<client.Msg>, client.Error>> |
| 302 | -> PoolState / <io, proc, random> |
| 303 | { |
| 304 | match state.idle |
| 305 | { [(info, idle_ticks), ..rest] -> |
| 306 | grant_idle PoolState(..state, idle: rest) borrower reply info idle_ticks |
| 307 | , [] -> if live state < state.settings.size |
| 308 | { |
| 309 | match client.connect state.config |
| 310 | { Ok(conn) -> grant state borrower reply ConnInfo(conn, 0) |
| 311 | , Error(e) -> |
| 312 | { |
| 313 | process.send reply Error(e) |
| 314 | state |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | PoolState( ..state |
| 321 | , waiting: [..state.waiting, (borrower, reply)] |
| 322 | , monitored: watch borrower state.monitored |
| 323 | ) |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// Hand an idle connection to a borrower: straight away, or after a |
| 329 | /// ping when it sat past `check_after`. |
| 330 | fn grant_idle :: |
| 331 | state: PoolState |
| 332 | borrower: Pid |
| 333 | reply: Subject<Result<Subject<client.Msg>, client.Error>> |
| 334 | info: ConnInfo |
| 335 | idle_ticks: Int |
| 336 | -> PoolState / <proc> |
| 337 | { |
| 338 | if state.settings.check_after > 0 |
| 339 | && idle_ticks * state.settings.tick >= state.settings.check_after |
| 340 | { |
| 341 | let pong = process.subject () |
| 342 | actor.cast info.conn client.Ping(pong) |
| 343 | PoolState( ..state |
| 344 | , checking: [Checking(borrower, reply, info, pong, 0), ..state.checking] |
| 345 | , monitored: watch borrower state.monitored |
| 346 | ) |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | grant state borrower reply info |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /// Hand `info` to `borrower`: watch the borrower, record the checkout, |
| 355 | /// answer. |
| 356 | fn grant :: |
| 357 | state: PoolState |
| 358 | borrower: Pid |
| 359 | reply: Subject<Result<Subject<client.Msg>, client.Error>> |
| 360 | info: ConnInfo |
| 361 | -> PoolState / <proc> |
| 362 | { |
| 363 | process.send reply Ok(info.conn) |
| 364 | PoolState( ..state |
| 365 | , checked_out: [(borrower, info), ..state.checked_out] |
| 366 | , monitored: watch (process.owner info.conn) (watch borrower state.monitored) |
| 367 | ) |
| 368 | } |
| 369 | |
| 370 | /// A returned connection answers the oldest waiter, or idles — unless |
| 371 | /// it reached `max_lifetime`, in which case it is closed and a waiter |
| 372 | /// gets a fresh one. |
| 373 | fn returned :: |
| 374 | state: PoolState |
| 375 | conn: Subject<client.Msg> |
| 376 | -> PoolState / <io, proc, random> |
| 377 | { |
| 378 | let previous = owner_of state.checked_out conn |
| 379 | let info = info_of state.checked_out conn |
| 380 | let state = |
| 381 | PoolState( ..state |
| 382 | , checked_out: drop_conn state.checked_out conn |
| 383 | , monitored: unwatch previous state.monitored |
| 384 | ) |
| 385 | match info |
| 386 | { Some |
| 387 | ( info |
| 388 | ) if state.settings.max_lifetime > 0 |
| 389 | && info.age * state.settings.tick >= state.settings.max_lifetime -> |
| 390 | { |
| 391 | actor.cast conn client.Close(process.subject ()) |
| 392 | drain |
| 393 | PoolState( ..state |
| 394 | , monitored: unwatch Some(process.owner conn) state.monitored |
| 395 | ) |
| 396 | } |
| 397 | , _ -> match state.waiting |
| 398 | { [(borrower, reply), ..rest] -> match info |
| 399 | { Some(info) -> |
| 400 | grant PoolState(..state, waiting: rest) borrower reply info |
| 401 | , None -> PoolState(..state, waiting: rest) |
| 402 | } |
| 403 | , [] -> match info |
| 404 | { Some(info) -> PoolState(..state, idle: [(info, 0), ..state.idle]) |
| 405 | , None -> state |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /// A connection with an unknown outcome leaves the pool instead of becoming |
| 412 | /// idle. Removing its checkout before closing it also lets a waiter reconnect. |
| 413 | fn discarded :: |
| 414 | state: PoolState |
| 415 | conn: Subject<client.Msg> |
| 416 | -> PoolState / <io, proc, random> |
| 417 | { |
| 418 | let previous = owner_of state.checked_out conn |
| 419 | match info_of state.checked_out conn |
| 420 | { None -> state |
| 421 | , Some(_) -> |
| 422 | { |
| 423 | let state = |
| 424 | PoolState( ..state |
| 425 | , checked_out: drop_conn state.checked_out conn |
| 426 | , monitored: unwatch previous state.monitored |
| 427 | ) |
| 428 | actor.cast conn client.Close(process.subject ()) |
| 429 | drain |
| 430 | PoolState( ..state |
| 431 | , monitored: unwatch Some(process.owner conn) state.monitored |
| 432 | ) |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /// A ping's answer: hand the connection over, or — it failed, and the |
| 438 | /// connection actor is stopping — forget it and find the borrower |
| 439 | /// another one. An answer for a connection no longer being checked (its |
| 440 | /// borrower gave up) returns it to the idle list; an answer for a |
| 441 | /// connection the pool no longer tracks at all closes it. |
| 442 | fn ponged :: |
| 443 | state: PoolState |
| 444 | conn: Subject<client.Msg> |
| 445 | res: Result<(), client.Error> |
| 446 | -> PoolState / <io, proc, random> |
| 447 | { |
| 448 | match find_checking state.checking conn |
| 449 | { Some(entry) -> |
| 450 | { |
| 451 | let Checking(borrower, reply, info, _, _) = entry |
| 452 | let state = |
| 453 | PoolState(..state, checking: drop_checking state.checking conn) |
| 454 | match res |
| 455 | { Ok(()) -> grant state borrower reply info |
| 456 | , Error(_) -> |
| 457 | grant_or_queue |
| 458 | PoolState( ..state |
| 459 | , monitored: unwatch Some(process.owner conn) state.monitored |
| 460 | ) |
| 461 | borrower |
| 462 | reply |
| 463 | } |
| 464 | } |
| 465 | , None -> if is_idle state.idle conn |
| 466 | { |
| 467 | state |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | actor.cast conn client.Close(process.subject ()) |
| 472 | PoolState( ..state |
| 473 | , monitored: unwatch Some(process.owner conn) state.monitored |
| 474 | ) |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /// A monitor report: a stopped connection actor frees its slot; a stopped |
| 480 | /// borrower frees its connection (closed, not reused) or its queue entry. |
| 481 | fn stopped :: state: PoolState down: Down -> PoolState / <io, proc, random> |
| 482 | { |
| 483 | let Down(pid, _) = down |
| 484 | let state = PoolState(..state, monitored: drop_pid state.monitored pid) |
| 485 | match find_conn state.checked_out pid |
| 486 | { Some(entry) -> |
| 487 | { |
| 488 | // The borrower died holding it: its state is unknown, so close it. |
| 489 | let (_, info) = entry |
| 490 | actor.cast info.conn client.Close(process.subject ()) |
| 491 | drain PoolState(..state, checked_out: drop_pid state.checked_out pid) |
| 492 | } |
| 493 | , None -> match find_checking_by_borrower state.checking pid |
| 494 | { Some(entry) -> |
| 495 | { |
| 496 | // The borrower died waiting on a ping: the connection is |
| 497 | // clean (a bare Sync changes nothing), so it idles; its late |
| 498 | // answer is a no-op then. |
| 499 | let Checking(_, _, info, _, _) = entry |
| 500 | PoolState( ..state |
| 501 | , checking: drop_checking state.checking info.conn |
| 502 | , idle: [(info, 0), ..state.idle] |
| 503 | ) |
| 504 | } |
| 505 | , None -> match find_checking_by_owner state.checking pid |
| 506 | { Some(entry) -> |
| 507 | { |
| 508 | // The connection died mid-ping: the borrower gets another. |
| 509 | let Checking(borrower, reply, info, _, _) = entry |
| 510 | grant_or_queue |
| 511 | PoolState(..state, checking: drop_checking state.checking info.conn) |
| 512 | borrower |
| 513 | reply |
| 514 | } |
| 515 | , None -> match find_owner state.idle pid |
| 516 | { Some(_) -> drain PoolState(..state, idle: drop_owner state.idle pid) |
| 517 | , None -> |
| 518 | // A waiter's death only empties its queue entry. |
| 519 | drain PoolState(..state, waiting: drop_pid state.waiting pid) |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /// A waiter gave up: drop its entry, queued or checking (a checking |
| 527 | /// connection is clean; it idles, and its late answer is a no-op). |
| 528 | fn drop_waiter :: state: PoolState borrower: Pid -> PoolState |
| 529 | { |
| 530 | match find_checking_by_borrower state.checking borrower |
| 531 | { Some(entry) -> |
| 532 | { |
| 533 | let Checking(_, _, info, _, _) = entry |
| 534 | PoolState( ..state |
| 535 | , checking: drop_checking state.checking info.conn |
| 536 | , idle: [(info, 0), ..state.idle] |
| 537 | , monitored: unwatch Some(borrower) state.monitored |
| 538 | ) |
| 539 | } |
| 540 | , None -> |
| 541 | PoolState( ..state |
| 542 | , waiting: drop_pid state.waiting borrower |
| 543 | , monitored: unwatch Some(borrower) state.monitored |
| 544 | ) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /// Answer waiting callers while there is something to hand out. |
| 549 | fn drain :: state: PoolState -> PoolState / <io, proc, random> |
| 550 | { |
| 551 | match state.waiting |
| 552 | { [] -> state |
| 553 | , [(borrower, reply), ..rest] -> match state.idle |
| 554 | { [(info, idle_ticks), ..idle] -> |
| 555 | drain |
| 556 | ( grant_idle |
| 557 | PoolState(..state, idle: idle, waiting: rest) |
| 558 | borrower |
| 559 | reply |
| 560 | info |
| 561 | idle_ticks |
| 562 | ) |
| 563 | , [] -> if live state < state.settings.size |
| 564 | { |
| 565 | match client.connect state.config |
| 566 | { Ok(conn) -> |
| 567 | drain |
| 568 | ( grant |
| 569 | PoolState(..state, waiting: rest) |
| 570 | borrower |
| 571 | reply |
| 572 | ConnInfo(conn, 0) |
| 573 | ) |
| 574 | , Error(e) -> |
| 575 | { |
| 576 | process.send reply Error(e) |
| 577 | drain PoolState(..state, waiting: rest) |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | state |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // ---- the tick ------------------------------------------------------------------- |
| 590 | |
| 591 | /// Maintenance, every tick while the pool is quiet: age everything, |
| 592 | /// expire pings that take too long, close connections past |
| 593 | /// `max_lifetime`, close connections idle past `idle_timeout` down to |
| 594 | /// the `min_idle` floor, and warm the floor back up. |
| 595 | fn maintain :: state: PoolState -> PoolState / <io, proc, random> |
| 596 | { |
| 597 | let state = age state |
| 598 | let state = expire_checks state |
| 599 | let state = reap state |
| 600 | replenish state |
| 601 | } |
| 602 | |
| 603 | /// Everything ages a tick. |
| 604 | fn age :: state: PoolState -> PoolState |
| 605 | { |
| 606 | PoolState( ..state |
| 607 | , idle: list.map |
| 608 | state.idle |
| 609 | fn entry |
| 610 | { |
| 611 | let (info, ticks) = entry |
| 612 | (ConnInfo(..info, age: info.age + 1), ticks + 1) |
| 613 | } |
| 614 | , checked_out: list.map |
| 615 | state.checked_out |
| 616 | fn entry |
| 617 | { |
| 618 | let (borrower, info) = entry |
| 619 | (borrower, ConnInfo(..info, age: info.age + 1)) |
| 620 | } |
| 621 | , checking: list.map |
| 622 | state.checking |
| 623 | fn entry |
| 624 | { |
| 625 | let Checking(borrower, reply, info, pong, ticks) = entry |
| 626 | Checking( borrower |
| 627 | , reply |
| 628 | , ConnInfo(..info, age: info.age + 1) |
| 629 | , pong |
| 630 | , ticks + 1 |
| 631 | ) |
| 632 | } |
| 633 | ) |
| 634 | } |
| 635 | |
| 636 | /// A ping is answered within a round trip; two ticks without an answer |
| 637 | /// means the connection is hung: close it and find the borrower another. |
| 638 | fn expire_checks :: state: PoolState -> PoolState / <io, proc, random> |
| 639 | { |
| 640 | match state.checking |
| 641 | { [] -> state |
| 642 | , [entry, ..rest] -> |
| 643 | { |
| 644 | let Checking(borrower, reply, info, _, ticks) = entry |
| 645 | let state = PoolState(..state, checking: rest) |
| 646 | if ticks >= 2 |
| 647 | { |
| 648 | actor.cast info.conn client.Close(process.subject ()) |
| 649 | let state = |
| 650 | PoolState( ..state |
| 651 | , monitored: unwatch Some(process.owner info.conn) state.monitored |
| 652 | ) |
| 653 | expire_checks (grant_or_queue state borrower reply) |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | expire_checks PoolState(..state, checking: [entry, ..state.checking]) |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /// Close idle connections past `max_lifetime` (always) or past |
| 664 | /// `idle_timeout` (down to the `min_idle` floor). |
| 665 | fn reap :: state: PoolState -> PoolState / <proc> |
| 666 | { |
| 667 | let tick = state.settings.tick |
| 668 | let floor = state.settings.min_idle |
| 669 | let count = list.length state.idle |
| 670 | let (closing, keeping) = |
| 671 | partition_indexed |
| 672 | state.idle |
| 673 | [] |
| 674 | [] |
| 675 | fn entry i |
| 676 | { |
| 677 | let (info, idle_ticks) = entry |
| 678 | let too_old = |
| 679 | state.settings.max_lifetime > 0 |
| 680 | && info.age * tick >= state.settings.max_lifetime |
| 681 | let too_idle = |
| 682 | state.settings.idle_timeout > 0 |
| 683 | && idle_ticks * tick >= state.settings.idle_timeout |
| 684 | && count - i > floor |
| 685 | too_old || too_idle |
| 686 | } |
| 687 | close_all closing |
| 688 | match closing |
| 689 | { [] -> state |
| 690 | , _ -> |
| 691 | PoolState( ..state |
| 692 | , idle: keeping |
| 693 | , monitored: list.fold |
| 694 | closing |
| 695 | state.monitored |
| 696 | fn m entry |
| 697 | { |
| 698 | let (info, _) = entry |
| 699 | unwatch Some(process.owner info.conn) m |
| 700 | } |
| 701 | ) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /// Warm the `min_idle` floor with fresh connections. |
| 706 | fn replenish :: state: PoolState -> PoolState / <io, proc, random> |
| 707 | { |
| 708 | let missing = state.settings.min_idle - list.length state.idle |
| 709 | if missing <= 0 || live state >= state.settings.size |
| 710 | { |
| 711 | state |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | match client.connect state.config |
| 716 | { Ok(conn) -> |
| 717 | replenish |
| 718 | PoolState(..state, idle: [(ConnInfo(conn, 0), 0), ..state.idle]) |
| 719 | // The database is down; the next tick tries again. |
| 720 | , Error(_) -> state |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /// Close every connection in the list (best effort, fire and forget). |
| 726 | fn close_all :: entries: List<(ConnInfo, Int)> -> () / <proc> |
| 727 | { |
| 728 | match entries |
| 729 | { [] -> () |
| 730 | , [(info, _), ..rest] -> |
| 731 | { |
| 732 | actor.cast info.conn client.Close(process.subject ()) |
| 733 | close_all rest |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // ---- small helpers -------------------------------------------------------------- |
| 739 | |
| 740 | /// The connections out, idle, or being checked (those are the live |
| 741 | /// ones: a stopped connection actor's subject is removed on its Down). |
| 742 | fn live :: state: PoolState -> Int |
| 743 | { |
| 744 | list.length state.idle + list.length state.checked_out |
| 745 | + list.length state.checking |
| 746 | } |
| 747 | |
| 748 | /// Watch `pid` (once). |
| 749 | fn watch :: |
| 750 | pid: Pid |
| 751 | monitored: List<(Pid, Subject<Down>)> |
| 752 | -> List<(Pid, Subject<Down>)> / <proc> |
| 753 | { |
| 754 | if has_pid monitored pid |
| 755 | { |
| 756 | monitored |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | [(pid, process.monitor pid), ..monitored] |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /// Stop watching `pid` (its subject stays in the mailbox; read no more). |
| 765 | fn unwatch :: |
| 766 | pid: Option<Pid> |
| 767 | monitored: List<(Pid, Subject<Down>)> |
| 768 | -> List<(Pid, Subject<Down>)> |
| 769 | { |
| 770 | match pid { Some(pid) -> drop_pid monitored pid, None -> monitored } |
| 771 | } |
| 772 | |
| 773 | /// The entry whose borrower is `pid`. |
| 774 | fn find_conn :: list_: List<(Pid, ConnInfo)> pid: Pid -> Option<(Pid, ConnInfo)> |
| 775 | { |
| 776 | match list_ |
| 777 | { [] -> None |
| 778 | , [entry, ..rest] -> if entry.0 == pid |
| 779 | { |
| 780 | Some(entry) |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | find_conn rest pid |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /// The pid a connection is checked out to, by its owner pid. |
| 790 | fn owner_of :: |
| 791 | entries: List<(Pid, ConnInfo)> |
| 792 | conn: Subject<client.Msg> |
| 793 | -> Option<Pid> |
| 794 | { |
| 795 | match entries |
| 796 | { [] -> None |
| 797 | , [ (pid, info) |
| 798 | , ..rest |
| 799 | ] -> if process.owner info.conn == process.owner conn |
| 800 | { |
| 801 | Some(pid) |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | owner_of rest conn |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /// The connection info checked out under `conn`'s owner pid. |
| 811 | fn info_of :: |
| 812 | entries: List<(Pid, ConnInfo)> |
| 813 | conn: Subject<client.Msg> |
| 814 | -> Option<ConnInfo> |
| 815 | { |
| 816 | match entries |
| 817 | { [] -> None |
| 818 | , [ (_, info) |
| 819 | , ..rest |
| 820 | ] -> if process.owner info.conn == process.owner conn |
| 821 | { |
| 822 | Some(info) |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | info_of rest conn |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /// Drop the pair whose connection is `conn` (by its owner's pid). |
| 832 | fn drop_conn :: |
| 833 | entries: List<(Pid, ConnInfo)> |
| 834 | conn: Subject<client.Msg> |
| 835 | -> List<(Pid, ConnInfo)> |
| 836 | { |
| 837 | match entries |
| 838 | { [] -> [] |
| 839 | , [ (p, info) |
| 840 | , ..rest |
| 841 | ] -> if process.owner info.conn == process.owner conn |
| 842 | { |
| 843 | drop_conn rest conn |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | [(p, info), ..drop_conn rest conn] |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /// The checking entry for `conn`. |
| 853 | fn find_checking :: |
| 854 | entries: List<Checking> |
| 855 | conn: Subject<client.Msg> |
| 856 | -> Option<Checking> |
| 857 | { |
| 858 | match entries |
| 859 | { [] -> None |
| 860 | , [ entry |
| 861 | , ..rest |
| 862 | ] -> if process.owner entry.info.conn == process.owner conn |
| 863 | { |
| 864 | Some(entry) |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | find_checking rest conn |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /// The checking entry whose borrower is `pid`. |
| 874 | fn find_checking_by_borrower :: |
| 875 | entries: List<Checking> |
| 876 | pid: Pid |
| 877 | -> Option<Checking> |
| 878 | { |
| 879 | match entries |
| 880 | { [] -> None |
| 881 | , [entry, ..rest] -> if entry.borrower == pid |
| 882 | { |
| 883 | Some(entry) |
| 884 | } |
| 885 | else |
| 886 | { |
| 887 | find_checking_by_borrower rest pid |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /// The checking entry whose connection's owner is `pid`. |
| 893 | fn find_checking_by_owner :: |
| 894 | entries: List<Checking> |
| 895 | pid: Pid |
| 896 | -> Option<Checking> |
| 897 | { |
| 898 | match entries |
| 899 | { [] -> None |
| 900 | , [entry, ..rest] -> if process.owner entry.info.conn == pid |
| 901 | { |
| 902 | Some(entry) |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | find_checking_by_owner rest pid |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /// Drop the checking entry for `conn`. |
| 912 | fn drop_checking :: |
| 913 | entries: List<Checking> |
| 914 | conn: Subject<client.Msg> |
| 915 | -> List<Checking> |
| 916 | { |
| 917 | match entries |
| 918 | { [] -> [] |
| 919 | , [ entry |
| 920 | , ..rest |
| 921 | ] -> if process.owner entry.info.conn == process.owner conn |
| 922 | { |
| 923 | drop_checking rest conn |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | [entry, ..drop_checking rest conn] |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /// Is `conn` in the idle list? |
| 933 | fn is_idle :: entries: List<(ConnInfo, Int)> conn: Subject<client.Msg> -> Bool |
| 934 | { |
| 935 | match entries |
| 936 | { [] -> False |
| 937 | , [(info, _), ..rest] -> |
| 938 | process.owner info.conn == process.owner conn || is_idle rest conn |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /// The idle entry whose connection's owner is `pid`. |
| 943 | fn find_owner :: |
| 944 | list_: List<(ConnInfo, Int)> |
| 945 | pid: Pid |
| 946 | -> Option<(ConnInfo, Int)> |
| 947 | { |
| 948 | match list_ |
| 949 | { [] -> None |
| 950 | , [entry, ..rest] -> if process.owner entry.0.conn == pid |
| 951 | { |
| 952 | Some(entry) |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | find_owner rest pid |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | fn drop_owner :: list_: List<(ConnInfo, Int)> pid: Pid -> List<(ConnInfo, Int)> |
| 962 | { |
| 963 | match list_ |
| 964 | { [] -> [] |
| 965 | , [ entry |
| 966 | , ..rest |
| 967 | ] -> if process.owner entry.0.conn == pid |
| 968 | { |
| 969 | drop_owner rest pid |
| 970 | } |
| 971 | else |
| 972 | { |
| 973 | [entry, ..drop_owner rest pid] |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | fn drop_pid :: entries: List<(Pid, a)> pid: Pid -> List<(Pid, a)> |
| 979 | { |
| 980 | match entries |
| 981 | { [] -> [] |
| 982 | , [(p, v), ..rest] -> if p == pid |
| 983 | { |
| 984 | drop_pid rest pid |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | [(p, v), ..drop_pid rest pid] |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | fn has_pid :: entries: List<(Pid, a)> pid: Pid -> Bool |
| 994 | { |
| 995 | match entries |
| 996 | { [] -> False, [(p, _), ..rest] -> p == pid || has_pid rest pid } |
| 997 | } |
| 998 | |
| 999 | /// `list.partition`, with each element's index: the idle-timeout floor |
| 1000 | /// counts from the end of the list. `(closing, keeping)`. |
| 1001 | fn partition_indexed :: |
| 1002 | entries: List<a> |
| 1003 | yes: List<a> |
| 1004 | no: List<a> |
| 1005 | close: fn(a, Int) -> Bool |
| 1006 | -> (List<a>, List<a>) |
| 1007 | { |
| 1008 | partition_from entries yes no 0 close |
| 1009 | } |
| 1010 | |
| 1011 | fn partition_from :: |
| 1012 | entries: List<a> |
| 1013 | yes: List<a> |
| 1014 | no: List<a> |
| 1015 | i: Int |
| 1016 | close: fn(a, Int) -> Bool |
| 1017 | -> (List<a>, List<a>) |
| 1018 | { |
| 1019 | match entries |
| 1020 | { [] -> (list.reverse yes, list.reverse no) |
| 1021 | , [entry, ..rest] -> if close entry i |
| 1022 | { |
| 1023 | partition_from rest [entry, ..yes] no (i + 1) close |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | partition_from rest yes [entry, ..no] (i + 1) close |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /// Watch every connection's owner. |
| 1033 | fn monitor_all :: |
| 1034 | conns: List<Subject<client.Msg>> |
| 1035 | acc: List<(Pid, Subject<Down>)> |
| 1036 | -> List<(Pid, Subject<Down>)> / <proc> |
| 1037 | { |
| 1038 | match conns |
| 1039 | { [] -> acc |
| 1040 | , [conn, ..rest] -> monitor_all rest (watch (process.owner conn) acc) |
| 1041 | } |
| 1042 | } |