Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: erlef/setup-beam@v1
with:
otp-version: "27.1.2"
gleam-version: "1.6.3"
gleam-version: "1.11.1"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
Expand Down
65 changes: 65 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
# Changelog

## v3.0.0 - 2025-06-14

Bath has been updated to use the new stable versions of `gleam/erlang` and
`gleam/otp`. Bath now follows the conventions laid out in `gleam/otp`, and
creating a resource pool under a supervisor is now much easier.

### Example

The recommended way to start a Bath pool is with the `supervised` function. You
can use this to include the Bath pool in your application's supervision tree.

```gleam
import bath
import fake_db
import gleam/otp/static_supervisor as supervisor

pub fn main() {
let pool_receiver = process.new_subject()

// Define a pool of 10 connections to some fictional database, and create a child
// spec to allow it to be supervised.
let bath_child_spec =
bath.new(fn() { fake_db.get_conn() })
|> bath.size(10)
|> bath.supervised(pool_receiver, 1000)

// Start the pool under a supervisor
let assert Ok(_started) =
supervisor.new(supervisor.OneForOne)
|> supervisor.add(bath_child_spec)
|> supervisor.start

// Receive the pool handle now that it's started
let assert Ok(pool) = process.receive(pool_receiver, 1000)

// Use the pool. Shown here in a block to use `use`.
let assert Ok("Hello!") = {
use conn <- bath.apply(pool, 1000)
// Do stuff with the connection...
"Hello!"
}

// Close the pool.
let assert Ok(_) = bath.shutdown(pool, False, 1000)
}
```

### Behavioural changes

#### Panics

Like the new version of `gleam/erlang`, failing to send messages to the Bath pool will
will now panic rather than returning an error result.

##### Why?

Previously, Bath used the `process.try_call` function that was present in
`gleam/erlang`. However, this had the potential to cause a memory leak if the
process did not return within the provided timeout.

The calling process would cancel its receive operation and continue, and the
process would also continue its operation. When the process replied to the
calling process, that message would be stuck in the caller's queue, never to
be received.

## v2.0.0 - 2024-12-29

- Switch to a builder pattern for pool configuration.
Expand Down
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,37 @@ any value, such as database connections, file handles, or other resources.
## Installation

```sh
gleam add bath@2
gleam add bath@3
```

## Usage

The recommended way to start a Bath pool is with the `supervised` function. You
can use this to include the Bath pool in your application's supervision tree.

```gleam
import bath
import fake_db
import gleam/otp/static_supervisor as supervisor

pub fn main() {
// Create a pool of 10 connections to some fictional database.
let assert Ok(pool) =
bath.new(fn() { fake_db.connect() })
|> bath.with_size(10)
|> bath.with_shutdown(fn(conn) { fake_db.close(conn) })
|> bath.start(1000)
let pool_receiver = process.new_subject()

// Define a pool of 10 connections to some fictional database, and create a child
// spec to allow it to be supervised.
let bath_child_spec =
bath.new(fn() { fake_db.get_conn() })
|> bath.size(10)
|> bath.supervised(pool_receiver, 1000)

// Start the pool under a supervisor
let assert Ok(_started) =
supervisor.new(supervisor.OneForOne)
|> supervisor.add(bath_child_spec)
|> supervisor.start

// Receive the pool handle now that it's started
let assert Ok(pool) = process.receive(pool_receiver, 1000)

// Use the pool. Shown here in a block to use `use`.
let assert Ok("Hello!") = {
Expand All @@ -34,15 +49,20 @@ pub fn main() {
}

// Close the pool.
let assert Ok(_) = bath.shutdown(pool, fn(conn) { fake_db.close(conn) }, 1000)
let assert Ok(_) = bath.shutdown(pool, False, 1000)
}
```

Further documentation can be found at <https://hexdocs.pm/bath>.

## Development

If you've found any bugs, please open an issue on
[GitHub](https://github.com/Pevensie/bath/issues).

The code is reasonably well tested and documented, but PRs to improve either are always
welcome.

```sh
gleam run # Run the project
gleam test # Run the tests
```
8 changes: 4 additions & 4 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "bath"
version = "2.0.0"
version = "3.0.0"
description = "A resource pool for Gleam!"
licences = ["MIT"]
repository = { type = "github", user = "Pevensie", repo = "bath" }
# links = [{ title = "Website", href = "" }]

[dependencies]
gleam_stdlib = ">= 0.51.0 and < 2.0.0"
gleam_otp = ">= 0.16.0 and < 1.0.0"
gleam_erlang = ">= 0.33.0 and < 1.0.0"
gleam_stdlib = ">= 0.60.0 and < 2.0.0"
gleam_otp = ">= 1.0.0 and < 2.0.0"
gleam_erlang = ">= 1.0.0 and < 2.0.0"
gleam_deque = ">= 1.0.0 and < 2.0.0"
logging = ">= 1.3.0 and < 2.0.0"

Expand Down
14 changes: 7 additions & 7 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

packages = [
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_otp", version = "0.16.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "FA0EB761339749B4E82D63016C6A18C4E6662DA05BAB6F1346F9AF2E679E301A" },
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "gleam_erlang", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "7E6A5234F927C4B24F8054AB1E4572206C41F9E6D5C6C02273CB7531E7E5CED0" },
{ name = "gleam_otp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "7020E652D18F9ABAC9C877270B14160519FA0856EE80126231C505D719AD68DA" },
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
]

[requirements]
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
gleam_erlang = { version = ">= 0.33.0 and < 1.0.0" }
gleam_otp = { version = ">= 0.16.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.51.0 and < 2.0.0" }
gleam_erlang = { version = ">= 1.0.0 and < 2.0.0" }
gleam_otp = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.60.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
logging = { version = ">= 1.3.0 and < 2.0.0" }
Loading