News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Is the RestClient class in the API thread safe?
Tags: rest api
Aug 10, 2021 at 2:50pm   •   3 replies  •  0 likes
Brandon Poindexter

As the title asks. Let's say I have the below code, where three tasks are being run concurrently. Let's also say that within each task, they are calling a single, shared RestClient instance and sending requests to the RingCentral API to gather various information.


Is this thread safe to have multiple threads making calls concurrently using the same RestClient instance? I have read that in a plain HttpClient instance, it would be thread safe if the requests were the 'async' version of those requests, ie, GetAsync(), PostAsync, etc. It's not immediately clear if the RingCentral API's RestClient class is working in that manner.


public async Task MainTask()
{
  Task[] tasks = new Task[3];
  tasks[0] = new Task(() => asyncMethod1());
  tasks[1] = new Task(() => asyncMethod2());
  tasks[2] = new Task(() => asyncMethod3());

  Task.WaitAll(tasks);
}
public async Task asyncMethod1()
{
  //do stuff using a shared RestClient instance
}
public async Task asyncMethod2()
{
  //do stuff using a shared RestClient instance
}
public async Task asyncMethod3()
{
  //do stuff using a shared RestClient instance
}





3 Answers
answered on Aug 11, 2021 at 3:18am  

Similar concern in this reference: https://community.ringcentral.com/questions/7550/java-sdk-restclient-thread-safe.html

@Phong Vu if you can clear the query here


 0
answered on Aug 11, 2021 at 8:42am  

I am the author of the RingCentral.Net SDK. Yes I think it is thread safe. Because the SDK does NOT explicitly use threads at all. It always use async/await. And there is no temporary variables shared between threads. (there are variables shared but you are not supposed to update them, such as the clientId, clientSecret, server).

> I have read that in a plain HttpClient instance, it would be thread safe if the requests were the 'async' version of those requests, ie, GetAsync(), PostAsync,

We are using the SendAsync() method: https://github.com/ringcentral/RingCentral.Net/blob/master/RingCentral.Net/RestClient.cs#L86





 1
answered on Aug 11, 2021 at 6:32am  

I'd like an official answer, but I looked at the RestClient source code as posted on GitHub. I'm pretty new to multithreading, but just looking at it, my feeling is that it is not thread safe, and that multiple threads using the same RestClient instance would need to lock the RestClient instance to make it safe. Or, alternatively, use multiple separate RestClient instances, but I'm not sure how the RingCentral API would react to that.


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us