Skip to content

Commit ad8ba6a

Browse files
Mpdreamzrusscam
authored andcommitted
fix places where IMemoryStreamFactory was being side stepped (#3650)
fix places where IMemoryStreamFactory was being side stepped and MemoryStreams were being new'ed directly
1 parent c60cc3b commit ad8ba6a

File tree

25 files changed

+80
-69
lines changed

25 files changed

+80
-69
lines changed

src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
166166
TimeSpan? IConnectionConfigurationValues.MaxDeadTimeout => _maxDeadTimeout;
167167
int? IConnectionConfigurationValues.MaxRetries => _maxRetries;
168168
TimeSpan? IConnectionConfigurationValues.MaxRetryTimeout => _maxRetryTimeout;
169-
IMemoryStreamFactory IConnectionConfigurationValues.MemoryStreamFactory { get; } = new RecyclableMemoryStreamFactory();
169+
IMemoryStreamFactory IConnectionConfigurationValues.MemoryStreamFactory { get; } = RecyclableMemoryStreamFactory.Default;
170170

171171
Func<Node, bool> IConnectionConfigurationValues.NodePredicate => _nodePredicate;
172172
Action<IApiCallDetails> IConnectionConfigurationValues.OnRequestCompleted => _completedRequestHandler;

src/Elasticsearch.Net/Providers/RecyclableMemoryStreamFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class RecyclableMemoryStreamFactory : IMemoryStreamFactory
99
{
1010
private readonly RecyclableMemoryStreamManager _manager;
1111

12+
public static RecyclableMemoryStreamFactory Default { get; } = new RecyclableMemoryStreamFactory();
13+
1214
public RecyclableMemoryStreamFactory() =>
1315
_manager = new RecyclableMemoryStreamManager { AggressiveBufferReturn = true };
1416

src/Elasticsearch.Net/Responses/ElasticsearchResponse.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public abstract class ElasticsearchResponseBase : IApiCallDetails, IElasticsearc
3333
/// <inheritdoc cref="IApiCallDetails.Uri"/>
3434
public Uri Uri => ApiCall.Uri;
3535

36+
/// <inheritdoc cref="IApiCallDetails.ConnectionConfiguration"/>
37+
public IConnectionConfigurationValues ConnectionConfiguration => ApiCall.ConnectionConfiguration;
38+
3639
/// <inheritdoc cref="IApiCallDetails.ResponseBodyInBytes"/>
3740
public byte[] ResponseBodyInBytes => ApiCall.ResponseBodyInBytes;
3841

src/Elasticsearch.Net/Responses/HttpDetails/ApiCallDetails.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public string DebugInformation
3636

3737
public Uri Uri { get; set; }
3838

39+
public IConnectionConfigurationValues ConnectionConfiguration { get; set; }
40+
3941
public override string ToString() =>
4042
$"{(Success ? "S" : "Uns")}uccessful low level call on {HttpMethod.GetStringValue()}: {Uri.PathAndQuery}";
4143
}

src/Elasticsearch.Net/Responses/HttpDetails/IApiCallDetails.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public interface IApiCallDetails
2222
/// </summary>
2323
string DebugInformation { get; }
2424

25+
/// <summary>
26+
/// Reference to the connection configuration that yielded this response
27+
/// </summary>
28+
IConnectionConfigurationValues ConnectionConfiguration { get; }
29+
2530
/// <summary>
2631
/// A collection of deprecation warnings returned from Elasticsearch.
2732
/// <para>Used to signal that the request uses an API feature that is marked as deprecated</para>

src/Elasticsearch.Net/Responses/Special/BytesResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool TryGetServerError(out ServerError serverError)
1515
if (Body == null || Body.Length == 0 || ResponseMimeType != RequestData.MimeType)
1616
return false;
1717

18-
using(var stream = new MemoryStream(Body))
18+
using(var stream = ConnectionConfiguration.MemoryStreamFactory.Create(Body))
1919
return ServerError.TryCreate(stream, out serverError);
2020
}
2121

src/Elasticsearch.Net/Responses/Special/StringResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public bool TryGetServerError(out ServerError serverError)
1616
if (string.IsNullOrEmpty(Body) || ResponseMimeType != RequestData.MimeType)
1717
return false;
1818

19-
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(Body)))
19+
using(var stream = ConnectionConfiguration.MemoryStreamFactory.Create(Encoding.UTF8.GetBytes(Body)))
2020
return ServerError.TryCreate(stream, out serverError);
2121
}
2222

src/Elasticsearch.Net/Serialization/IElasticsearchSerializer.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,40 @@ public interface IElasticsearchSerializer
1111

1212
T Deserialize<T>(Stream stream);
1313

14-
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken));
14+
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default);
1515

16-
Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default(CancellationToken));
16+
Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default);
1717

1818
void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented);
1919

