Skip to content

Commit

Permalink
The ability to control memory sizw when doing docker run.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Toffia committed Aug 6, 2018
1 parent 8ec79ba commit 8912cd2
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0;net452</TargetFrameworks>
<VersionPrefix>2.3.1</VersionPrefix>
<VersionPrefix>2.3.2</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker.MsTest</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -19,7 +19,7 @@ Documentation: https://github.com/mariotoffia/FluentDocker
<Copyright>© 2016, 2017, 2018 Mario Toffia</Copyright>
<DefineConstants Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.3.1</Version>
<Version>2.3.2</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand Down
8 changes: 4 additions & 4 deletions Ductus.FluentDocker/Ductus.FluentDocker.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.6;netstandard2.0;net452</TargetFrameworks>
<VersionPrefix>2.3.1</VersionPrefix>
<VersionPrefix>2.3.2</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -16,9 +16,9 @@
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(DefineConstants);COREFX</DefineConstants>
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard2.0' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.3.1.0</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<Version>2.3.1</Version>
<AssemblyVersion>2.3.2.0</AssemblyVersion>
<FileVersion>2.3.2.0</FileVersion>
<Version>2.3.2</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand Down
47 changes: 47 additions & 0 deletions Ductus.FluentDocker/Extensions/ConversionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;

namespace Ductus.FluentDocker.Extensions
{
public static class ConversionExtension
{
/// <summary>
/// Converts a numeric expression combined with an optional suffix to denote
/// b, k, m, g.
/// </summary>
/// <param name="value">The value to be parsed.</param>
/// <param name="unit">An optional custom array of suffix. But has to be among b, k, m, g.</param>
/// <returns>If successful the number, otherwise <see cref="long.MinValue" /> is returned.</returns>
public static long Convert(this string value, params string[] unit)
{
if (null == unit || 0 == unit.Length) unit = new[] {"b", "k", "m", "g"};

if (string.IsNullOrWhiteSpace(value)) return long.MinValue;

var num = value.Substring(0, value.Length - 2);
var u = value.Substring(value.Length - 2, 1).ToLower();

if (char.IsDigit(u[0])) return !long.TryParse(value, out var n) ? long.MinValue : n;

if (!unit.Contains(u)) return long.MinValue;

if (!long.TryParse(num, out var val))
return long.MinValue;

if (u == "b") return val;

switch (u)
{
case "b":
return val;
case "k":
return val * 1024;
case "m":
return val * 1024 * 1024;
case "g":
return val * 1024 * 1024 * 1024;
default:
return long.MinValue;
}
}
}
}
88 changes: 32 additions & 56 deletions Ductus.FluentDocker/Extensions/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ namespace Ductus.FluentDocker.Extensions
{
public static class ModelExtensions
{
public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string value)
public static StringBuilder SizeOptionIfValid(this StringBuilder sb, string option, string value,
long maxSize = long.MaxValue)
{
if (!string.IsNullOrEmpty(value))
{
sb.Append($" {option}{value}");
var num = value.Convert();
if (num == long.MinValue)
return sb;

if (num <= maxSize)
sb.Append($" {option}{value}");
}

return sb;
}

public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string value)
{
if (!string.IsNullOrEmpty(value)) sb.Append($" {option}{value}");

return sb;
}

public static StringBuilder OptionIfExists(this StringBuilder sb, string option, bool enabled)
{
if (enabled)
{
sb.Append($" {option}");
}
if (enabled) sb.Append($" {option}");

return sb;
}

public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string[] values)
{
if (null == values || 0 == values.Length)
{
return sb;
}
if (null == values || 0 == values.Length) return sb;

foreach (var value in values)
{
sb.Append($" {option}{value}");
}
foreach (var value in values) sb.Append($" {option}{value}");

return sb;
}

public static StringBuilder OptionIfExists(this StringBuilder sb, string option, IDictionary<string,string> values)
public static StringBuilder OptionIfExists(this StringBuilder sb, string option, IDictionary<string, string> values)
{
if (null == values || 0 == values.Count)
{
return sb;
}
if (null == values || 0 == values.Count) return sb;

foreach (var value in values)
{
sb.Append($" {option}{value.Key}={value.Value}");
}
foreach (var value in values) sb.Append($" {option}{value.Key}={value.Value}");

return sb;
}
Expand Down Expand Up @@ -92,41 +92,20 @@ public static TemplateString AsTemplate(this string str)

