Skip to content

Commit bf82411

Browse files
committed
Performance considerations article (#2)
1 parent f20adb0 commit bf82411

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

DocFx.Net.Http.WebPush/DocFx.Net.Http.WebPush.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<None Remove="log.txt" />
77
</ItemGroup>
88
<ItemGroup>
9-
<Folder Include="articles\" />
109
<Folder Include="wwwroot\" />
1110
</ItemGroup>
1211
<ItemGroup>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Performance Considerations
2+
3+
A typical application can be processing a very high number of *Push Messages*. Because of that it's important to consider performance best practices.
4+
5+
## Proper Instantiation
6+
7+
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) class is internally holding an instance of `HttpClient` class. As one can read in documentation:
8+
9+
> HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads.
10+
11+
Because of that (in order to avoid Improper Instantiation antipattern) a shared singleton instance of `PushServiceClient` should be created or a pool of reusable instances should be used.
12+
13+
## VAPID Tokens Caching
14+
15+
Generating *VAPID* tokens requires expensive cryptography. The structure of tokens allows for them to be cached per *Audience* (which means by *Push Service*) and *Application Server Keys* pair (for the token expiration period). This library provides such possibility through [`IVapidTokenCache`](../api/Lib.Net.Http.WebPush.Authentication.IVapidTokenCache.html). If an implementation of this interface will be provided to [`VapidAuthentication`](../api/Lib.Net.Http.WebPush.Authentication.VapidAuthentication.html) instance, it will result in tokens being cached.
16+
17+
Below is a sample implementation which uses [ASP.NET Core in-memory caching](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory).
18+
19+
```cs
20+
public class MemoryVapidTokenCache : IVapidTokenCache
21+
{
22+
private readonly IMemoryCache _memoryCache;
23+
24+
public MemoryVapidTokenCache(IMemoryCache memoryCache)
25+
{
26+
_memoryCache = memoryCache;
27+
}
28+
29+
public string Get(string audience)
30+
{
31+
if (!_memoryCache.TryGetValue(audience, out string token))
32+
{
33+
token = null;
34+
}
35+
36+
return token;
37+
}
38+
39+
public void Put(string audience, DateTimeOffset expiration, string token)
40+
{
41+
_memoryCache.Set(audience, token, expiration);
42+
}
43+
}
44+
```
45+
46+
The usage of *Audience* as cache key means that intended context of this interface is a single *Application Server Keys* pair. If application is handling multiple *Application Server Keys* pairs it should provide a separate implementation for every pair and make sure those doesn't clash.

DocFx.Net.Http.WebPush/docfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"files": [
2323
"api/*.yml",
2424
"toc.md",
25-
"index.md"
25+
"index.md",
26+
"articles/performance-considerations.md"
2627
]
2728
}
2829
],

DocFx.Net.Http.WebPush/index.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lib.Net.Http.WebPush
22

3-
Lib.Net.Http.WebPush is a library which provides a [Web Push Protocol](https://tools.ietf.org/html/rfc8030) based client for Push Service. It provides support for [Voluntary Application Server Identification (VAPID) for Web Push](https://tools.ietf.org/html/rfc8292) and [Message Encryption for Web Push](https://tools.ietf.org/html/rfc8291).
3+
Lib.Net.Http.WebPush is a library which provides a [*Web Push Protocol*](https://tools.ietf.org/html/rfc8030) based client for *Push Service*. It provides support for [Voluntary Application Server Identification (*VAPID*) for Web Push](https://tools.ietf.org/html/rfc8292) and [Message Encryption for Web Push](https://tools.ietf.org/html/rfc8291).
44

55
## Installation
66

@@ -14,6 +14,14 @@ PM> Install-Package Lib.Net.Http.WebPush
1414

1515
There is a demo project available [here](https://github.com/tpeczek/Demo.AspNetCore.PushNotifications).
1616

17+
## Additional Resources
18+
19+
There is a "Push Notifications and ASP.NET Core" series which provides a lot of information about *Push API*, *Web Push Protocol* and internals of this library:
20+
21+
- [Push Notifications and ASP.NET Core - Part 1 (Push API)](https://www.tpeczek.com/2017/12/push-notifications-and-aspnet-core-part.html)
22+
- [Push Notifications and ASP.NET Core - Part 2 (Requesting Delivery)](https://www.tpeczek.com/2018/01/push-notifications-and-aspnet-core-part.html)
23+
- [Push Notifications and ASP.NET Core - Part 3 (Replacing Messages & Urgency)](https://www.tpeczek.com/2018/01/push-notifications-and-aspnet-core-part_18.html)
24+
1725
## Donating
1826

1927
My blog and open source projects are result of my passion for software development, but they require a fair amount of my personal time. If you got value from any of the content I create, then I would appreciate your support by [buying me a coffee](https://www.buymeacoffee.com/tpeczek).

DocFx.Net.Http.WebPush/toc.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# [Introduction](index.md)
22

3+
# [Performance Considerations](articles/performance-considerations.md)
4+
35
# [API Reference](api/Lib.Net.Http.WebPush.html)

0 commit comments

Comments
 (0)