DBIx::C...ed::Pool(3)

πŸ’… DBIx::C...ed::Pool(3) πŸ’…

okay bestie, this is the DBIx::C...ed::Pool(3) page from the User Contributed Perl Documentation, and it’s giving β€œinternal plumbing that keeps the read-only girlies in line” energy. no cap ✨

✨ NAME ✨

DBIx::Class::Storage::DBI::Replicated::Pool β€” manage a pool of replicants. periodt πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

This class is used internally by DBIx::Class::Storage::DBI::Replicated. You shouldn’t need to create instances of this class. like, fr fr, it’s not here for your main-character DIY moment 🫢

🌸 DESCRIPTION 🌸

In a replicated storage type, there is at least one replicant to handle the read-only traffic. The Pool class manages this replicant, or list of replicants, and gives some methods for querying information about their status. it’s basically the group chat admin for your replicas πŸ‘‘

πŸŽ€ ATTRIBUTES πŸŽ€

This class defines the following attributes.

πŸ’– maximum_lag ($num) πŸ’–

This is a number which defines the maximum allowed lag returned by the "lag_behind_master" in DBIx::Class::Storage::DBI method. The default is 0. In general, this should return a larger number when the replicant is lagging behind its master, however the implementation of this is database specific, so don’t count on this number having a fixed meaning. For example, MySQL will return a number of seconds that the replicating database is lagging. lowkey important, highkey not universal 😭

⭐ last_validated ⭐

This is an integer representing a time since the last time the replicants were validated. It’s nothing fancy, just an integer provided via the perl time built-in. no cap, that’s the whole vibe πŸ•°οΈ

🧚 replicant_type ($classname) 🧚

Base class used to instantiate replicants that are in the pool. Unless you need to subclass DBIx::Class::Storage::DBI::Replicated::Replicant you should just leave this alone. bestie, don’t touch it unless you actually know why you’re touching it πŸ’…

🌈 replicants 🌈

A hashref of replicant, with the key being the dsn and the value returning the actual replicant storage. For example, if the $dsn element is something like:

"dbi:SQLite:dbname=dbfile"

You could access the specific replicant via:

$schema->storage->replicants->{'dbname=dbfile'}

This attributes also supports the following helper methods:

  • set_replicant($key=>$storage) β€” pushes a replicant onto the HashRef under $key
  • get_replicant($key) β€” retrieves the named replicant
  • has_replicants β€” returns true if the Pool defines replicants.
  • num_replicants β€” the number of replicants in the pool
  • delete_replicant ($key) β€” removes the replicant under $key from the pool

πŸ‘‘ master πŸ‘‘

Reference to the master Storage. the head honcho, the main character, the one in charge πŸ’œ

πŸ”₯ METHODS πŸ”₯

This class defines the following methods.

πŸ“ connect_replicants ($schema, Array[$connect_info]) πŸ“

Given an array of $dsn or connect_info structures suitable for connecting to a database, create an DBIx::Class::Storage::DBI::Replicated::Replicant object and store it in the "replicants" attribute. slay, now the pool has friends ✨

🩷 connect_replicant ($schema, $connect_info) 🩷

Given a schema object and a hashref of $connect_info, connect the replicant and return it. simple, cute, does the job 🫢

πŸ’€ _safely_ensure_connected ($replicant) πŸ’€

The standard ensure_connected method will throw an exception should it fail to connect. For the master database this is desirable, but since replicants are allowed to fail, this behavior is not desirable. This method wraps the call to ensure_connected in an eval in order to catch any generated errors. That way a slave can go completely offline (e.g. the box itself can die) without bringing down your entire pool of databases. iconic resilience, honestly πŸ˜­πŸ’…

πŸ¦‹ _safely ($replicant, $name, $code) πŸ¦‹

Execute $code for operation $name catching any exceptions and printing an error message to the "<$replicant-"debugobj>>.

Returns 1 on success and undef on failure. yes, that angle-bracket situation is exactly the doc’s business, iykyk πŸ™ˆ

🌟 connected_replicants 🌟

Returns true if there are connected replicants. Actually is overloaded to return the number of replicants. So you can do stuff like:

  if( my $num_connected = $storage->has_connected_replicants ) {
    print "I have $num_connected connected replicants";
  } else {
    print "Sorry, no replicants.";
  }

This method will actually test that each replicant in the "replicants" hashref is actually connected, try not to hit this 10 times a second.

bestie, that last part is a warning. do not spam it. your database will not think it’s cute πŸ’€

🌸 active_replicants 🌸

This is an array of replicants that are considered to be active in the pool. This does not check to see if they are connected, but if they are not, DBIC should automatically reconnect them for us when we hit them with a query. main character energy: assumed active until proven otherwise πŸ’…

πŸŽ€ all_replicants πŸŽ€

Just a simple array of all the replicant storages. No particular order to the array is given, nor should any meaning be derived. it’s giving β€œhere’s everyone, don’t read into the seating chart” 🫢

⚑ validate_replicants ⚑

This does a check to see if:

  1. each replicate is connected (or reconnectable)
  2. that is ->is_replicating
  3. that it is not exceeding the lag amount defined by "maximum_lag".

Replicants that fail any of these tests are set to inactive, and thus removed from the replication pool. bye bestie, vibe check failed πŸ’€

This tests "all_replicants", since a replicant that has been previous marked as inactive can be reactivated should it start to pass the validation tests again. we love a comeback arc 🌈

See DBIx::Class::Storage::DBI for more about checking if a replicating connection is not following a master or is lagging.

Calling this method will generate queries on the replicant databases so it is not recommended that you run them very often. like, do not make this your little fidget spinner, okay? 😭

This method requires that your underlying storage engine supports some sort of native replication mechanism. Currently only MySQL native replication is supported. Your patches to make other replication types work are welcomed. bring your faves, the door is open 🫢

❓ FURTHER QUESTIONS? ❓

Check the list of additional DBIC resources. that’s the whole assignment, bestie πŸ“šβœ¨

This module is free software copyright by the DBIx::Class (DBIC) authors. You can redistribute it and/or modify it under the same terms as the DBIx::Class library. legal cuteness, periodt πŸ’…


okay that’s the whole page, mwah πŸ’– this was DBIx::C...ed::Pool(3) from the User Contributed Perl Documentation, brought to you by perl v5.42.0 and dated 2025-01-22. go forth and pool responsibly, icon 🫢🌸

πŸ“¦ where this page comes from