| @@ -6,14 +6,23 @@ |
| 6 | 6 | /// Waiting callers queue; the acquire timeout is the caller's |
| 7 | 7 | /// (`acquire`'s), and a `CancelAcquire` best-effort message drops a |
| 8 | 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. |
| 9 | 20 | |
| 10 | 21 | import std/actor.{Continue, Stop} |
| 11 | 22 | import std/list |
| 12 | | −import std/option |
| 13 | 23 | import std/process.{type Down, type Pid, type Subject, type proc, Down} |
| 14 | 24 | import std/random.{type random} |
| 15 | 25 | import std/result |
| 16 | | −import std/string |
| 17 | 26 | |
| 18 | 27 | import client |
| 19 | 28 | import config.{type Config} |
| @@ -23,6 +32,35 @@ pub opaque type Pool { |
| 23 | 32 | Pool(Subject<Msg>) |
| 24 | 33 | } |
| 25 | 34 | |
| 35 | +/// The pool's knobs. `pool.defaults(size)` is a good start; adjust with |
| 36 | +/// a record update. A `0` duration disables its check. |
| 37 | +pub type PoolSettings { |
| 38 | + PoolSettings( |
| 39 | + /// The most connections the pool holds, idle or out. |
| 40 | + size: Int, |
| 41 | + /// The floor of idle connections the tick keeps warm (clamped to |
| 42 | + /// `size`). |
| 43 | + min_idle: Int, |
| 44 | + /// Close a connection idle this long, down to `min_idle` (0: never). |
| 45 | + idle_timeout: Int, |
| 46 | + /// Close a connection this old when it comes back, instead of |
| 47 | + /// reusing it (0: never). |
| 48 | + max_lifetime: Int, |
| 49 | + /// Ping a connection that sat idle this long before handing it out |
| 50 | + /// (0: never). On by default: a stale connection fails the ping and |
| 51 | + /// is replaced, instead of failing a borrower's first query. |
| 52 | + check_after: Int, |
| 53 | + /// The maintenance period, in milliseconds. |
| 54 | + tick: Int, |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +/// `size` connections, no reaping, a liveness check past 30 s idle, a |
| 59 | +/// one-second tick. |
| 60 | +pub fn defaults(size: Int) -> PoolSettings { |
| 61 | + PoolSettings(size: size, min_idle: 0, idle_timeout: 0, max_lifetime: 0, check_after: 30000, tick: 1000) |
| 62 | +} |
| 63 | + |
| 26 | 64 | /// The pool's messages. |
| 27 | 65 | pub type Msg { |
| 28 | 66 | /// Check a connection out; the answer is the connection's subject (or |
| @@ -34,22 +72,43 @@ pub type Msg { |
| 34 | 72 | Release(conn: Subject<client.Msg>) |
| 35 | 73 | } |
| 36 | 74 | |
| 37 | | −/// What the pool's mailbox carries: its messages, and monitor reports. |
| 75 | +/// What the pool's mailbox carries: its messages, monitor reports, and |
| 76 | +/// ping answers. |
| 38 | 77 | type Event { |
| 39 | 78 | Ev(Msg) |
| 40 | 79 | DownEv(Down) |
| 80 | + PongEv(conn: Subject<client.Msg>, result: Result<(), client.Error>) |
| 81 | +} |
| 82 | + |
| 83 | +/// A connection the pool tracks: its subject and its age in ticks. |
| 84 | +type ConnInfo { |
| 85 | + ConnInfo(conn: Subject<client.Msg>, age: Int) |
| 86 | +} |
| 87 | + |
| 88 | +/// A handout waiting on a ping: the borrower, its answer subject, the |
| 89 | +/// connection, the ping's reply subject, and ticks since the ping. |
| 90 | +type Checking { |
| 91 | + Checking( |
| 92 | + borrower: Pid, |
| 93 | + reply: Subject<Result<Subject<client.Msg>, client.Error>>, |
| 94 | + info: ConnInfo, |
| 95 | + pong: Subject<Result<(), client.Error>>, |
| 96 | + ticks: Int, |
| 97 | + ) |
| 41 | 98 | } |
| 42 | 99 | |
| 43 | 100 | type PoolState { |
| 44 | 101 | PoolState( |
| 45 | 102 | config: Config, |
| 46 | | − size: Int, |
| 103 | + settings: PoolSettings, |
| 47 | 104 | /// The pool's own subject. |
| 48 | 105 | inbox: Subject<Msg>, |
| 49 | | − /// Connections ready to hand out. |
| 50 | | − idle: List<Subject<client.Msg>>, |
| 106 | + /// Connections ready to hand out, with their idle time in ticks. |
| 107 | + idle: List<(ConnInfo, Int)>, |
| 51 | 108 | /// (borrower, connection) pairs. |
| 52 | | − checked_out: List<(Pid, Subject<client.Msg>)>, |
| 109 | + checked_out: List<(Pid, ConnInfo)>, |
| 110 | + /// Handouts waiting on a ping. |
| 111 | + checking: List<Checking>, |
| 53 | 112 | /// Waiting borrowers, in order: (pid, its reply subject). |
| 54 | 113 | waiting: List<(Pid, Subject<Result<Subject<client.Msg>, client.Error>>)>, |
| 55 | 114 | /// Everyone watched: connection actors and borrowers. |
| @@ -57,20 +116,50 @@ type PoolState { |
| 57 | 116 | ) |
| 58 | 117 | } |
| 59 | 118 | |
| 60 | | −/// A pool of `size` connections, made now. The pool replaces dead |
| 61 | | −/// connections as they're needed. |
| 119 | +/// A pool of `size` connections, made now, with the default settings. |
| 120 | +/// The pool replaces dead connections as they're needed. |
| 62 | 121 | pub fn start(config: Config, size: Int) -> Result<Pool, client.Error> / <io, proc, random> { |
| 63 | | − use conns <- result.try(connect_all(config, size, [])) |
| 122 | + start_with(config, defaults(size)) |
| 123 | +} |
| 124 | + |
| 125 | +/// A pool with explicit settings, its connections made now. |
| 126 | +pub fn start_with(config: Config, settings: PoolSettings) -> Result<Pool, client.Error> / <io, proc, random> { |
| 127 | + let settings = sane(settings) |
| 128 | + use conns <- result.try(connect_all(config, settings.size, [])) |
| 64 | 129 | let ready = process.subject() |
| 65 | 130 | { |
| 66 | 131 | use <- process.spawn |
| 67 | 132 | let inbox = process.subject() |
| 68 | 133 | process.send(ready, inbox) |
| 69 | | − serve(PoolState(config, size, inbox, conns, [], [], monitor_all(conns, []))) |
| 134 | + serve(PoolState( |
| 135 | + config, |
| 136 | + settings, |
| 137 | + inbox, |
| 138 | + list.map(conns, fn(c) { (ConnInfo(c, 0), 0) }), |
| 139 | + [], |
| 140 | + [], |
| 141 | + [], |
| 142 | + monitor_all(conns, []), |
| 143 | + )) |
| 70 | 144 | } |
| 71 | 145 | Ok(Pool(process.receive_forever(ready))) |
| 72 | 146 | } |
| 73 | 147 | |
| 148 | +/// Settings with the clamps applied: a positive tick, `min_idle` within |
| 149 | +/// `size`. |
| 150 | +fn sane(settings: PoolSettings) -> PoolSettings { |
| 151 | + let tick = if settings.tick <= 0 { 1000 } else { settings.tick } |
| 152 | + let min_idle = |
| 153 | + if settings.min_idle < 0 { |
| 154 | + 0 |
| 155 | + } else if settings.min_idle > settings.size { |
| 156 | + settings.size |
| 157 | + } else { |
| 158 | + settings.min_idle |
| 159 | + } |
| 160 | + PoolSettings(..settings, tick: tick, min_idle: min_idle) |
| 161 | +} |
| 162 | + |
| 74 | 163 | /// `size` connections, or the first failure. |
| 75 | 164 | fn connect_all( |
| 76 | 165 | config: Config, |
| @@ -118,17 +207,26 @@ pub fn release(pool: Pool, conn: Subject<client.Msg>) -> () / <proc> { |
| 118 | 207 | // ---- the actor ------------------------------------------------------------------ |
| 119 | 208 | |
| 120 | 209 | fn serve(state: PoolState) -> () / <proc, io, random> { |
| 121 | | − let selector = selector_of(state.inbox, state.monitored) |
| 122 | | − match handle_event(state, process.select_forever(selector)) { |
| 123 | | − Continue(next) -> serve(next), |
| 124 | | − Stop -> (), |
| 210 | + match process.select(selector_of(state), state.settings.tick) { |
| 211 | + Some(event) -> |
| 212 | + match handle_event(state, event) { |
| 213 | + Continue(next) -> serve(next), |
| 214 | + Stop -> (), |
| 215 | + }, |
| 216 | + // A quiet pool: the tick fires, and maintenance runs. |
| 217 | + None -> serve(maintain(state)), |
| 125 | 218 | } |
| 126 | 219 | } |
| 127 | 220 | |
| 128 | | −/// A selector over the pool's messages and every monitor report. |
| 129 | | −fn selector_of(inbox: Subject<Msg>, monitored: List<(Pid, Subject<Down>)>) -> process.Selector<Event> { |
| 130 | | − let base = process.new_selector() |> process.selecting(inbox, Ev) |
| 131 | | − list.fold(monitored, base, fn(sel, entry) { process.selecting(sel, entry.1, DownEv) }) |
| 221 | +/// A selector over the pool's messages, every monitor report, and every |
| 222 | +/// outstanding ping. |
| 223 | +fn selector_of(state: PoolState) -> process.Selector<Event> { |
| 224 | + let base = process.new_selector() |> process.selecting(state.inbox, Ev) |
| 225 | + let with_downs = list.fold(state.monitored, base, fn(sel, entry) { process.selecting(sel, entry.1, DownEv) }) |
| 226 | + list.fold(state.checking, with_downs, fn(sel, entry) { |
| 227 | + let Checking(_, _, info, pong, _) = entry |
| 228 | + process.selecting(sel, pong, fn(result) { PongEv(info.conn, result) }) |
| 229 | + }) |
| 132 | 230 | } |
| 133 | 231 | |
| 134 | 232 | fn handle_event(state: PoolState, event: Event) -> actor.Next<PoolState> / <io, proc, random> { |
| @@ -137,22 +235,24 @@ fn handle_event(state: PoolState, event: Event) -> actor.Next<PoolState> / <io, |
| 137 | 235 | Ev(CancelAcquire(borrower)) -> Continue(drop_waiter(state, borrower)), |
| 138 | 236 | Ev(Release(conn)) -> Continue(returned(state, conn)), |
| 139 | 237 | DownEv(down) -> Continue(stopped(state, down)), |
| 238 | + PongEv(conn, result) -> Continue(ponged(state, conn, result)), |
| 140 | 239 | } |
| 141 | 240 | } |
| 142 | 241 | |
| 143 | | −/// Answer an acquire: from the idle list, from a new connection (while |
| 144 | | −/// under size), else queue the waiter. |
| 242 | +/// Answer an acquire: from the idle list (pinging first when the |
| 243 | +/// connection sat long enough), from a new connection (while under |
| 244 | +/// size), else queue the waiter. |
| 145 | 245 | fn grant_or_queue( |
| 146 | 246 | state: PoolState, |
| 147 | 247 | borrower: Pid, |
| 148 | 248 | reply: Subject<Result<Subject<client.Msg>, client.Error>>, |
| 149 | 249 | ) -> PoolState / <io, proc, random> { |
| 150 | 250 | match state.idle { |
| 151 | | − [conn, ..rest] -> grant(PoolState(..state, idle: rest), borrower, reply, conn), |
| 251 | + [(info, idle_ticks), ..rest] -> grant_idle(PoolState(..state, idle: rest), borrower, reply, info, idle_ticks), |
| 152 | 252 | [] -> |
| 153 | | − if live(state) < state.size { |
| 253 | + if live(state) < state.settings.size { |
| 154 | 254 | match client.connect(state.config) { |
| 155 | | − Ok(conn) -> grant(state, borrower, reply, conn), |
| 255 | + Ok(conn) -> grant(state, borrower, reply, ConnInfo(conn, 0)), |
| 156 | 256 | Error(e) -> { |
| 157 | 257 | process.send(reply, Error(e)) |
| 158 | 258 | state |
| @@ -167,32 +267,105 @@ fn grant_or_queue( |
| 167 | 267 | } |
| 168 | 268 | } |
| 169 | 269 | |
| 170 | | −/// Hand `conn` to `borrower`: watch the borrower, record the checkout, |
| 270 | +/// Hand an idle connection to a borrower: straight away, or after a |
| 271 | +/// ping when it sat past `check_after`. |
| 272 | +fn grant_idle( |
| 273 | + state: PoolState, |
| 274 | + borrower: Pid, |
| 275 | + reply: Subject<Result<Subject<client.Msg>, client.Error>>, |
| 276 | + info: ConnInfo, |
| 277 | + idle_ticks: Int, |
| 278 | +) -> PoolState / <proc> { |
| 279 | + if state.settings.check_after > 0 && idle_ticks * state.settings.tick >= state.settings.check_after { |
| 280 | + let pong = process.subject() |
| 281 | + actor.cast(info.conn, client.Ping(pong)) |
| 282 | + PoolState(..state, |
| 283 | + checking: [Checking(borrower, reply, info, pong, 0), ..state.checking], |
| 284 | + monitored: watch(borrower, state.monitored), |
| 285 | + ) |
| 286 | + } else { |
| 287 | + grant(state, borrower, reply, info) |
| 288 | + } |
| 289 | +} |
| 290 | + |
| 291 | +/// Hand `info` to `borrower`: watch the borrower, record the checkout, |
| 171 | 292 | /// answer. |
| 172 | 293 | fn grant( |
| 173 | 294 | state: PoolState, |
| 174 | 295 | borrower: Pid, |
| 175 | 296 | reply: Subject<Result<Subject<client.Msg>, client.Error>>, |
| 176 | | − conn: Subject<client.Msg>, |
| 297 | + info: ConnInfo, |
| 177 | 298 | ) -> PoolState / <proc> { |
| 178 | | − process.send(reply, Ok(conn)) |
| 299 | + process.send(reply, Ok(info.conn)) |
| 179 | 300 | PoolState(..state, |
| 180 | | − checked_out: [(borrower, conn), ..state.checked_out], |
| 181 | | − monitored: watch(conn.owner, watch(borrower, state.monitored)), |
| 301 | + checked_out: [(borrower, info), ..state.checked_out], |
| 302 | + monitored: watch(info.conn.owner, watch(borrower, state.monitored)), |
| 182 | 303 | ) |
| 183 | 304 | } |
| 184 | 305 | |
| 185 | | −/// A returned connection answers the oldest waiter, or idles. |
| 186 | | −fn returned(state: PoolState, conn: Subject<client.Msg>) -> PoolState / <proc> { |
| 306 | +/// A returned connection answers the oldest waiter, or idles — unless |
| 307 | +/// it reached `max_lifetime`, in which case it is closed and a waiter |
| 308 | +/// gets a fresh one. |
| 309 | +fn returned(state: PoolState, conn: Subject<client.Msg>) -> PoolState / <io, proc, random> { |
| 187 | 310 | let previous = owner_of(state.checked_out, conn) |
| 311 | + let info = info_of(state.checked_out, conn) |
| 188 | 312 | let state = PoolState(..state, |
| 189 | 313 | checked_out: drop_conn(state.checked_out, conn), |
| 190 | 314 | monitored: unwatch(previous, state.monitored), |
| 191 | 315 | ) |
| 192 | | − match state.waiting { |
| 193 | | − [(borrower, reply), ..rest] -> |
| 194 | | − grant(PoolState(..state, waiting: rest), borrower, reply, conn), |
| 195 | | − [] -> PoolState(..state, idle: [conn, ..state.idle]), |
| 316 | + match info { |
| 317 | + Some(info) |
| 318 | + if state.settings.max_lifetime > 0 && info.age * state.settings.tick >= state.settings.max_lifetime -> { |
| 319 | + actor.cast(conn, client.Close(process.subject())) |
| 320 | + drain(PoolState(..state, monitored: unwatch(Some(conn.owner), state.monitored))) |
| 321 | + }, |
| 322 | + _ -> |
| 323 | + match state.waiting { |
| 324 | + [(borrower, reply), ..rest] -> |
| 325 | + match info { |
| 326 | + Some(info) -> grant(PoolState(..state, waiting: rest), borrower, reply, info), |
| 327 | + None -> PoolState(..state, waiting: rest), |
| 328 | + }, |
| 329 | + [] -> |
| 330 | + match info { |
| 331 | + Some(info) -> PoolState(..state, idle: [(info, 0), ..state.idle]), |
| 332 | + None -> state, |
| 333 | + }, |
| 334 | + }, |
| 335 | + } |
| 336 | +} |
| 337 | + |
| 338 | +/// A ping's answer: hand the connection over, or — it failed, and the |
| 339 | +/// connection actor is stopping — forget it and find the borrower |
| 340 | +/// another one. An answer for a connection no longer being checked (its |
| 341 | +/// borrower gave up) returns it to the idle list; an answer for a |
| 342 | +/// connection the pool no longer tracks at all closes it. |
| 343 | +fn ponged( |
| 344 | + state: PoolState, |
| 345 | + conn: Subject<client.Msg>, |
| 346 | + result: Result<(), client.Error>, |
| 347 | +) -> PoolState / <io, proc, random> { |
| 348 | + match find_checking(state.checking, conn) { |
| 349 | + Some(entry) -> { |
| 350 | + let Checking(borrower, reply, info, _, _) = entry |
| 351 | + let state = PoolState(..state, checking: drop_checking(state.checking, conn)) |
| 352 | + match result { |
| 353 | + Ok(()) -> grant(state, borrower, reply, info), |
| 354 | + Error(_) -> |
| 355 | + grant_or_queue( |
| 356 | + PoolState(..state, monitored: unwatch(Some(conn.owner), state.monitored)), |
| 357 | + borrower, |
| 358 | + reply, |
| 359 | + ), |
| 360 | + } |
| 361 | + }, |
| 362 | + None -> |
| 363 | + if is_idle(state.idle, conn) { |
| 364 | + state |
| 365 | + } else { |
| 366 | + actor.cast(conn, client.Close(process.subject())) |
| 367 | + PoolState(..state, monitored: unwatch(Some(conn.owner), state.monitored)) |
| 368 | + }, |
| 196 | 369 | } |
| 197 | 370 | } |
| 198 | 371 | |
| @@ -202,27 +375,65 @@ fn stopped(state: PoolState, down: Down) -> PoolState / <io, proc, random> { |
| 202 | 375 | let Down(pid, _) = down |
| 203 | 376 | let state = PoolState(..state, monitored: drop_pid(state.monitored, pid)) |
| 204 | 377 | match find_conn(state.checked_out, pid) { |
| 205 | | − Some(conn) -> { |
| 378 | + Some(entry) -> { |
| 206 | 379 | // The borrower died holding it: its state is unknown, so close it. |
| 207 | | − actor.cast(conn, client.Close(process.subject())) |
| 380 | + let (_, info) = entry |
| 381 | + actor.cast(info.conn, client.Close(process.subject())) |
| 208 | 382 | drain(PoolState(..state, checked_out: drop_pid(state.checked_out, pid))) |
| 209 | 383 | }, |
| 210 | 384 | None -> |
| 211 | | − match find_owner(state.idle, pid) { |
| 212 | | − Some(_) -> drain(PoolState(..state, idle: drop_owner(state.idle, pid))), |
| 385 | + match find_checking_by_borrower(state.checking, pid) { |
| 386 | + Some(entry) -> { |
| 387 | + // The borrower died waiting on a ping: the connection is |
| 388 | + // clean (a bare Sync changes nothing), so it idles; its late |
| 389 | + // answer is a no-op then. |
| 390 | + let Checking(_, _, info, _, _) = entry |
| 391 | + PoolState(..state, |
| 392 | + checking: drop_checking(state.checking, info.conn), |
| 393 | + idle: [(info, 0), ..state.idle], |
| 394 | + ) |
| 395 | + }, |
| 213 | 396 | None -> |
| 214 | | − // A waiter's death only empties its queue entry. |
| 215 | | − drain(PoolState(..state, waiting: drop_pid(state.waiting, pid))), |
| 397 | + match find_checking_by_owner(state.checking, pid) { |
| 398 | + Some(entry) -> { |
| 399 | + // The connection died mid-ping: the borrower gets another. |
| 400 | + let Checking(borrower, reply, info, _, _) = entry |
| 401 | + grant_or_queue( |
| 402 | + PoolState(..state, checking: drop_checking(state.checking, info.conn)), |
| 403 | + borrower, |
| 404 | + reply, |
| 405 | + ) |
| 406 | + }, |
| 407 | + None -> |
| 408 | + match find_owner(state.idle, pid) { |
| 409 | + Some(_) -> drain(PoolState(..state, idle: drop_owner(state.idle, pid))), |
| 410 | + None -> |
| 411 | + // A waiter's death only empties its queue entry. |
| 412 | + drain(PoolState(..state, waiting: drop_pid(state.waiting, pid))), |
| 413 | + }, |
| 414 | + }, |
| 216 | 415 | }, |
| 217 | 416 | } |
| 218 | 417 | } |
| 219 | 418 | |
| 220 | | −/// A waiter gave up: drop its entry. |
| 419 | +/// A waiter gave up: drop its entry, queued or checking (a checking |
| 420 | +/// connection is clean; it idles, and its late answer is a no-op). |
| 221 | 421 | fn drop_waiter(state: PoolState, borrower: Pid) -> PoolState { |
| 222 | | − PoolState(..state, |
| 223 | | − waiting: drop_pid(state.waiting, borrower), |
| 224 | | − monitored: unwatch(Some(borrower), state.monitored), |
| 225 | | − ) |
| 422 | + match find_checking_by_borrower(state.checking, borrower) { |
| 423 | + Some(entry) -> { |
| 424 | + let Checking(_, _, info, _, _) = entry |
| 425 | + PoolState(..state, |
| 426 | + checking: drop_checking(state.checking, info.conn), |
| 427 | + idle: [(info, 0), ..state.idle], |
| 428 | + monitored: unwatch(Some(borrower), state.monitored), |
| 429 | + ) |
| 430 | + }, |
| 431 | + None -> |
| 432 | + PoolState(..state, |
| 433 | + waiting: drop_pid(state.waiting, borrower), |
| 434 | + monitored: unwatch(Some(borrower), state.monitored), |
| 435 | + ), |
| 436 | + } |
| 226 | 437 | } |
| 227 | 438 | |
| 228 | 439 | /// Answer waiting callers while there is something to hand out. |
| @@ -231,13 +442,13 @@ fn drain(state: PoolState) -> PoolState / <io, proc, random> { |
| 231 | 442 | [] -> state, |
| 232 | 443 | [(borrower, reply), ..rest] -> |
| 233 | 444 | match state.idle { |
| 234 | | − [conn, ..idle] -> |
| 235 | | − drain(grant(PoolState(..state, idle: idle, waiting: rest), borrower, reply, conn)), |
| 445 | + [(info, idle_ticks), ..idle] -> |
| 446 | + drain(grant_idle(PoolState(..state, idle: idle, waiting: rest), borrower, reply, info, idle_ticks)), |
| 236 | 447 | [] -> |
| 237 | | − if live(state) < state.size { |
| 448 | + if live(state) < state.settings.size { |
| 238 | 449 | match client.connect(state.config) { |
| 239 | 450 | Ok(conn) -> |
| 240 | | − drain(grant(PoolState(..state, waiting: rest), borrower, reply, conn)), |
| 451 | + drain(grant(PoolState(..state, waiting: rest), borrower, reply, ConnInfo(conn, 0))), |
| 241 | 452 | Error(e) -> { |
| 242 | 453 | process.send(reply, Error(e)) |
| 243 | 454 | drain(PoolState(..state, waiting: rest)) |
| @@ -250,12 +461,115 @@ fn drain(state: PoolState) -> PoolState / <io, proc, random> { |
| 250 | 461 | } |
| 251 | 462 | } |
| 252 | 463 | |
| 464 | +// ---- the tick ------------------------------------------------------------------- |
| 465 | + |
| 466 | +/// Maintenance, every tick while the pool is quiet: age everything, |
| 467 | +/// expire pings that take too long, close connections past |
| 468 | +/// `max_lifetime`, close connections idle past `idle_timeout` down to |
| 469 | +/// the `min_idle` floor, and warm the floor back up. |
| 470 | +fn maintain(state: PoolState) -> PoolState / <io, proc, random> { |
| 471 | + let state = age(state) |
| 472 | + let state = expire_checks(state) |
| 473 | + let state = reap(state) |
| 474 | + replenish(state) |
| 475 | +} |
| 476 | + |
| 477 | +/// Everything ages a tick. |
| 478 | +fn age(state: PoolState) -> PoolState { |
| 479 | + PoolState(..state, |
| 480 | + idle: list.map(state.idle, fn(entry) { |
| 481 | + let (info, ticks) = entry |
| 482 | + (ConnInfo(..info, age: info.age + 1), ticks + 1) |
| 483 | + }), |
| 484 | + checked_out: list.map(state.checked_out, fn(entry) { |
| 485 | + let (borrower, info) = entry |
| 486 | + (borrower, ConnInfo(..info, age: info.age + 1)) |
| 487 | + }), |
| 488 | + checking: list.map(state.checking, fn(entry) { |
| 489 | + let Checking(borrower, reply, info, pong, ticks) = entry |
| 490 | + Checking(borrower, reply, ConnInfo(..info, age: info.age + 1), pong, ticks + 1) |
| 491 | + }), |
| 492 | + ) |
| 493 | +} |
| 494 | + |
| 495 | +/// A ping is answered within a round trip; two ticks without an answer |
| 496 | +/// means the connection is hung: close it and find the borrower another. |
| 497 | +fn expire_checks(state: PoolState) -> PoolState / <io, proc, random> { |
| 498 | + match state.checking { |
| 499 | + [] -> state, |
| 500 | + [entry, ..rest] -> { |
| 501 | + let Checking(borrower, reply, info, _, ticks) = entry |
| 502 | + let state = PoolState(..state, checking: rest) |
| 503 | + if ticks >= 2 { |
| 504 | + actor.cast(info.conn, client.Close(process.subject())) |
| 505 | + let state = PoolState(..state, monitored: unwatch(Some(info.conn.owner), state.monitored)) |
| 506 | + expire_checks(grant_or_queue(state, borrower, reply)) |
| 507 | + } else { |
| 508 | + expire_checks(PoolState(..state, checking: [entry, ..state.checking])) |
| 509 | + } |
| 510 | + }, |
| 511 | + } |
| 512 | +} |
| 513 | + |
| 514 | +/// Close idle connections past `max_lifetime` (always) or past |
| 515 | +/// `idle_timeout` (down to the `min_idle` floor). |
| 516 | +fn reap(state: PoolState) -> PoolState / <proc> { |
| 517 | + let tick = state.settings.tick |
| 518 | + let floor = state.settings.min_idle |
| 519 | + let count = list.length(state.idle) |
| 520 | + let (closing, keeping) = partition_indexed(state.idle, [], [], fn(entry, i) { |
| 521 | + let (info, idle_ticks) = entry |
| 522 | + let too_old = state.settings.max_lifetime > 0 && info.age * tick >= state.settings.max_lifetime |
| 523 | + let too_idle = state.settings.idle_timeout > 0 |
| 524 | + && idle_ticks * tick >= state.settings.idle_timeout |
| 525 | + && count - i > floor |
| 526 | + too_old || too_idle |
| 527 | + }) |
| 528 | + close_all(closing) |
| 529 | + match closing { |
| 530 | + [] -> state, |
| 531 | + _ -> |
| 532 | + PoolState(..state, |
| 533 | + idle: keeping, |
| 534 | + monitored: list.fold(closing, state.monitored, fn(m, entry) { |
| 535 | + let (info, _) = entry |
| 536 | + unwatch(Some(info.conn.owner), m) |
| 537 | + }), |
| 538 | + ), |
| 539 | + } |
| 540 | +} |
| 541 | + |
| 542 | +/// Warm the `min_idle` floor with fresh connections. |
| 543 | +fn replenish(state: PoolState) -> PoolState / <io, proc, random> { |
| 544 | + let missing = state.settings.min_idle - list.length(state.idle) |
| 545 | + if missing <= 0 || live(state) >= state.settings.size { |
| 546 | + state |
| 547 | + } else { |
| 548 | + match client.connect(state.config) { |
| 549 | + Ok(conn) -> replenish(PoolState(..state, idle: [(ConnInfo(conn, 0), 0), ..state.idle])), |
| 550 | + // The database is down; the next tick tries again. |
| 551 | + Error(_) -> state, |
| 552 | + } |
| 553 | + } |
| 554 | +} |
| 555 | + |
| 556 | +/// Close every connection in the list (best effort, fire and forget). |
| 557 | +fn close_all(entries: List<(ConnInfo, Int)>) -> () / <proc> { |
| 558 | + match entries { |
| 559 | + [] -> (), |
| 560 | + [(info, _), ..rest] -> { |
| 561 | + actor.cast(info.conn, client.Close(process.subject())) |
| 562 | + close_all(rest) |
| 563 | + }, |
| 564 | + } |
| 565 | +} |
| 566 | + |
| 253 | 567 | // ---- small helpers -------------------------------------------------------------- |
| 254 | 568 | |
| 255 | | −/// The connections out or idle (those are the live ones: a stopped |
| 256 | | −/// connection actor's subject is removed on its Down). |
| 569 | +/// The connections out, idle, or being checked (those are the live |
| 570 | +/// ones: a stopped connection actor's subject is removed on its Down). |
| 257 | 571 | fn live(state: PoolState) -> Int { |
| 258 | | − list.length(state.idle) + list.length(state.checked_out) |
| 572 | + list.length(state.idle) + list.length(state.checked_out) + list.length(state.checking) |
| 259 | 573 | } |
| 260 | 574 | |
| 261 | 575 | /// Watch `pid` (once). |
| @@ -275,51 +589,105 @@ fn unwatch(pid: Option<Pid>, monitored: List<(Pid, Subject<Down>)>) -> List<(Pid |
| 275 | 589 | } |
| 276 | 590 | } |
| 277 | 591 | |
| 278 | | −/// The connection whose owner's pid is `pid`. |
| 279 | | −fn find_conn(list_: List<(Pid, Subject<client.Msg>)>, pid: Pid) -> Option<Subject<client.Msg>> { |
| 592 | +/// The entry whose borrower is `pid`. |
| 593 | +fn find_conn(list_: List<(Pid, ConnInfo)>, pid: Pid) -> Option<(Pid, ConnInfo)> { |
| 280 | 594 | match list_ { |
| 281 | 595 | [] -> None, |
| 282 | | − [(p, conn), ..rest] -> if p == pid { Some(conn) } else { find_conn(rest, pid) }, |
| 596 | + [entry, ..rest] -> if entry.0 == pid { Some(entry) } else { find_conn(rest, pid) }, |
| 283 | 597 | } |
| 284 | 598 | } |
| 285 | 599 | |
| 286 | 600 | /// The pid a connection is checked out to, by its owner pid. |
| 287 | | −fn owner_of(entries: List<(Pid, Subject<client.Msg>)>, conn: Subject<client.Msg>) -> Option<Pid> { |
| 601 | +fn owner_of(entries: List<(Pid, ConnInfo)>, conn: Subject<client.Msg>) -> Option<Pid> { |
| 288 | 602 | match entries { |
| 289 | 603 | [] -> None, |
| 290 | | − [(pid, c), ..rest] -> if c.owner == conn.owner { Some(pid) } else { owner_of(rest, conn) }, |
| 604 | + [(pid, info), ..rest] -> if info.conn.owner == conn.owner { Some(pid) } else { owner_of(rest, conn) }, |
| 605 | + } |
| 606 | +} |
| 607 | + |
| 608 | +/// The connection info checked out under `conn`'s owner pid. |
| 609 | +fn info_of(entries: List<(Pid, ConnInfo)>, conn: Subject<client.Msg>) -> Option<ConnInfo> { |
| 610 | + match entries { |
| 611 | + [] -> None, |
| 612 | + [(_, info), ..rest] -> if info.conn.owner == conn.owner { Some(info) } else { info_of(rest, conn) }, |
| 291 | 613 | } |
| 292 | 614 | } |
| 293 | 615 | |
| 294 | 616 | /// Drop the pair whose connection is `conn` (by its owner's pid). |
| 295 | | −fn drop_conn(entries: List<(Pid, Subject<client.Msg>)>, conn: Subject<client.Msg>) -> List<(Pid, Subject<client.Msg>)> { |
| 617 | +fn drop_conn(entries: List<(Pid, ConnInfo)>, conn: Subject<client.Msg>) -> List<(Pid, ConnInfo)> { |
| 296 | 618 | match entries { |
| 297 | 619 | [] -> [], |
| 298 | | − [(p, c), ..rest] -> |
| 299 | | − if c.owner == conn.owner { |
| 620 | + [(p, info), ..rest] -> |
| 621 | + if info.conn.owner == conn.owner { |
| 300 | 622 | drop_conn(rest, conn) |
| 301 | 623 | } else { |
| 302 | | − [(p, c), ..drop_conn(rest, conn)] |
| 624 | + [(p, info), ..drop_conn(rest, conn)] |
| 303 | 625 | }, |
| 304 | 626 | } |
| 305 | 627 | } |
| 306 | 628 | |
| 307 | | −/// The subject whose owner is `pid`, in a bare subject list. |
| 308 | | −fn find_owner(list_: List<Subject<client.Msg>>, pid: Pid) -> Option<Subject<client.Msg>> { |
| 629 | +/// The checking entry for `conn`. |
| 630 | +fn find_checking(entries: List<Checking>, conn: Subject<client.Msg>) -> Option<Checking> { |
| 631 | + match entries { |
| 632 | + [] -> None, |
| 633 | + [entry, ..rest] -> |
| 634 | + if entry.info.conn.owner == conn.owner { Some(entry) } else { find_checking(rest, conn) }, |
| 635 | + } |
| 636 | +} |
| 637 | + |
| 638 | +/// The checking entry whose borrower is `pid`. |
| 639 | +fn find_checking_by_borrower(entries: List<Checking>, pid: Pid) -> Option<Checking> { |
| 640 | + match entries { |
| 641 | + [] -> None, |
| 642 | + [entry, ..rest] -> if entry.borrower == pid { Some(entry) } else { find_checking_by_borrower(rest, pid) }, |
| 643 | + } |
| 644 | +} |
| 645 | + |
| 646 | +/// The checking entry whose connection's owner is `pid`. |
| 647 | +fn find_checking_by_owner(entries: List<Checking>, pid: Pid) -> Option<Checking> { |
| 648 | + match entries { |
| 649 | + [] -> None, |
| 650 | + [entry, ..rest] -> if entry.info.conn.owner == pid { Some(entry) } else { find_checking_by_owner(rest, pid) }, |
| 651 | + } |
| 652 | +} |
| 653 | + |
| 654 | +/// Drop the checking entry for `conn`. |
| 655 | +fn drop_checking(entries: List<Checking>, conn: Subject<client.Msg>) -> List<Checking> { |
| 656 | + match entries { |
| 657 | + [] -> [], |
| 658 | + [entry, ..rest] -> |
| 659 | + if entry.info.conn.owner == conn.owner { |
| 660 | + drop_checking(rest, conn) |
| 661 | + } else { |
| 662 | + [entry, ..drop_checking(rest, conn)] |
| 663 | + }, |
| 664 | + } |
| 665 | +} |
| 666 | + |
| 667 | +/// Is `conn` in the idle list? |
| 668 | +fn is_idle(entries: List<(ConnInfo, Int)>, conn: Subject<client.Msg>) -> Bool { |
| 669 | + match entries { |
| 670 | + [] -> False, |
| 671 | + [(info, _), ..rest] -> info.conn.owner == conn.owner || is_idle(rest, conn), |
| 672 | + } |
| 673 | +} |
| 674 | + |
| 675 | +/// The idle entry whose connection's owner is `pid`. |
| 676 | +fn find_owner(list_: List<(ConnInfo, Int)>, pid: Pid) -> Option<(ConnInfo, Int)> { |
| 309 | 677 | match list_ { |
| 310 | 678 | [] -> None, |
| 311 | | − [c, ..rest] -> if c.owner == pid { Some(c) } else { find_owner(rest, pid) }, |
| 679 | + [entry, ..rest] -> if entry.0.conn.owner == pid { Some(entry) } else { find_owner(rest, pid) }, |
| 312 | 680 | } |
| 313 | 681 | } |
| 314 | 682 | |
| 315 | | −fn drop_owner(list_: List<Subject<client.Msg>>, pid: Pid) -> List<Subject<client.Msg>> { |
| 683 | +fn drop_owner(list_: List<(ConnInfo, Int)>, pid: Pid) -> List<(ConnInfo, Int)> { |
| 316 | 684 | match list_ { |
| 317 | 685 | [] -> [], |
| 318 | | − [c, ..rest] -> |
| 319 | | − if c.owner == pid { |
| 686 | + [entry, ..rest] -> |
| 687 | + if entry.0.conn.owner == pid { |
| 320 | 688 | drop_owner(rest, pid) |
| 321 | 689 | } else { |
| 322 | | − [c, ..drop_owner(rest, pid)] |
| 690 | + [entry, ..drop_owner(rest, pid)] |
| 323 | 691 | }, |
| 324 | 692 | } |
| 325 | 693 | } |
| @@ -343,6 +711,35 @@ fn has_pid(entries: List<(Pid, a)>, pid: Pid) -> Bool { |
| 343 | 711 | } |
| 344 | 712 | } |
| 345 | 713 | |
| 714 | +/// `list.partition`, with each element's index: the idle-timeout floor |
| 715 | +/// counts from the end of the list. `(closing, keeping)`. |
| 716 | +fn partition_indexed( |
| 717 | + entries: List<a>, |
| 718 | + yes: List<a>, |
| 719 | + no: List<a>, |
| 720 | + close: fn(a, Int) -> Bool, |
| 721 | +) -> (List<a>, List<a>) { |
| 722 | + partition_from(entries, yes, no, 0, close) |
| 723 | +} |
| 724 | + |
| 725 | +fn partition_from( |
| 726 | + entries: List<a>, |
| 727 | + yes: List<a>, |
| 728 | + no: List<a>, |
| 729 | + i: Int, |
| 730 | + close: fn(a, Int) -> Bool, |
| 731 | +) -> (List<a>, List<a>) { |
| 732 | + match entries { |
| 733 | + [] -> (list.reverse(yes), list.reverse(no)), |
| 734 | + [entry, ..rest] -> |
| 735 | + if close(entry, i) { |
| 736 | + partition_from(rest, [entry, ..yes], no, i + 1, close) |
| 737 | + } else { |
| 738 | + partition_from(rest, yes, [entry, ..no], i + 1, close) |
| 739 | + }, |
| 740 | + } |
| 741 | +} |
| 742 | + |
| 346 | 743 | /// Watch every connection's owner. |
| 347 | 744 | fn monitor_all(conns: List<Subject<client.Msg>>, acc: List<(Pid, Subject<Down>)>) -> List<(Pid, Subject<Down>)> / <proc> { |
| 348 | 745 | match conns { |