Reputation: 1923
I have a web service, where data for an identifier can be gotten (e.g. billing information for a customer id). In my client there are multiple threads. These threads call the method at the web service to get the data for the identifier.
Now it happens, that during a request to the web service for e.g CustomerID = 12345
another thread tries to call the web service for the same customer. Now I want to spare the server und to recycle the result of the request.
Therefore I want to wait the second thread until the result of the first request is gotten and return the result also to the second thread.
An important requirement is, that if the second thread wants to get the data after the web request returned, a new request has to be sent. To illustrate my problem, I show a :
Can anybody explain me, how I can achieve this in C#.
Upvotes: 4
Views: 898
Reputation: 1923
Finally I ended with the following solution:
private class RequestHandler
{
private EventWaitHandle _handle;
public RequestHandler(Func<string> request)
{
_handle = new EventWaitHandle(false, EventResetMode.ManualReset);
Start(request);
}
private void Start(Func<string> doRequest)
{
Result = doRequest();
_handle.Set();
}
public void WaitForResponse()
{
_handle.WaitOne();
}
public string Result { get; private set; }
}
private Object _lockObject = new Object();
private Dictionary<string, RequestHandler> _pendingRequests = new Dictionary<string, RequestHandler>();
public string GetCustomer(int id)
{
RequestHandler request;
lock (_lockObject)
{
if (!_pendingRequests.TryGetValue(id, out request))
{
request = new RequestHandler(() => LoadEntity(id));
_pendingRequests[id] = request;
}
}
request.WaitForResponse();
lock (_lockObject)
{
_pendingRequests.Clear();
}
return request.Result;
}
Although it is still possible to have duplicated posts to the server this is a very minimal chance (on thread deletes all the pending requests after the first thread created a request and before a second thread tries to get the same customer data).
Thanks to the inspiration the posts here gave.
Upvotes: 0
Reputation: 156978
I think you can do it like this (pseudocode):
Task
;await
the task (or thread);
If another request comes in, first check the dictionary for a running task. If it is there, await it too. If not, create a new one.
Something like this:
Dictionary<string, Task<string>> tasks = new Dictionary<string, Task<string>>();
public async Task<string> GetCustomer(int id)
{
string taskName = $"get-customer-{id}";
try
{
Task<string> task;
lock (tasks)
{
if (!tasks.TryGetValue(taskName, out task))
{
task = new Task<string>(() => GetCustomerInternal(id));
tasks[taskName] = task;
}
}
string result = await task;
return result;
}
finally
{
lock (tasks)
{
tasks.Remove(taskName);
}
}
}
private string GetCustomerInternal(int id)
{
return "hello";
}
Upvotes: 1
Reputation: 1325
You can do something like this:
private static ConcurrentDictionary<string, Task<string>> s_urlToContents;
public static Task<string> GetContentsAsync(string url)
{
Task<string> contents;
if(!s_urlToContents.TryGetValue(url, out contents))
{
contents = GetContentsAsync(url);
contents.ContinueWith(t => s_urlToContents.TryAdd(url, t); },
TaskContinuationOptions.OnlyOnRanToCompletion |
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
return contents;
}
private static async Task<string> GetContentsAsync(string url)
{
var response = await new HttpClient().GetAsync(url);
return response.EnsureSuccessStatusCode().Content.ReadAsString();
}
This code caches an http request, if another thread wants access to the same request, you just return the already cached task, if it doesn't exist, you spawn the task and cache it for future requests.
Upvotes: 1