public static ServiceRunningState ToServiceState(this ContainerState state)
{
if (null == state)
{
return ServiceRunningState.Unknown;
}
if (null == state) return ServiceRunningState.Unknown;

if (state.Running)
{
return ServiceRunningState.Running;
}
if (state.Running) return ServiceRunningState.Running;

if (state.Dead)
{
return ServiceRunningState.Stopped;
}
if (state.Dead) return ServiceRunningState.Stopped;

if (state.Restarting)
{
return ServiceRunningState.Starting;
}
if (state.Restarting) return ServiceRunningState.Starting;

if (state.Paused)
{
return ServiceRunningState.Paused;
}
if (state.Paused) return ServiceRunningState.Paused;

var status = state.Status?.ToLower() ?? string.Empty;
if (status == "created")
{
return ServiceRunningState.Stopped;
}
if (status == "created") return ServiceRunningState.Stopped;

if (status == "exited")
{
return ServiceRunningState.Stopped;
}
if (status == "exited") return ServiceRunningState.Stopped;

return ServiceRunningState.Unknown;
}
Expand All @@ -146,15 +125,12 @@ public static string ToDocker(this MountType access)

public static string[] ArrayAddDistinct(this string[] arr, params string[] values)
{
return ArrayAdd(arr,values).Distinct().ToArray();
return ArrayAdd(arr, values).Distinct().ToArray();
}

public static string[] ArrayAdd(this string[] arr, params string[] values)
{
if (null == values || 0 == values.Length)
{
return arr;
}
if (null == values || 0 == values.Length) return arr;

if (null == arr)
{
Expand Down
43 changes: 42 additions & 1 deletion Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,48 @@ public sealed class ContainerCreateParams
/// </remarks>
public string Network { get; set; }

/// <summary>
/// Restart policy for this container.
/// </summary>
/// <remarks>
/// --restart
/// </remarks>
public RestartPolicy RestartPolicy { get; set; }

/// <summary>
/// The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 4m (4
/// megabyte).
/// </summary>
/// <remarks>
/// -m, --memory=
/// </remarks>
public string Memory { get; set; }

/// <summary>
/// The amount of memory this container is allowed to swap to disk.
/// </summary>
/// <remarks>
/// --memory-swap *
/// --memory-swap is a modifier flag that only has meaning if --memory is also set. Using swap allows the container to
/// write excess memory requirements to disk when the container has exhausted all the RAM that is available to it. There
/// is a performance penalty for applications that swap memory to disk often.
/// Its setting can have complicated effects:
/// If --memory-swap is set to a positive integer, then both --memory and --memory-swap must be set. --memory-swap
/// represents the total amount of memory and swap that can be used, and --memory controls the amount used by non-swap
/// memory. So if --memory="300m" and --memory-swap="1g", the container can use 300m of memory and 700m (1g - 300m) swap.
/// If --memory-swap is set to 0, the setting is ignored, and the value is treated as unset.
/// If --memory-swap is set to the same value as --memory, and --memory is set to a positive integer, the container does
/// not have access to swap. See Prevent a container from using swap.
/// If --memory-swap is unset, and --memory is set, the container can use twice as much swap as the --memory setting, if
/// the host container has swap memory configured. For instance, if --memory="300m" and --memory-swap is not set, the
/// container can use 300m of memory and 600m of swap.
/// If --memory-swap is explicitly set to -1, the container is allowed to use unlimited swap, up to the amount available
/// on the host system.
/// Inside the container, tools like free report the host’s available swap, not what’s available inside the container.
/// Don’t rely on the output of free or similar tools to determine whether swap is present.
/// </remarks>
public string MemorySwap { get; set; }// TODO: https://docs.docker.com/config/containers/resource_constraints/#understand-the-risks-of-running-out-of-memory

/// <summary>
/// Renders the argument string from this instance.
/// </summary>
Expand Down Expand Up @@ -258,6 +298,8 @@ public override string ToString()
break;
}

sb.SizeOptionIfValid("--memory=", Memory, 4 * 1024 * 1024 /*4m*/);

return sb.ToString();
}
}
Expand Down Expand Up @@ -309,6 +351,5 @@ public override string ToString()
--tmpfs=[] Mount a tmpfs directory
--ulimit=[] Ulimit options
--uts UTS namespace to use
--network Connect a container to a network
*/
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version: 0.0.0.{build}
version: 2.3.1
version: 2.3.2
skip_non_tags: true
image: Visual Studio 2017
configuration: Release
Expand Down

0 comments on commit 8912cd2

Please sign in to comment.