2020
Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented,
21-
CancellationToken cancellationToken = default(CancellationToken)
21+
CancellationToken cancellationToken = default
2222
);
2323
}
2424

2525
public static class ElasticsearchSerializerExtensions
2626
{
27-
public static byte[] SerializeToBytes<T>(this IElasticsearchSerializer serializer, T data,
27+
public static byte[] SerializeToBytes<T>(
28+
this IElasticsearchSerializer serializer,
29+
T data,
30+
IMemoryStreamFactory memoryStreamFactory = null,
2831
SerializationFormatting formatting = SerializationFormatting.Indented
2932
)
3033
{
31-
using (var ms = new MemoryStream())
34+
memoryStreamFactory = memoryStreamFactory ?? RecyclableMemoryStreamFactory.Default;
35+
using (var ms = memoryStreamFactory.Create())
3236
{
3337
serializer.Serialize(data, ms, formatting);
3438
return ms.ToArray();
3539
}
3640
}
3741

38-
public static string SerializeToString<T>(this IElasticsearchSerializer serializer, T data,
42+
public static string SerializeToString<T>(
43+
this IElasticsearchSerializer serializer,
44+
T data,
45+
IMemoryStreamFactory memoryStreamFactory = null,
3946
SerializationFormatting formatting = SerializationFormatting.Indented
4047
) =>
41-
serializer.SerializeToBytes(data, formatting).Utf8String();
48+
serializer.SerializeToBytes(data, memoryStreamFactory, formatting).Utf8String();
4249
}
4350
}

src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ ElasticsearchClientException exception
135135
if (response == null)
136136
{
137137
//make sure we copy over the error body in case we disabled direct streaming.
138-
var s = callDetails?.ResponseBodyInBytes == null ? Stream.Null : new MemoryStream(callDetails.ResponseBodyInBytes);
138+
var s = callDetails?.ResponseBodyInBytes == null ? Stream.Null : _memoryStreamFactory.Create(callDetails.ResponseBodyInBytes);
139139
var m = callDetails?.ResponseMimeType ?? RequestData.MimeType;
140140
response = ResponseBuilder.ToResponse<TResponse>(data, exception, callDetails?.HttpStatusCode, null, s, m);
141141
}

src/Elasticsearch.Net/Transport/Pipeline/ResponseBuilder.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ private static ApiCallDetails Initialize(
7070
Uri = requestData.Uri,
7171
HttpMethod = requestData.Method,
7272
DeprecationWarnings = warnings ?? Enumerable.Empty<string>(),
73-
ResponseMimeType = mimeType
73+
ResponseMimeType = mimeType,
74+
ConnectionConfiguration = requestData.ConnectionSettings
7475
};
7576
return details;
7677
}
@@ -90,7 +91,7 @@ private static TResponse SetBody<TResponse>(ApiCallDetails details, RequestData
9091

9192
using (responseStream)
9293
{
93-
if (SetSpecialTypes<TResponse>(requestData, bytes, out var r))
94+
if (SetSpecialTypes<TResponse>(bytes, requestData.MemoryStreamFactory, out var r))
9495
return r;
9596

9697
if (details.HttpStatusCode.HasValue && requestData.SkipDeserializationForStatusCodes.Contains(details.HttpStatusCode.Value))
@@ -121,7 +122,7 @@ private static async Task<TResponse> SetBodyAsync<TResponse>(
121122

122123
using (responseStream)
123124
{
124-
if (SetSpecialTypes<TResponse>(requestData, bytes, out var r)) return r;
125+
if (SetSpecialTypes<TResponse>(bytes, requestData.MemoryStreamFactory, out var r)) return r;
125126

126127
if (details.HttpStatusCode.HasValue && requestData.SkipDeserializationForStatusCodes.Contains(details.HttpStatusCode.Value))
127128
return null;
@@ -136,7 +137,7 @@ private static async Task<TResponse> SetBodyAsync<TResponse>(
136137
}
137138
}
138139

139-
private static bool SetSpecialTypes<TResponse>(RequestData requestData, byte[] bytes, out TResponse cs)
140+
private static bool SetSpecialTypes<TResponse>(byte[] bytes, IMemoryStreamFactory memoryStreamFactory, out TResponse cs)
140141
where TResponse : class, IElasticsearchResponse, new()
141142
{
142143
cs = null;
@@ -151,7 +152,7 @@ private static bool SetSpecialTypes<TResponse>(RequestData requestData, byte[] b
151152
cs = new VoidResponse() as TResponse;
152153
else if (responseType == typeof(DynamicResponse))
153154
{
154-
using (var ms = requestData.MemoryStreamFactory.Create(bytes))
155+
using (var ms = memoryStreamFactory.Create(bytes))
155156
{
156157
var body = LowLevelRequestResponseSerializer.Instance.Deserialize<DynamicBody>(ms);
157158
cs = new DynamicResponse(body) as TResponse;

0 commit comments

Comments
 (0)