Skip to content

Commit

Permalink
Add language info to fenced code blocks. Testing it in GFM.
Browse files Browse the repository at this point in the history
  • Loading branch information
spier committed Dec 12, 2023
1 parent 6d52a0e commit 01dd23b
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Or install it yourself as:

Add an initializer at config/knockoff.rb with the below contents

```
```ruby
Knockoff.enabled = true # NOTE: Consider adding ENV based disabling
```

Expand Down Expand Up @@ -71,7 +71,7 @@ KNOCKOFF_REPLICA_ENVS=REPLICA_1,REPLICA_2

Lastly, knockoff will read the `'knockoff_replicas'` database.yml config for specifying additional params:

```
```yml
# database.yml

knockoff_replicas:
Expand All @@ -83,21 +83,21 @@ knockoff_replicas:
To use one of the replica databases, use
```
```ruby
Knockoff.on_replica { User.count }
```

To force primary, use

```
```ruby
Knockoff.on_primary { User.create(name: 'Bob') }
```

### Using in Controllers

A common use case is to use replicas for GET requests and otherwise use primary. A simplified use case might look something like this:

```
```ruby
# application_controller.rb

around_action :choose_database
Expand All @@ -120,7 +120,7 @@ end

Replicas will often be slightly behind the primary database. To compensate, consider "sticking" a user who has recently made changes to the primary for a small duration of time to the primary database. This will avoid cases where a user creates a record on primary, is redirected to view that record, and receives a 404 error since the record is not yet in the replica. A simple implementation for this could look like:

```
```ruby
# application_record.rb

after_commit :track_commit_occurred_in_request
Expand Down Expand Up @@ -149,13 +149,13 @@ Then, in your `should_use_primary_database?` method, consult `session[:use_leade

Knockoff can be configured during runtime. This is done through the `establish_new_connections!` method which takes in a hash of new configurations to apply to each replica before re-connecting.

```
```ruby
Knockoff.establish_new_connections!({ 'pool' => db_pool })
```

For example, to specify a puma connection pool at bootup your code might look something like

```
```ruby
# puma.rb

db_pool = Integer(ENV['PUMA_WORKER_DB_POOL'] || threads_count)
Expand All @@ -171,7 +171,7 @@ Knockoff.establish_new_connections!({ 'pool' => db_pool })

For forking servers, you may disconnect all replicas before forking with `Knockoff.disconnect_all!`.

```
```ruby
# puma.rb

before_fork do
Expand Down

0 comments on commit 01dd23b

Please sign in to comment.