Skip to content

Commit 045c888

Browse files
authored
Merge pull request #41647 from tdykstra/conn
new manage connections article
2 parents 0eb0248 + 3996b66 commit 045c888

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

articles/azure-functions/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
href: functions-run-local.md
143143
- name: Visual Studio development
144144
href: functions-develop-vs.md
145+
- name: Manage connections
146+
href: manage-connections.md
145147
- name: Error handling
146148
href: functions-bindings-error-pages.md
147149
- name: Deploy

articles/azure-functions/functions-best-practices.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ Settings in the hosts file apply across all functions within the app, within a *
116116
Other host configuration options can be found [in the host configuration document](functions-host-json.md).
117117

118118
## Next steps
119+
119120
For more information, see the following resources:
120121

121-
Because Azure Functions uses Azure App Service, you should also be aware of App Service guidelines.
122-
* [Patterns and Practices HTTP Performance Optimizations](https://docs.microsoft.com/azure/architecture/antipatterns/improper-instantiation/)
122+
* [How to manage connections in Azure Functions](manage-connections.md)
123+
* [Azure App Service best practices](../app-service/app-service-best-practices.md)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: How to manage connections in Azure Functions
3+
description: Learn how to avoid performance problems in Azure Functions by using static connection clients.
4+
services: functions
5+
documentationcenter:
6+
author: tdykstra
7+
manager: cfowler
8+
editor: ''
9+
10+
ms.service: functions
11+
ms.workload: na
12+
ms.devlang: na
13+
ms.topic: article
14+
ms.date: 05/18/2018
15+
ms.author: tdykstra
16+
---
17+
18+
# How to manage connections in Azure Functions
19+
20+
Functions in a function app share resources, and among those shared resources are connections — HTTP connections, database connections, and connections to Azure services such as Storage. When many functions are running concurrently it's possible to run out of available connections. This article explains how to code your functions to avoid using more connections than they actually need.
21+
22+
## Connections limit
23+
24+
The number of available connections is limited partly because a function app runs in the [Azure App Service sandbox](https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox). One of the restrictions that the sandbox imposes on your code is a [cap on the number of connections, currently 300](https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#numerical-sandbox-limits). When you reach this limit, the functions runtime creates a log with the following message: `Host thresholds exceeded: Connections`.
25+
26+
Chances of exceeding the limit increase when the [scale controller adds function app instances](functions-scale.md#how-the-consumption-plan-works). Each function app instance can be invoking functions many times at once, and all of these functions are using connections that count toward the 300 limit.
27+
28+
## Use static clients
29+
30+
To avoid holding more connections than necessary, reuse client instances rather than creating new ones with each function invocation. .NET clients like the `HttpClient`, `DocumentClient`, and Azure Storage clients can manage connections if you use a single, static client.
31+
32+
Here are some guidelines to follow when using a service-specific client in an Azure Functions application:
33+
34+
- **DO NOT** create a new client with every function invocation.
35+
- **DO** create a single, static client that can be used by every function invocation.
36+
- **CONSIDER** creating a single, static client in a shared helper class if different functions use the same service.
37+
38+
## HttpClient code example
39+
40+
Here's an example of function code that creates a static `HttpClient`:
41+
42+
```cs
43+
// Create a single, static HttpClient
44+
private static HttpClient httpClient = new HttpClient();
45+
46+
public static async Task Run(string input)
47+
{
48+
var response = await httpClient.GetAsync("http://example.com");
49+
// Rest of function
50+
}
51+
```
52+
53+
A common question about the .NET `HttpClient` is "Should I be disposing my client?" In general, you dispose objects that implement `IDisposable` when you're done using them. But you don't dispose a static client because you aren't done using it when the function ends. You want the static client to live for the duration of your application.
54+
55+
## DocumentClient code example
56+
57+
`DocumentClient` connects to a Cosmos DB instance. The Cosmos DB documentation recommends that you [use a singleton Azure Cosmos DB client for the lifetime of your application](https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#sdk-usage). The following example shows one pattern for doing that in a function.
58+
59+
```cs
60+
#r "Microsoft.Azure.Documents.Client"
61+
using Microsoft.Azure.Documents.Client;
62+
63+
private static Lazy<DocumentClient> lazyClient = new Lazy<DocumentClient>(InitializeDocumentClient);
64+
private static DocumentClient documentClient => lazyClient.Value;
65+
66+
private static DocumentClient InitializeDocumentClient()
67+
{
68+
// Perform any initialization here
69+
var uri = new Uri("example");
70+
var authKey = "authKey";
71+
72+
return new DocumentClient(uri, authKey);
73+
}
74+
75+
public static async Task Run(string input)
76+
{
77+
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("database", "collection");
78+
object document = new { Data = "example" };
79+
await documentClient.UpsertDocumentAsync(collectionUri, document);
80+
81+
// Rest of function
82+
}
83+
```
84+
85+
## Next steps
86+
87+
For more information about why static clients are recommended, see [Improper instantiation antipattern](https://docs.microsoft.com/azure/architecture/antipatterns/improper-instantiation/).
88+
89+
For more Azure Functions performance tips, see [Optimize the performance and reliability of Azure Functions](functions-best-practices.md).
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
> [!TIP]
22
>
3-
> If you plan to use the HTTP or WebHook bindings, plan to avoid port exhaustion that can be caused by improper instantiation of `HttpClient`. For more information, review the article [Improper Instantiation antipattern](https://docs.microsoft.com/azure/architecture/antipatterns/improper-instantiation/).
4-
>
3+
> If you plan to use the HTTP or WebHook bindings, plan to avoid port exhaustion that can be caused by improper instantiation of `HttpClient`. For more information, see [How to manage connections in Azure Functions](../articles/azure-functions/manage-connections.md).

0 commit comments

Comments
 (0)