Home > Back-end >  Run a method only once in case of concurrent calls and return the same result to all of them
Run a method only once in case of concurrent calls and return the same result to all of them

Time:08-30

I'm writing an ASP.net Core 6 application (but the question is more about C# in gneral) where I have a controller action like this:

[HttpGet]
public async Task<IActionResult> MyAction() {
  var result = await myService.LongOperationAsync();
  return Ok(result);
}

Basically, the action calls a service that needs to do some complex operation and can take a bit of time to respond, up to a minute. Obviously, if in the meantime another request arrives a second run of LongOperationAsync() starts, consuming even more resources.

What I would like to do is redesign this so that the calls to LongOperationAsync() don't run in parallel, but instead wait for the result of the first call and then all return the same result, so that LongOperationAsync() only runs once.

I thought of a few ways to implement this (for example by using a scheduler like Quartz to run the call and then check if a relevant Job is already running before enqueueing another one) but they all require quite a bit of relatively complicated plumbing.

So I guess my questions are:

  • Is there an established design pattern / best practice to implement this scenario? Is it even practical / a good idea?
  • Are there features in the C# language and/or the ASP.net Core framework that facilitate implementing something like this?

CodePudding user response:

As Matthew's answer points out, what you're looking for is an "async lazy". There is no built-in type for this, but it's not that hard to create.

What you should be aware of, though, is that there are a few design tradeoffs in an async lazy type:

  • What context the factory function is run on (the first invoker's context or no context at all). In ASP.NET Core, there isn't a context. So the Task.Factory.StartNew in Stephen Toub's example code is unnecessary overhead.
  • Whether failures should be cached. In the simple AsyncLazy<T> approach, if the factory function fails, then a faulted task is cached indefinitely.
  • When to reset. Again, by default the simple AsyncLazy<T> code never resets; a successful response is also cached indefinitely.

I'm assuming you do want the code to run multiple times; you just want it not to run multiple times concurrently. In that case, you want the async lazy to be reset immediately upon completion, whether successful or failed.

The resetting can be tricky. You want to reset only when it's completed, and only once (i.e., you don't want your reset code to clear the next operation). My go-to for this kind of logic is a unique identifier; I like to use new object() for this.

So, I would start with the Lazy<Task<T>> idea, but wrap it instead of derive, which allows you to do a reset, as such:

public class AsyncLazy<T>
{
  private readonly Func<Task<T>> _factory;
  private readonly object _mutex = new();
  private Lazy<Task<T>> _lazy;
  private object _id;

  public AsyncLazy(Func<Task<T>> factory)
  {
    _factory = factory;
    _lazy = new(_factory);
    _id = new();
  }

  private (object LocalId, Task<T> Task) Start()
  {
    lock (_mutex)
    {
      return (_id, _lazy.Value);
    }
  }

  private void Reset(object localId)
  {
    lock (_mutex)
    {
      if (localId != _id)
        return;
      _lazy = new(_factory);
      _id = new();
    }
  }

  public async Task<T> InvokeAsync()
  {
    var (localId, task) = Start();
    try
    {
      return await task;
    }
    finally
    {
      Reset(localId);
    }
  }
}

CodePudding user response:

You could use an async version of Lazy<T> to do this.

Stephen Toub has posted a sample implementation of LazyAsync<T> here, which I reproduce below:

public class AsyncLazy<T> : Lazy<Task<T>>
{
    public AsyncLazy(Func<T> valueFactory) :
        base(() => Task.Factory.StartNew(valueFactory))
    { }

    public AsyncLazy(Func<Task<T>> taskFactory) :
        base(() => Task.Factory.StartNew(taskFactory).Unwrap())
    { }

    public TaskAwaiter<T> GetAwaiter() { return Value.GetAwaiter(); }
}

You could use it like this:

public class Program
{
    public static async Task Main()
    {
        var test = new Test();

        var task1 = Task.Run(async () => await test.AsyncString());
        var task2 = Task.Run(async () => await test.AsyncString());
        var task3 = Task.Run(async () => await test.AsyncString());

        var results = await Task.WhenAll(task1, task2, task3);

        Console.WriteLine(string.Join(", ", results));
    }
}

public sealed class Test
{
    public async Task<string> AsyncString()
    {
        Console.WriteLine("Started awaiting lazy string.");
        var result = await _lazyString;
        Console.WriteLine("Finished awaiting lazy string.");

        return result;
    }

    static async Task<string> longRunningOperation()
    {
        Console.WriteLine("longRunningOperation() started.");
        await Task.Delay(4000);
        Console.WriteLine("longRunningOperation() finished.");
        return "finished";
    }

    readonly AsyncLazy<string> _lazyString = new (longRunningOperation);
}

If you run this console app, you'll see that longRunningOperation() is only called once, and when it's finished all the tasks waiting on it will complete.

Try it on DotNetFiddle

  • Related