-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect default memory pool for bolt (#758)
* The driver should use it's own pool by default * fix docs * Remove ability to disable pipelined message reader * make pool a readonly field instead of get property
- Loading branch information
1 parent
da06979
commit acb8ff6
Showing
2 changed files
with
111 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
94 changes: 94 additions & 0 deletions
94
Neo4j.Driver/Neo4j.Driver/Internal/Util/PipeReaderMemoryPool.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) "Neo4j" | ||
// Neo4j Sweden AB [https://neo4j.com] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Buffers; | ||
|
||
namespace Neo4j.Driver.Internal; | ||
|
||
/// <summary> | ||
/// Simple memory pool based on the .NET's Pool. | ||
/// </summary> | ||
internal sealed class PipeReaderMemoryPool : MemoryPool<byte> | ||
{ | ||
private readonly int _defaultSize; | ||
private readonly ArrayPool<byte> _pool; | ||
|
||
public PipeReaderMemoryPool(int defaultSize) | ||
{ | ||
_defaultSize = defaultSize; | ||
_pool = ArrayPool<byte>.Create(); | ||
} | ||
|
||
public override int MaxBufferSize => int.MaxValue; | ||
|
||
public override IMemoryOwner<byte> Rent(int minimumBufferSize = -1) | ||
{ | ||
if (minimumBufferSize == -1) | ||
{ | ||
minimumBufferSize = _defaultSize; | ||
} | ||
|
||
if (minimumBufferSize < 0 || minimumBufferSize > MaxBufferSize) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(minimumBufferSize), minimumBufferSize, "requested size is invalid"); | ||
} | ||
|
||
return new PooledMemory(minimumBufferSize, _pool); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
} | ||
|
||
private sealed class PooledMemory : IMemoryOwner<byte> | ||
{ | ||
private byte[] _array; | ||
private readonly ArrayPool<byte> _pool; | ||
|
||
public PooledMemory(int size, ArrayPool<byte> pool) | ||
{ | ||
_array = pool.Rent(size); | ||
_pool = pool; | ||
} | ||
|
||
public Memory<byte> Memory | ||
{ | ||
get | ||
{ | ||
var array = _array; | ||
if (array == null) | ||
{ | ||
throw new ObjectDisposedException(nameof(PooledMemory)); | ||
} | ||
|
||
return new Memory<byte>(array); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
var array = _array; | ||
|
||
if (array == null) | ||
{ | ||
return; | ||
} | ||
|
||
_array = null; | ||
_pool.Return(array); | ||
} | ||
} | ||
} |