Skip to content

Allow MemoryStreamFactory to be pluggable #4202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FixedPipelineFactory : IRequestPipelineFactory
public FixedPipelineFactory(IConnectionConfigurationValues connectionSettings, IDateTimeProvider dateTimeProvider)
{
DateTimeProvider = dateTimeProvider;
MemoryStreamFactory = new MemoryStreamFactory();
MemoryStreamFactory = RecyclableMemoryStreamFactory.Default;

Settings = connectionSettings;
Pipeline = Create(Settings, DateTimeProvider, MemoryStreamFactory, new SearchRequestParameters());
Expand All @@ -16,7 +16,7 @@ public FixedPipelineFactory(IConnectionConfigurationValues connectionSettings, I
public IRequestPipeline Pipeline { get; }

private IDateTimeProvider DateTimeProvider { get; }
private MemoryStreamFactory MemoryStreamFactory { get; }
private IMemoryStreamFactory MemoryStreamFactory { get; }
private IConnectionConfigurationValues Settings { get; }

private Transport<IConnectionConfigurationValues> Transport =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public abstract class ConnectionConfiguration<T> : IConnectionConfigurationValue
private bool _sniffOnStartup;
private bool _throwExceptions;
private bool _transferEncodingChunked;
private IMemoryStreamFactory _memoryStreamFactory = RecyclableMemoryStreamFactory.Default;

private string _userAgent = ConnectionConfiguration.DefaultUserAgent;
private Func<HttpMethod, int, bool> _statusCodeToResponseSuccess;
Expand Down Expand Up @@ -209,7 +210,7 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
TimeSpan? IConnectionConfigurationValues.MaxDeadTimeout => _maxDeadTimeout;
int? IConnectionConfigurationValues.MaxRetries => _maxRetries;
TimeSpan? IConnectionConfigurationValues.MaxRetryTimeout => _maxRetryTimeout;
IMemoryStreamFactory IConnectionConfigurationValues.MemoryStreamFactory { get; } = RecyclableMemoryStreamFactory.Default;
IMemoryStreamFactory IConnectionConfigurationValues.MemoryStreamFactory => _memoryStreamFactory;

Func<Node, bool> IConnectionConfigurationValues.NodePredicate => _nodePredicate;
Action<IApiCallDetails> IConnectionConfigurationValues.OnRequestCompleted => _completedRequestHandler;
Expand Down Expand Up @@ -557,6 +558,11 @@ public T SkipDeserializationForStatusCodes(params int[] statusCodes) =>
/// </summary>
public T TransferEncodingChunked(bool transferEncodingChunked = true) => Assign(transferEncodingChunked, (a, v) => a._transferEncodingChunked = v);

/// <summary>
/// The memory stream factory to use, defaults to <see cref="RecyclableMemoryStreamFactory.Default"/>
/// </summary>
public T MemoryStreamFactory(IMemoryStreamFactory memoryStreamFactory) => Assign(memoryStreamFactory, (a, v) => a._memoryStreamFactory = v);

protected virtual void DisposeManagedResources()
{
_connectionPool?.Dispose();
Expand Down
3 changes: 3 additions & 0 deletions src/Elasticsearch.Net/Providers/MemoryStreamFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Elasticsearch.Net
{
// TODO we use this in some places but its no longer clear to me why need to circumvent RecyclableMemoryStream in some cases
/// <summary>
/// A factory for creating memory streams using instances of <see cref="MemoryStream" />
/// </summary>
public class MemoryStreamFactory : IMemoryStreamFactory
{
public static MemoryStreamFactory Default { get; } = new MemoryStreamFactory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should rename these to .Instance in future?

Copy link
Member Author

@Mpdreamz Mpdreamz Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to roll with .Instance 👍


/// <inheritdoc />
public MemoryStream Create() => new MemoryStream();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static byte[] SerializeToBytes<T>(
SerializationFormatting formatting = SerializationFormatting.None
)
{
memoryStreamFactory = memoryStreamFactory ?? RecyclableMemoryStreamFactory.Default;
memoryStreamFactory ??= RecyclableMemoryStreamFactory.Default;
using (var ms = memoryStreamFactory.Create())
{
serializer.Serialize(data, ms, formatting);
Expand Down Expand Up @@ -58,7 +58,7 @@ public static string SerializeToString<T>(
SerializationFormatting formatting = SerializationFormatting.None
)
{
memoryStreamFactory = memoryStreamFactory ?? RecyclableMemoryStreamFactory.Default;
memoryStreamFactory ??= RecyclableMemoryStreamFactory.Default;
using (var ms = memoryStreamFactory.Create())
{
serializer.Serialize(data, ms, formatting);
Expand Down