Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a connection pool #1

Merged
merged 1 commit into from
Dec 16, 2022
Merged

Conversation

jgaskins
Copy link
Contributor

@jgaskins jgaskins commented Dec 7, 2022

I noticed in ce72180 there was mention of a desire for a connection pool. The crystal-lang/crystal-db shard is capable of managing pools of arbitrary connection types, including HTTP::Clients — really, anything with a close method).

This commit adds DB::Pool for a thread-safe, configurable connection pool. By default, it spins up a single connection on instantiation and will close all but one connection on checking connections back into the pool. This commit does not add configurability to that connection pool, but it can be added in a followup commit.

DB::Pool is used in several of my own shards that implement network clients:

A couple other minor touches in this commit include:

  • removing a nil check on @callback
  • explicit assignment of that callback in before_request, favoring assignment from within the method signature
  • parsing JSON/Protobuf directly from the socket instead of reading the full body into memory, avoiding having multiple representations of the response in memory at once

These would've been separate PRs, but they would not have been able to be done concurrently due to merge conflicts so I shoveled them all in here. 😄

This commit uses the DB::Pool class for a thread-safe, configurable
connection pool. By default, it spins up a single connection on
instantiation and will close all but one connection on checking
connections back into the pool. This commit does not add configurability
to that connection pool, but it can be added in a followup commit.
@mloughran
Copy link
Owner

Thanks for the contribution and detailed writeup @jgaskins; this looks really helpful and I wasn't aware that DB::Pool could be used for this. Give me a couple of days to try it on our side, then I will merge this PR and release a new shard version :)

Out of interest, are you considering using twirp & crystal at shopify?

@jgaskins
Copy link
Contributor Author

I wasn't aware that DB::Pool could be used for this.

I added it a while back. I needed a generic connection pool that was more featureful than ysbaddaden/pool — elasticity, the ability to evict broken connections, etc. I originally offered to extract DB::Pool to its own shard, but the discussion ended up heading in the direction of just keeping the connection pool in the db shard.

are you considering using twirp & crystal at shopify?

Not to my knowledge. I work on infrastructure/SRE there, so I don't work on a lot of app features. I mostly get paged when things go wrong. 😄

@mloughran mloughran merged commit 16e5519 into mloughran:master Dec 16, 2022
@mloughran
Copy link
Owner

Merged, and released as 0.3.0. Thanks again :)

@jgaskins jgaskins deleted the use-connection-pool branch December 16, 2022 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants