Reputation: 901
Situation: I have a bunch of azure functions that go out and get data from various websites. The domain of the websites changes and each function makes multiple http calls. It is very import that when function for website A does not pull data from website B.
I was reading that it is best to reuse your httpclient/restclient etc.
Link: HttpClient best practices in Azure Functions
But I want advice on how to do this correctly.
public class WebsiteCaller
{
private static RestClient httpClient = new RestSharp.RestClient();
public WebsiteCaller(string domainName)
{
httpClient = new RestClient(domainName);
}
public object GetWebData1(string parameters)
{
//use httpClient with parameters and return data
}
public object GetWebData2(string parameters)
{
//use httpClient with parameters and return data
}
}
public class Manager
{
public void GetDataAndDoStuff()
{
var webConnection = new WebsiteCaller("clientnameDomain.com");
var result1 = webConnection.GetWebData1("name=something");
//Do something with result1
var result2 = webConnection.GetWebData2("name=result1Info");
//Continue making more http calls
}
}
If this function is fired from a queue trigger where potentially hundreds could happen a minute. Do I have to worry about Job1 from queue using the RestClient from Job2. Is there a better way to handle this?
I am using both straight HttpClient for some sections of my app and RestClient for other parts (new and old). That is why I mention both in this question.
This is still in Azure Functions 1.x .net standard
Upvotes: 1
Views: 662
Reputation: 8415
Azure Functions uses an execution model where there can be multiple concurrent executions of multiple functions within the same process. If you have a static variable defined in that process that multiple functions refer to, then absolutely that static variable would be reused across the executions of those functions.
For types like HttpClient, this behavior is often desirable. You'll generally want to reuse the same HttpClient instance across multiple executions of a function, because that allows the underlying connection to be reused, and HttpClient is threadsafe. I'm not familiar with RestClient, you'll need to do your own research on whether its threadsafe.
If you need to configure your client objects with specific settings per each domain you're talking to, then you'll probably want to create a static dictionary of clients, holding one client per domain.
Upvotes: 3