title: Restsharp portable parallel requests are sent successively by one instance of RestClient
body: <p>I use <a href="https://github.com/FubarDevelopment/restsharp.portable" rel="nofollow noreferrer">restsharp.portable 3.1.3</a> in my Xamarin native application. Usually all my requests are sent by one RestClient instance. That's because the creating and disposing of HttpClient are heavy operations for mobile application.
  Sometimes I have some cases when my application should send several requests in parallel. </p>
  
  <p>For example:</p>
  
  <pre><code>private async void button2_Click(object sender, EventArgs e)
      {
          var maxTaskCount = 3;
  
          var client = new RestClient("http://jsonplaceholder.typicode.com/");
  
          var tasks = new List&lt;Task&lt;IRestResponse&gt;&gt;();
          for (var i = 0; i &lt; maxTaskCount; i++)
          {
              var request = new RestRequest("photos");
              request.Method = Method.GET;
  
              var task = client.Execute(request);
              tasks.Add(task);
          }
  
  
          var result = await Task.WhenAll(tasks);
      }
  </code></pre>
  
  <p>While I was profiling my app using proxy(Fiddler or Charles) I found out some bottleneck inside restsharp.portable library. The requests are sent one by one successively. The rest sharp package has <strong>AsyncLock</strong> class with <strong>LockAsync</strong> method for synchronization all requests which are sent by one instance of RestClient. </p>
  
  <pre><code>/// &lt;summary&gt;
      /// Execute the given request
      /// &lt;/summary&gt;
      /// &lt;param name="request"&gt;Request to execute&lt;/param&gt;
      /// &lt;returns&gt;Response returned&lt;/returns&gt;
      public override async Task&lt;IRestResponse&gt; Execute(IRestRequest request)
      {
          using (await _requestGuard.LockAsync(CancellationToken.None))
          {
              using (var response = await ExecuteRequest(request, CancellationToken.None))
              {
                  return await RestResponse.CreateResponse(this, request, response);
              }
          }
      }
  </code></pre>
  
  <p>That's why  I have had the performance issue which was solved by creating own RestClient instance for every requests which should be sent in parallel.</p>
  
  <p>I have no idea why the <a href="https://github.com/fubar-coder" rel="nofollow noreferrer">author</a> of RestSharp portable decided to use AsyncLock inside his package. Maybe because the RestClient keeps some state inside it.</p>
  
  <p>Bellow are the results of my research:</p>
  
  <ol>
  <li>One RestClient instance and three API calls. As we can see all 3 calls are successive.
  <a href="https://i.stack.imgur.com/zORvm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zORvm.jpg" alt="enter image description here"></a></li>
  <li>Three RestClient instances and three API calls(one call for one RestClient instance). As we can see all three calls are parallel.
  <a href="https://i.stack.imgur.com/HwOSM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HwOSM.jpg" alt="enter image description here"></a></li>
  </ol>