Skip to content

Commit

Permalink
Merge pull request #28 from spier/syntax-highlight-readme
Browse files Browse the repository at this point in the history
Syntax highlight all code in the main README file
  • Loading branch information
davkutalek authored Aug 12, 2024
2 parents 9ad0838 + dd0a7f0 commit 3c07624
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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
```

### Configuration

Configuration is done using ENV properties. This makes it easy to add and remove replicas at runtime (or to fully disable if needed). First, set up ENV variables pointing to your replica databases. Consider using the [dotenv](https://github.com/bkeepers/dotenv) gem for manging ENV variables.

```
```dotenv
# .env
REPLICA_1=postgres://username:password@localhost:5432/database_name
Expand All @@ -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 3c07624

Please sign in to comment.