PHP code example of uak35 / laravel-weighted-dbmanager

1. Go to this page and download the library: Download uak35/laravel-weighted-dbmanager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uak35 / laravel-weighted-dbmanager example snippets


config('app.providers') ?? ServiceProvider::defaultProviders()->toArray()
// … and only then appends whatever bootstrap/providers.php returns

'providers' => \Illuminate\Support\ServiceProvider::defaultProviders()->replace([
    \Illuminate\Database\DatabaseServiceProvider::class
        => \Uak35\WeightedDbManager\Providers\WeightedDatabaseServiceProvider::class,
])->toArray(),

'pgsql' => [
    'driver' => 'pgsql',
    'write' => [[ 'host' => '127.0.0.1', 'port' => 5433, /* … */ ]],
    'read' => [
        ['host' => '10.0.1.11', 'port' => 5432, 'cpu_cores' => 16, 'ram_gb' => 64],
        ['host' => '10.0.1.12', 'port' => 5432, 'cpu_cores' => 8,  'ram_gb' => 32],
        ['host' => '10.0.1.13', 'port' => 5432, 'weight' => 0],   // explicitly disabled
    ],
    // optional per-connection overrides
    'weight_formula' => 'diminishing',
    'weight_cpu_factor' => 3.0,
    'weight_ram_factor' => 3.375,
],

'pgcat' => [
    // No (bool) here on purpose: a cast reads 'off' as on, so `enabled` and
    // `allow_local_fallback` hand over the value as written and let the package
    // read it — see "Switches are read, not cast" above.
    'enabled'          => env('SWRR_PGCAT_ENABLED', false),
    'config_path'      => env('SWRR_PGCAT_CONFIG', '/etc/pgcat/pgcat.toml'),
    'readers_path'     => env('SWRR_PGCAT_READERS', '/etc/pgcat/pgcat-readers.toml'),
    'no_readers_path'  => env('SWRR_PGCAT_NO_READERS', '/etc/pgcat/pgcat-no-readers.toml'),
    'restart_command'  => 'supervisorctl restart "pgcat:*"',
    'reload_command'   => 'supervisorctl signal HUP "pgcat:*"',
    'use_reload'       => true,
    'state_file'       => sys_get_temp_dir().'/pgcat-flip-state.json',
    'lock_file'        => sys_get_temp_dir().'/pgcat-flip.lock',
],

$manager->setAllowLocalFallback(false);

use Uak35\WeightedDbManager\Http\Controllers\DatabaseHealthController;

Route::get('/health/db', [DatabaseHealthController::class, 'index'])
    ->middleware('auth.basic');

// routes/console.php
Schedule::command('db:probe-replicas')->everyThirtySeconds()->withoutOverlapping()->runInBackground();
Schedule::command('db:pgcat-flip')->everyMinute()->withoutOverlapping(60)->runInBackground();

try {
    DB::select('SELECT …');
} catch (\Illuminate\Database\QueryException $e) {
    app('db')->markReplicaFailed('10.0.1.11', 5432, $e->getMessage());
}
bash
   php artisan vendor:publish --tag=db-manager-config
   
json
"post-install-cmd": [
    "@php artisan vendor:publish --tag=db-manager-config --ansi"
],
"post-update-cmd": [
    "@php artisan vendor:publish --tag=db-manager-config --ansi"
]
bash
# Unix
diff -u config/db-manager.php vendor/uak35/laravel-weighted-dbmanager/config/db-manager.php

# Windows
fc config\db-manager.php vendor\uak35\laravel-weighted-dbmanager\config\db-manager.php