sequel-pg_reaper 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +13 -0
- data/LICENSE.txt +21 -0
- data/Readme.adoc +94 -0
- data/documents/motivation.adoc +13 -0
- data/lib/sequel/extensions/pg_reaper.rb +11 -0
- data/lib/sequel/pg_reaper/version.rb +11 -0
- data/lib/sequel/pg_reaper.rb +81 -0
- metadata +68 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c7913ddd64a29c3656aae7b2dc5bf9dd24d98155d95188a39c56eee9bfbfc595
|
|
4
|
+
data.tar.gz: 0f796c4ac4f0350a9905b20be4b812ca6832129e9241af6a1d0c8f3c17c91a6d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d1a5880580d6ce8d9cfc95bfd043659c8ce2bf9ef9a20f961bb3eb7b1792052a508c9d5814f2ac2b6c4db95a75f4178e413653feb439fb37f75cda04ffe1b6aa
|
|
7
|
+
data.tar.gz: 296f67f6788c2700be3c879c0327ede358268aac94a754fd8ffc3b40a6fbc7b37d40794870df20e63baf20e9f5441831b5a6cfc1a99ea49a762dfafa46634d17
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 (2026-10-14)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add pg_reaper extension to recycle aged Sequel connections ([9003e07](https://github.com/rubyists/sequel-pg_reaper/commit/9003e0769e3d00a28cb9f71385ef298425d69629))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* complete the example documentation for real world usage ([#1](https://github.com/rubyists/sequel-pg_reaper/issues/1)) ([c77e39c](https://github.com/rubyists/sequel-pg_reaper/commit/c77e39c7993208aeb431bfba074795e56bfe46d1))
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tj (bougyman) Vanderpoel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/Readme.adoc
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
= sequel-pg_reaper
|
|
2
|
+
Tj Vanderpoel (bougyman) <bougyman@users.noreply.github.com>
|
|
3
|
+
:toc: right
|
|
4
|
+
:toclevels: 3
|
|
5
|
+
:sectanchors:
|
|
6
|
+
:icons: font
|
|
7
|
+
:tip-caption: 💡
|
|
8
|
+
:note-caption: 📝
|
|
9
|
+
:experimental:
|
|
10
|
+
:docinfo: private
|
|
11
|
+
:docinfodir: {docdir}
|
|
12
|
+
ifdef::env-github[]
|
|
13
|
+
:tip-caption: :bulb:
|
|
14
|
+
:note-caption: :information_source:
|
|
15
|
+
:important-caption: :heavy_exclamation_mark:
|
|
16
|
+
:caution-caption: :fire:
|
|
17
|
+
:warning-caption: :warning:
|
|
18
|
+
endif::[]
|
|
19
|
+
:conventional-commits: https://www.conventionalcommits.org/en/v1.0.0/[Conventional Commits]
|
|
20
|
+
|
|
21
|
+
A https://sequel.jeremyevans.net/[Sequel] extension that retires pooled connections once they pass a maximum age.
|
|
22
|
+
|
|
23
|
+
See link:documents/motivation.adoc[the motivation] for why it exists.
|
|
24
|
+
|
|
25
|
+
== Usage
|
|
26
|
+
|
|
27
|
+
[source,ruby]
|
|
28
|
+
----
|
|
29
|
+
DB = Sequel.connect(url, connection_max_lifetime: 90)
|
|
30
|
+
DB.extension :pg_reaper
|
|
31
|
+
----
|
|
32
|
+
|
|
33
|
+
* `connection_max_lifetime` is in seconds and defaults to 120
|
|
34
|
+
* The age is checked as a connection is checked out of the pool, so the cost is one hash lookup
|
|
35
|
+
* A connection that is too old is disconnected and replaced before your code sees it
|
|
36
|
+
* A connection already checked out, such as inside a transaction, is never reaped
|
|
37
|
+
* Idle connections are not closed in the background, see <<Idle connections>>
|
|
38
|
+
* Single and sharded single pools are not supported
|
|
39
|
+
|
|
40
|
+
The lifetime can also be changed at runtime with `DB.pool.connection_max_lifetime = 60`.
|
|
41
|
+
|
|
42
|
+
== Idle connections
|
|
43
|
+
|
|
44
|
+
Nothing in this extension, or in Sequel's `connection_validator`, closes an idle socket in the background.
|
|
45
|
+
Both act when a connection is checked out:
|
|
46
|
+
|
|
47
|
+
* `pg_reaper` replaces any connection older than `connection_max_lifetime`, however long it sat idle
|
|
48
|
+
* `connection_validator` runs a query on connections idle longer than `connection_validation_timeout`
|
|
49
|
+
and replaces the ones that fail
|
|
50
|
+
|
|
51
|
+
For a pgdog rollout `pg_reaper` is enough, if two conditions hold that this extension does not enforce:
|
|
52
|
+
|
|
53
|
+
* The pod's shutdown grace is longer than `connection_max_lifetime`, as with pgdog's 180s against the default 120s
|
|
54
|
+
* New connections stop reaching a pod once it is terminating
|
|
55
|
+
|
|
56
|
+
Connection age does not tell you a connection is alive. If the grace is shorter than the lifetime, a connection
|
|
57
|
+
opened just before SIGTERM is still young when the pod dies, and it is handed out dead.
|
|
58
|
+
|
|
59
|
+
Load `connection_validator` as well to cover connections that die for other reasons, such as a pgdog crash
|
|
60
|
+
or a network failure:
|
|
61
|
+
|
|
62
|
+
[source,ruby]
|
|
63
|
+
----
|
|
64
|
+
DB = Sequel.connect(url, connection_max_lifetime: 120)
|
|
65
|
+
DB.extension :connection_validator, :pg_reaper
|
|
66
|
+
DB.pool.connection_validation_timeout = 30
|
|
67
|
+
----
|
|
68
|
+
|
|
69
|
+
* `connection_validation_timeout` is an idle threshold, not a detection deadline, so the 30 above is not a promise
|
|
70
|
+
that a dead connection is found within 30s
|
|
71
|
+
* Only a connection idle longer than the threshold is validated at checkout, and an unused dead connection is not
|
|
72
|
+
found until something checks it out
|
|
73
|
+
* Set it to `-1` to validate on every checkout, which costs a query each time
|
|
74
|
+
* Load order does not matter
|
|
75
|
+
|
|
76
|
+
[WARNING]
|
|
77
|
+
----
|
|
78
|
+
A connection checked out for longer than the time left before the pod terminates is not interrupted, keep transactions short
|
|
79
|
+
----
|
|
80
|
+
|
|
81
|
+
== Development
|
|
82
|
+
|
|
83
|
+
[source,sh]
|
|
84
|
+
----
|
|
85
|
+
mise install
|
|
86
|
+
bundle exec rake
|
|
87
|
+
----
|
|
88
|
+
|
|
89
|
+
Tests use `Sequel.mock`, so no database is needed.
|
|
90
|
+
|
|
91
|
+
Commits and PR titles must follow {conventional-commits} to be included in the changelog and release notes.
|
|
92
|
+
The hooks in `.githooks` enforce it, and `mise run setup` enables them.
|
|
93
|
+
|
|
94
|
+
Releases are cut by https://github.com/googleapis/release-please[release-please] and published to rubygems.org.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
= Motivation
|
|
2
|
+
|
|
3
|
+
To make pgdog rollouts smooth.
|
|
4
|
+
|
|
5
|
+
Pgdog's shutdown timer is 180s. If the clients all disconnect and reconnect themselves within 120s,
|
|
6
|
+
they are drained from pgdog before the pod terminates, and have already been routed to a fresher pgdog.
|
|
7
|
+
|
|
8
|
+
Persistent connections also keep load from ever balancing across all the dogs. Recycling connections
|
|
9
|
+
now and then lets the load eventually balance.
|
|
10
|
+
|
|
11
|
+
== Problem
|
|
12
|
+
|
|
13
|
+
When pgdog pods cycle in Kubernetes, Sequel can get bad disconnects in the middle of transactions.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../pg_reaper'
|
|
4
|
+
|
|
5
|
+
# The pg_reaper extension retires pooled connections older than the Database's
|
|
6
|
+
# :connection_max_lifetime option (default 120s) as they are checked out.
|
|
7
|
+
Sequel::Database.register_extension(:pg_reaper) do |db|
|
|
8
|
+
pool = db.pool.extend(Rubyists::Sequel::PgReaper::PoolMethods)
|
|
9
|
+
lifetime = db.opts[:connection_max_lifetime]
|
|
10
|
+
pool.connection_max_lifetime = lifetime if lifetime
|
|
11
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sequel'
|
|
4
|
+
require_relative 'pg_reaper/version'
|
|
5
|
+
|
|
6
|
+
module Rubyists
|
|
7
|
+
module Sequel
|
|
8
|
+
# Retires pooled connections once they pass a maximum age, so clients move
|
|
9
|
+
# to fresher pgdog pods before the old ones terminate
|
|
10
|
+
module PgReaper
|
|
11
|
+
DEFAULT_MAX_LIFETIME = 120
|
|
12
|
+
|
|
13
|
+
# Extended onto the Database's connection pool
|
|
14
|
+
module PoolMethods
|
|
15
|
+
attr_reader :connection_max_lifetime
|
|
16
|
+
|
|
17
|
+
def self.extended(pool)
|
|
18
|
+
case pool.pool_type
|
|
19
|
+
when :single, :sharded_single
|
|
20
|
+
raise ::Sequel::Error, 'cannot load pg_reaper extension if using single or sharded_single connection pool'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
pool.instance_exec do
|
|
24
|
+
sync do
|
|
25
|
+
@connection_timers ||= {}.compare_by_identity
|
|
26
|
+
@connection_max_lifetime ||= DEFAULT_MAX_LIFETIME
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Maximum connection age in seconds, checked as a connection is checked out
|
|
32
|
+
def connection_max_lifetime=(seconds)
|
|
33
|
+
unless seconds.is_a?(Numeric) && seconds.positive?
|
|
34
|
+
raise ArgumentError, 'connection_max_lifetime must be a positive number'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@connection_max_lifetime = seconds
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def make_new(*)
|
|
43
|
+
conn = super
|
|
44
|
+
sync { @connection_timers[conn] = ::Sequel.start_timer }
|
|
45
|
+
conn
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def disconnect_connection(conn)
|
|
49
|
+
sync { @connection_timers.delete(conn) }
|
|
50
|
+
super
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Connections opened before the extension loaded get their clock started on first checkout
|
|
54
|
+
def expired?(conn)
|
|
55
|
+
timer = sync { @connection_timers[conn] ||= ::Sequel.start_timer }
|
|
56
|
+
::Sequel.elapsed_seconds_since(timer) > @connection_max_lifetime
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def forget_allocation(server)
|
|
60
|
+
case pool_type
|
|
61
|
+
when :sharded_threaded, :sharded_timed_queue
|
|
62
|
+
sync { @allocated[server].delete(::Sequel.current) }
|
|
63
|
+
else
|
|
64
|
+
sync { @allocated.delete(::Sequel.current) }
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Mirrors Sequel's connection_validator: drop the allocation, disconnect, and ask for another connection
|
|
69
|
+
def acquire(*args)
|
|
70
|
+
loop do
|
|
71
|
+
conn = super
|
|
72
|
+
return conn unless conn && expired?(conn)
|
|
73
|
+
|
|
74
|
+
forget_allocation(args.last)
|
|
75
|
+
disconnect_acquired_connection(conn, *args)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sequel-pg_reaper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- bougyman
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: sequel
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '5.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '5.0'
|
|
26
|
+
description: Retires pooled Sequel connections after a maximum lifetime, so clients
|
|
27
|
+
reach fresher pgdog pods.
|
|
28
|
+
email:
|
|
29
|
+
- bougyman@users.noreply.github.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- CHANGELOG.md
|
|
35
|
+
- LICENSE.txt
|
|
36
|
+
- Readme.adoc
|
|
37
|
+
- documents/motivation.adoc
|
|
38
|
+
- lib/sequel/extensions/pg_reaper.rb
|
|
39
|
+
- lib/sequel/pg_reaper.rb
|
|
40
|
+
- lib/sequel/pg_reaper/version.rb
|
|
41
|
+
homepage: https://github.com/rubyists/sequel-pg_reaper
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
allowed_push_host: https://rubygems.org
|
|
46
|
+
homepage_uri: https://github.com/rubyists/sequel-pg_reaper
|
|
47
|
+
source_code_uri: https://github.com/rubyists/sequel-pg_reaper
|
|
48
|
+
changelog_uri: https://github.com/rubyists/sequel-pg_reaper/blob/main/CHANGELOG.md
|
|
49
|
+
rubygems_mfa_required: 'true'
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: 3.3.0
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 4.0.20
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: Recycles Sequel connections by age so pgdog pods can drain cleanly during
|
|
67
|
+
rollouts.
|
|
68
|
+
test_files: []
|