Skip to content

Commit

Permalink
Merge pull request #222 from kroymann/dan/fix-sync-load
Browse files Browse the repository at this point in the history
Load source images asynchronously to prevent thread pool exhaustion
  • Loading branch information
JimBobSquarePants authored Feb 7, 2022
2 parents 9a03ec8 + 0160ec3 commit 39c9a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/ImageSharp.Web/FormattedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -91,12 +92,31 @@ public static FormattedImage Load(Configuration configuration, Stream source)
return new FormattedImage(image, format);
}

/// <summary>
/// Loads the specified source.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="source">The source.</param>
/// <returns>A <see cref="Task{FormattedImage}"/> representing the asynchronous operation.</returns>
public static async Task<FormattedImage> LoadAsync(Configuration configuration, Stream source)
{
(Image<Rgba32> image, IImageFormat format) = await ImageSharp.Image.LoadWithFormatAsync<Rgba32>(configuration, source);
return new FormattedImage(image, format);
}

/// <summary>
/// Saves image to the specified destination stream.
/// </summary>
/// <param name="destination">The destination stream.</param>
public void Save(Stream destination) => this.Image.Save(destination, this.encoder);

/// <summary>
/// Saves image to the specified destination stream.
/// </summary>
/// <param name="destination">The destination stream.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SaveAsync(Stream destination) => await this.Image.SaveAsync(destination, this.encoder);

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged resources.
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private async Task ProcessRequestAsync(
}
else
{
using var image = FormattedImage.Load(this.options.Configuration, inStream);
using FormattedImage image = await FormattedImage.LoadAsync(this.options.Configuration, inStream);

image.Process(
this.logger,
Expand Down

0 comments on commit 39c9a8c

Please sign in to comment.