Skip to content

Commit b309ead

Browse files
authored
Handle SIGTERM. Add docker-compose.yml (#59)
* docker-compsoe * remove statsd config * readme
1 parent 341ebf4 commit b309ead

File tree

8 files changed

+139
-24
lines changed

8 files changed

+139
-24
lines changed

.circleci/pgcat.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ healthcheck_timeout = 100
2929
# For how long to ban a server if it fails a health check (seconds).
3030
ban_time = 60 # Seconds
3131

32-
# Stats will be sent here
33-
statsd_address = "127.0.0.1:8125"
34-
3532
#
3633
# User to use for authentication against the server.
3734
[user]

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ COPY --from=builder /app/target/release/pgcat /usr/bin/pgcat
88
COPY --from=builder /app/pgcat.toml /etc/pgcat/pgcat.toml
99
WORKDIR /etc/pgcat
1010
ENV RUST_LOG=info
11-
ENTRYPOINT ["/usr/bin/pgcat"]
11+
CMD ["pgcat"]

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Meow. PgBouncer rewritten in Rust, with sharding, load balancing and failover support.
88

9-
**Alpha**: looking for alpha testers, see [#35](https://github.com/levkk/pgcat/issues/35).
9+
**Beta**: looking for beta testers, see [#35](https://github.com/levkk/pgcat/issues/35).
1010

1111
## Features
1212
| **Feature** | **Status** | **Comments** |
@@ -24,8 +24,16 @@ Meow. PgBouncer rewritten in Rust, with sharding, load balancing and failover su
2424

2525
## Deployment
2626

27-
See `Dockerfile` for example deployment using Docker. The pooler is configured to spawn 4 workers so 4 CPUs are recommended for optimal performance.
28-
That setting can be adjusted to spawn as many (or as little) workers as needed.
27+
See `Dockerfile` for example deployment using Docker. The pooler is configured to spawn 4 workers so 4 CPUs are recommended for optimal performance. That setting can be adjusted to spawn as many (or as little) workers as needed.
28+
29+
For quick local example, use the Docker Compose environment provided:
30+
31+
```bash
32+
docker-compose up
33+
34+
# In a new terminal:
35+
psql -h 127.0.0.1 -p 6432 -c 'SELECT 1'
36+
```
2937

3038
### Config
3139

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3"
2+
services:
3+
postgres:
4+
image: postgres:13
5+
environment:
6+
POSTGRES_PASSWORD: postgres
7+
POSTGRES_HOST_AUTH_METHOD: md5
8+
pgcat:
9+
build: .
10+
command:
11+
- "pgcat"
12+
- "/etc/pgcat/pgcat.toml"
13+
volumes:
14+
- "${PWD}/examples/docker/pgcat.toml:/etc/pgcat/pgcat.toml"
15+
ports:
16+
- "6432:6432"

examples/docker/pgcat.toml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# PgCat config example.
3+
#
4+
5+
#
6+
# General pooler settings
7+
[general]
8+
9+
# What IP to run on, 0.0.0.0 means accessible from everywhere.
10+
host = "0.0.0.0"
11+
12+
# Port to run on, same as PgBouncer used in this example.
13+
port = 6432
14+
15+
# How many connections to allocate per server.
16+
pool_size = 15
17+
18+
# Pool mode (see PgBouncer docs for more).
19+
# session: one server connection per connected client
20+
# transaction: one server connection per client transaction
21+
pool_mode = "transaction"
22+
23+
# How long to wait before aborting a server connection (ms).
24+
connect_timeout = 5000
25+
26+
# How much time to give `SELECT 1` health check query to return with a result (ms).
27+
healthcheck_timeout = 1000
28+
29+
# For how long to ban a server if it fails a health check (seconds).
30+
ban_time = 60 # Seconds
31+
32+
#
33+
# User to use for authentication against the server.
34+
[user]
35+
name = "postgres"
36+
password = "postgres"
37+
38+
39+
#
40+
# Shards in the cluster
41+
[shards]
42+
43+
# Shard 0
44+
[shards.0]
45+
46+
# [ host, port, role ]
47+
servers = [
48+
[ "postgres", 5432, "primary" ],
49+
[ "postgres", 5432, "replica" ],
50+
# [ "127.0.1.1", 5432, "replica" ],
51+
]
52+
# Database name (e.g. "postgres")
53+
database = "postgres"
54+
55+
[shards.1]
56+
# [ host, port, role ]
57+
servers = [
58+
[ "postgres", 5432, "primary" ],
59+
[ "postgres", 5432, "replica" ],
60+
# [ "127.0.1.1", 5432, "replica" ],
61+
]
62+
database = "postgres"
63+
64+
[shards.2]
65+
# [ host, port, role ]
66+
servers = [
67+
[ "postgres", 5432, "primary" ],
68+
[ "postgres", 5432, "replica" ],
69+
# [ "127.0.1.1", 5432, "replica" ],
70+
]
71+
database = "postgres"
72+
73+
74+
# Settings for our query routing layer.
75+
[query_router]
76+
77+
# If the client doesn't specify, route traffic to
78+
# this role by default.
79+
#
80+
# any: round-robin between primary and replicas,
81+
# replica: round-robin between replicas only without touching the primary,
82+
# primary: all queries go to the primary unless otherwise specified.
83+
default_role = "any"
84+
85+
86+
# Query parser. If enabled, we'll attempt to parse
87+
# every incoming query to determine if it's a read or a write.
88+
# If it's a read query, we'll direct it to a replica. Otherwise, if it's a write,
89+
# we'll direct it to the primary.
90+
query_parser_enabled = false
91+
92+
# If the query parser is enabled and this setting is enabled, the primary will be part of the pool of databases used for
93+
# load balancing of read queries. Otherwise, the primary will only be used for write
94+
# queries. The primary can always be explicitely selected with our custom protocol.
95+
primary_reads_enabled = true
96+
97+
# So what if you wanted to implement a different hashing function,
98+
# or you've already built one and you want this pooler to use it?
99+
#
100+
# Current options:
101+
#
102+
# pg_bigint_hash: PARTITION BY HASH (Postgres hashing function)
103+
# sha1: A hashing function based on SHA1
104+
#
105+
sharding_function = "pg_bigint_hash"

pgcat.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ healthcheck_timeout = 1000
2929
# For how long to ban a server if it fails a health check (seconds).
3030
ban_time = 60 # Seconds
3131

32-
# Stats will be sent here
33-
statsd_address = "127.0.0.1:8125"
34-
3532
#
3633
# User to use for authentication against the server.
3734
[user]

src/config.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ pub struct General {
103103
pub connect_timeout: u64,
104104
pub healthcheck_timeout: u64,
105105
pub ban_time: i64,
106-
pub statsd_address: String,
107106
}
108107

109108
impl Default for General {
@@ -116,7 +115,6 @@ impl Default for General {
116115
connect_timeout: 5000,
117116
healthcheck_timeout: 1000,
118117
ban_time: 60,
119-
statsd_address: String::from("127.0.0.1:8125"),
120118
}
121119
}
122120
}
@@ -198,10 +196,6 @@ impl From<&Config> for std::collections::HashMap<String, String> {
198196
config.general.healthcheck_timeout.to_string(),
199197
),
200198
("ban_time".to_string(), config.general.ban_time.to_string()),
201-
(
202-
"statsd_address".to_string(),
203-
config.general.statsd_address.to_string(),
204-
),
205199
(
206200
"default_role".to_string(),
207201
config.query_router.default_role.to_string(),

src/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,14 @@ async fn main() {
205205
}
206206
});
207207

208-
// Setup shut down sequence
209-
match signal::ctrl_c().await {
210-
Ok(()) => {
211-
info!("Shutting down...");
212-
}
208+
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();
213209

214-
Err(err) => {
215-
error!("Unable to listen for shutdown signal: {}", err);
216-
}
210+
tokio::select! {
211+
_ = signal::ctrl_c() => (),
212+
_ = term_signal.recv() => (),
217213
};
214+
215+
info!("Shutting down...");
218216
}
219217

220218
/// Format chrono::Duration to be more human-friendly.

0 commit comments

Comments
 (0)