Skip to content

Commit 4c16ba3

Browse files
committed
some comments
1 parent 9fe50c4 commit 4c16ba3

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/client.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Client {
168168

169169
// Active shard we're talking to.
170170
// The lifetime of this depends on the pool mode:
171-
// - if in session mode, this lives until client disconnects or changes it,
171+
// - if in session mode, this lives until the client disconnects,
172172
// - if in transaction mode, this lives for the duration of one transaction.
173173
let mut shard: Option<usize> = None;
174174

@@ -177,7 +177,7 @@ impl Client {
177177
// either a `Q` (query) or `P` (prepare, extended protocol).
178178
// We can parse it here before grabbing a server from the pool,
179179
// in case the client is sending some control messages, e.g.
180-
// SET sharding_context.key = '1234';
180+
// SET SHARDING KEY TO 'bigint';
181181
let mut message = read_message(&mut self.read).await?;
182182

183183
// Parse for special select shard command.
@@ -191,9 +191,6 @@ impl Client {
191191
None => (),
192192
};
193193

194-
// The message is part of the regular protocol.
195-
// self.buffer.put(message);
196-
197194
// Grab a server from the pool.
198195
// None = any shard
199196
let connection = pool.get(shard).await.unwrap();
@@ -361,12 +358,19 @@ impl Client {
361358
guard.remove(&(self.process_id, self.secret_key));
362359
}
363360

361+
/// Determine if the query is part of our special syntax, extract
362+
/// the shard key, and return the shard to query based on Postgres'
363+
/// PARTITION BY HASH function.
364364
async fn select_shard(&mut self, mut buf: BytesMut, shards: usize) -> Option<usize> {
365365
let code = buf.get_u8() as char;
366366

367+
// Only supporting simpe protocol here, so
368+
// one would have to execute something like this:
369+
// psql -c "SET SHARDING KEY TO '1234'"
370+
// after sanitizing the value manually, which can be just done with an
371+
// int parser, e.g. `let key = "1234".parse::<i64>().unwrap()`.
367372
match code {
368373
'Q' => (),
369-
// 'P' => (),
370374
_ => return None,
371375
};
372376

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub struct Config {
4343
pub shards: HashMap<String, Shard>,
4444
}
4545

46+
/// Parse the config.
4647
pub async fn parse(path: &str) -> Result<Config, Error> {
47-
// let path = Path::new(path);
4848
let mut contents = String::new();
4949
let mut file = match File::open(path).await {
5050
Ok(file) => file,

src/messages.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub async fn auth_ok(stream: &mut TcpStream) -> Result<(), Error> {
2626

2727
/// Send server parameters to the client. This will tell the client
2828
/// what server version and what's the encoding we're using.
29+
//
30+
// TODO: Forward these from the server instead of hardcoding.
31+
//
2932
pub async fn server_parameters(stream: &mut TcpStream) -> Result<(), Error> {
3033
let client_encoding = BytesMut::from(&b"client_encoding\0UTF8\0"[..]);
3134
let server_version =
@@ -138,16 +141,20 @@ pub async fn md5_password(
138141
Ok(write_all(stream, message).await?)
139142
}
140143

144+
/// Implements a response to our custom `SET SHARDING KEY` command.
145+
/// This tells the client we're ready for the next query.
141146
pub async fn set_sharding_key(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
142147
let mut res = BytesMut::with_capacity(25);
143148

144149
let set_complete = BytesMut::from(&"SET SHARDING KEY\0"[..]);
145150
let len = (set_complete.len() + 4) as i32;
146151

152+
// CommandComplete
147153
res.put_u8(b'C');
148154
res.put_i32(len);
149155
res.put_slice(&set_complete[..]);
150156

157+
// ReadyForQuery (idle)
151158
res.put_u8(b'Z');
152159
res.put_i32(5);
153160
res.put_u8(b'I');

src/pool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl ConnectionPool {
182182
pub fn is_banned(&self, address: &Address, shard: usize) -> bool {
183183
let mut guard = self.banlist.lock().unwrap();
184184

185-
// Everything is banned, nothig is banned
185+
// Everything is banned = nothing is banned.
186186
if guard[shard].len() == self.databases[shard].len() {
187187
guard[shard].clear();
188188
drop(guard);
@@ -194,8 +194,8 @@ impl ConnectionPool {
194194
match guard[shard].get(address) {
195195
Some(timestamp) => {
196196
let now = chrono::offset::Utc::now().naive_utc();
197+
// Ban expired.
197198
if now.timestamp() - timestamp.timestamp() > self.ban_time {
198-
// 1 minute
199199
guard[shard].remove(address);
200200
false
201201
} else {

0 commit comments

Comments
 (0)