Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this web api controller not concurrent?

I have a Web API Controller with the following method inside:

public string Tester()
{
    Thread.Sleep(2000);

    return "OK";
}

When I call it 10 times (Using Fiddler), I expect all 10 calls to return after ~ 2 seconds. However the calls return after 2,4,8...20 seconds, respectively. What is blocking it from running concurrently? How do I fix that?

Do regular Controllers behave the same as web-api controllers?

like image 533
Elad Katz Avatar asked Nov 04 '14 13:11

Elad Katz


People also ask

How many concurrent requests can a Web API handle?

Number of concurrent requests exceeded the limit of 52. Client applications are not limited to sending requests individually in succession. The client may apply parallel programming patterns or various methods to send multiple requests simultaneously.

How many requests can a API handle?

In the API Console, there is a similar quota referred to as Requests per 100 seconds per user. By default, it is set to 100 requests per 100 seconds per user and can be adjusted to a maximum value of 1,000. But the number of requests to the API is restricted to a maximum of 10 requests per second per user.

What is difference between controller and API controller?

The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client. The same because it is MVC platform.


2 Answers

What you describe matches the default behavior of the ASP.NET Session State and can be solved by disabling it in web.config.

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.

Source: ASP.NET Session State Overview

like image 189
sisve Avatar answered Sep 29 '22 01:09

sisve


I don't think I'm answering your question but what I'd like to write here is too big for a comment.

First up, ASP.NET Web API has nothing to do with session state. In fact, if session has to be used with web API, then it needs to be explicitly enabled.

Now, I have the same exact API controller like you and I host it in IIS. If I make concurrent requests, it is running the requests in parallel and not serially. Here is how I tested from Fiddler.

First, I made a GET to the API and waited for 200 status code (#1 in Fiddler left pane). Then, I selected #1 and pressed U to replay the same request unconditionally 9 times. With that I have 10 requests in the left pane (#1 through #10). Then, I selected all 10 of them and pressed R to reissue all the ten requests in parallel. I then selected requests 11 through 20 in the left pane and went to Timeline tab. I see this graph.

PS. I tested the web API running locally, BTW.

enter image description here

Next, I wanted to log the time stamp request as received by the action method. So, I modified action method to return a string like this.

public string Get()
{
    string ts = DateTime.Now.ToString("mm:ss.fff");
    Thread.Sleep(2000);
    return ts;
}

I got the following.

00:43.795
00:43.795
00:45.812
00:44.517
00:43.795
00:43.795
00:45.515
00:43.795
00:43.795
00:43.795

To me this means, all the requests are running in parallel.

like image 22
Badri Avatar answered Sep 29 '22 01:09

Badri