Skip to content

Commit

Permalink
improve example + add CI badge
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Jan 11, 2025
1 parent 88b456c commit 50900bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pgDog - PostgreSQL Load Balancer

[![Documentation](https://img.shields.io/badge/documentation-blue?style=flat)](https://pgdog.dev)
![CI](https://github.com/levkk/pgdog/actions/workflows/ci.yml/badge.svg)

pgDog is a PostgreSQL pooler, load balancer and sharding proxy, written in Rust.
Spiritual successor to [pgcat](https://github.com/levkk/pgcat), pgDog comes with a lot of
Expand Down
39 changes: 26 additions & 13 deletions examples/routing-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ use pgdog_plugin::*;
/// Route query.
#[no_mangle]
pub extern "C" fn pgdog_route_query(input: Input) -> Output {
if let Some(query) = input.query() {
let _id = if let Some(id) = query.parameter(0) {
if let Some(id) = id.as_str() {
id.parse::<i64>().ok()
} else if let Ok(id) = id.as_bytes().try_into() {
Some(i64::from_be_bytes(id))
} else {
None
let is_read = input
.query()
.map(|query| query.query().to_lowercase().trim().starts_with("select"))
.unwrap_or(false);

// This is just an example of extracing a parameter from
// the query. In the future, we'll use this to shard transactions.
let _parameter = input.query().map(|query| {
query.parameter(0).map(|parameter| {
let id = parameter.as_str().map(|str| str.parse::<i64>());
match id {
Some(Ok(id)) => id,
_ => i64::from_be_bytes(
parameter
.as_bytes()
.try_into()
.map(|bytes| bytes)
.unwrap_or([0u8; 8]),
),
}
} else {
None
};
}
})
});

Output::skip()
if is_read {
Output::forward(Route::read_any())
} else {
Output::forward(Route::write_any())
}
}

0 comments on commit 50900bc

Please sign in to comment.