Skip to content

Commit

Permalink
Merge pull request #32 from k9withabone/service-ulimits-unlimited
Browse files Browse the repository at this point in the history
fix(service)!: support unlimited ulimits
  • Loading branch information
k9withabone authored Oct 6, 2024
2 parents dac6176 + 622ba03 commit 6ea9c4e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ sort_commits = "oldest"

[package]
name = "compose_spec"
version = "0.3.0-alpha.1"
version = "0.3.0-alpha.2"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
14 changes: 13 additions & 1 deletion src/common/short_or_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
env_file,
ports::{Port, ShortPort},
volumes::{Mount, ShortVolume},
Build, ConfigOrSecret, Ulimit,
Build, ConfigOrSecret, Limit, Ulimit,
},
Identifier, Include,
};
Expand Down Expand Up @@ -249,6 +249,18 @@ impl<L> From<String> for ShortOrLong<PathBuf, L> {
}
}

impl<T, L> From<Limit<T>> for ShortOrLong<Limit<T>, L> {
fn from(value: Limit<T>) -> Self {
Self::Short(value)
}
}

impl<L> From<u64> for ShortOrLong<Limit<u64>, L> {
fn from(value: u64) -> Self {
Limit::Value(value).into()
}
}

/// `impl<S> From<Type> for ShortOrLong<S, Type>` and `impl<S> From<ShortOrLong<S, Type>> for Type`
macro_rules! impl_long_conversion {
($($t:ty),* $(,)?) => {
Expand Down
6 changes: 6 additions & 0 deletions src/service/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ impl From<u32> for Limit<u32> {
}
}

impl From<u64> for Limit<u64> {
fn from(value: u64) -> Self {
Self::Value(value)
}
}

impl From<ByteValue> for Limit<ByteValue> {
fn from(value: ByteValue) -> Self {
Self::Value(value)
Expand Down
20 changes: 14 additions & 6 deletions src/service/ulimit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use thiserror::Error;

use crate::{common::key_impls, AsShort, Extensions, ShortOrLong};

use super::Limit;

/// Override the default ulimits for a [`Service`](super::Service) container.
///
/// Ulimits are defined as map from a [`Resource`] to either a singe limit ([`u64`]) or a mapping
/// of a soft and hard limit ([`Ulimit`]).
/// Ulimits are defined as map from a [`Resource`] to either a singe limit ([`Limit<u64>`]) or a
/// mapping of a soft and hard limit ([`Ulimit`]).
///
/// [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/05-services.md#ulimits)
pub type Ulimits = IndexMap<Resource, ShortOrLong<u64, Ulimit>>;
pub type Ulimits = IndexMap<Resource, ShortOrLong<Limit<u64>, Ulimit>>;

/// [`Ulimit`] resource name (e.g. "nofile").
///
Expand Down Expand Up @@ -77,10 +79,10 @@ key_impls!(Resource => InvalidResourceError);
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Ulimit {
/// Soft limit.
pub soft: u64,
pub soft: Limit<u64>,

/// Hard limit.
pub hard: u64,
pub hard: Limit<u64>,

/// Extension values, which are (de)serialized via flattening.
///
Expand All @@ -90,7 +92,7 @@ pub struct Ulimit {
}

impl AsShort for Ulimit {
type Short = u64;
type Short = Limit<u64>;

fn as_short(&self) -> Option<&Self::Short> {
let Self {
Expand All @@ -105,6 +107,12 @@ impl AsShort for Ulimit {

impl From<u64> for Ulimit {
fn from(value: u64) -> Self {
Limit::Value(value).into()
}
}

impl From<Limit<u64>> for Ulimit {
fn from(value: Limit<u64>) -> Self {
Self {
soft: value,
hard: value,
Expand Down
8 changes: 8 additions & 0 deletions src/test-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ services:
soft: 100
hard: 200
x-test: test
unlimited: -1
unlimitedlong:
soft: -1
hard: -1
platforms:
- linux/amd64
- linux/arm
Expand Down Expand Up @@ -567,6 +571,10 @@ services:
soft: 100
hard: 200
x-test: test
unlimited: -1
unlimitedlong:
soft: -1
hard: -1
user: user:group
userns_mode: userns_mode
volumes:
Expand Down

0 comments on commit 6ea9c4e

Please sign in to comment.