This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Perf: significant resource optimization around garbage collection, memory and connection utilization #4727
Merged
Merged
Perf: significant resource optimization around garbage collection, memory and connection utilization #4727
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9a8ff8
Perf: significant resource improvements around garbage collection, me…
carlosscastro 0021b9f
Perf: significant resource improvements around garbage collection, me…
carlosscastro e9d3594
Merge branch 'ccastro/memory-connection-optimization' of https://gith…
carlosscastro 9d84c48
Cloud adapter: remove ported memory leaks and GC perf issues
carlosscastro 63f951c
ConnectorClient: Add special constructor to avoid disposing of custom…
carlosscastro ffcdcff
ConnectorClient: Override base uri after initialize so it's not overr…
carlosscastro 9158206
Streaming: ResponseExtensions - add async version of read as string
carlosscastro 8d4bc8e
Remove unused dispose
carlosscastro 5d4037c
Connectorclient: remove unnecessary customHttpClient assignment
carlosscastro 7ea191d
Merge branch 'main' into ccastro/memory-connection-optimization
carlosscastro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Microsoft.Bot.Streaming | ||
|
|
@@ -23,29 +24,37 @@ public static class ReceiveRequestExtensions | |
| /// Otherwise a default instance of type T. | ||
| /// </returns> | ||
| public static T ReadBodyAsJson<T>(this ReceiveRequest request) | ||
| { | ||
| return request.ReadBodyAsJsonAsync<T>().GetAwaiter().GetResult(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serializes the body of this <see cref="ReceiveRequest"/> as JSON. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type to attempt to deserialize the contents of this <see cref="ReceiveRequest"/>'s body into.</typeparam> | ||
| /// <param name="request">The current instance of <see cref="ReceiveRequest"/>.</param> | ||
| /// <returns>On success, an object of type T populated with data serialized from the <see cref="ReceiveRequest"/> body. | ||
| /// Otherwise a default instance of type T. | ||
| /// </returns> | ||
| public static async Task<T> ReadBodyAsJsonAsync<T>(this ReceiveRequest request) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
| { | ||
| // The first stream attached to a ReceiveRequest is always the ReceiveRequest body. | ||
| // Any additional streams must be defined within the body or they will not be | ||
| // attached properly when processing activities. | ||
| var contentStream = request.Streams.FirstOrDefault(); | ||
|
|
||
| /* If the response had no body we have to return a compatible | ||
| * but empty object to avoid throwing exceptions upstream anytime | ||
| * an empty response is received. | ||
| */ | ||
| * but empty object to avoid throwing exceptions upstream anytime | ||
| * an empty response is received. | ||
| */ | ||
| if (contentStream == null) | ||
| { | ||
| return default; | ||
| } | ||
|
|
||
| using (var reader = new StreamReader(contentStream.Stream, Encoding.UTF8)) | ||
| { | ||
| using (var jsonReader = new JsonTextReader(reader)) | ||
| { | ||
| var serializer = JsonSerializer.Create(SerializationSettings.DefaultDeserializationSettings); | ||
| return serializer.Deserialize<T>(jsonReader); | ||
| } | ||
| } | ||
| var bodyString = await request.ReadBodyAsStringAsync().ConfigureAwait(false); | ||
|
|
||
| return JsonConvert.DeserializeObject<T>(bodyString, SerializationSettings.DefaultDeserializationSettings); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -56,17 +65,29 @@ public static T ReadBodyAsJson<T>(this ReceiveRequest request) | |
| /// Otherwise null. | ||
| /// </returns> | ||
| public static string ReadBodyAsString(this ReceiveRequest request) | ||
| { | ||
| return request.ReadBodyAsStringAsync().GetAwaiter().GetResult(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads the body of this <see cref="ReceiveRequest"/> as a string. | ||
| /// </summary> | ||
| /// <param name="request">The current instance of <see cref="ReceiveRequest"/>.</param> | ||
| /// <returns>On success, a string populated with data read from the <see cref="ReceiveRequest"/> body. | ||
| /// Otherwise null. | ||
| /// </returns> | ||
| public static Task<string> ReadBodyAsStringAsync(this ReceiveRequest request) | ||
| { | ||
| var contentStream = request.Streams.FirstOrDefault(); | ||
|
|
||
| if (contentStream == null) | ||
| { | ||
| return string.Empty; | ||
| return Task.FromResult(string.Empty); | ||
| } | ||
|
|
||
| using (var reader = new StreamReader(contentStream.Stream, Encoding.UTF8)) | ||
| { | ||
| return reader.ReadToEnd(); | ||
| return reader.ReadToEndAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.