/// <summary> /// <see cref="BobNodeClient"/> constructor /// </summary> /// <param name="innerClient">Low-level BobNodeClient</param> /// <param name="keySerializer">Serializer for <typeparamref name="TKey"/> (null for default serializer)</param> /// <param name="keySerializationPoolSize">Size of the Key serialization pool (null - shared pool, 0 or less - pool is disabled, 1 or greater - custom pool with specified size)</param> protected internal BobNodeClient(BobNodeClient innerClient, BobKeySerializer <TKey> keySerializer, int?keySerializationPoolSize) { if (innerClient == null) { throw new ArgumentNullException(nameof(innerClient)); } _innerClient = innerClient; if (keySerializer != null || BobDefaultKeySerializers.TryGetKeySerializer <TKey>(out keySerializer)) { _keySerializer = keySerializer; } else { throw new ArgumentException($"KeySerializer is null and no default key serializer found for key type '{typeof(TKey).Name}'", nameof(keySerializer)); } keySerializationPoolSize = keySerializationPoolSize ?? innerClient.ConnectionParameters.KeySerializationPoolSize; if (keySerializationPoolSize == null) { _keySerializationPool = SharedKeySerializationArrayPools.GetOrCreateSharedPool(_keySerializer); } else if (keySerializationPoolSize.Value > 0) { _keySerializationPool = new ByteArrayPool(_keySerializer.SerializedSize, keySerializationPoolSize.Value); } else { _keySerializationPool = null; } }
public void RentReleaseTest(int poolSize, bool skipLocal) { using (var pool = new ByteArrayPool(sizeof(ulong), poolSize)) { Assert.Equal(sizeof(ulong), pool.ByteArrayLength); Assert.Equal(poolSize, pool.MaxElementCount); HashSet <byte[]> uniqArrays = new HashSet <byte[]>(); for (int i = 0; i < Math.Max(poolSize * 2 + 101, 100000); i++) { byte[] array = pool.Rent(skipLocal: skipLocal); Assert.NotNull(array); uniqArrays.Add(array); pool.Release(array, skipLocal: skipLocal); } if (poolSize > 0) { Assert.True(uniqArrays.Count <= pool.FullCapacity); Assert.True(uniqArrays.Count <= ByteArrayPool.GapSize + 1); } if (poolSize > 0 && !skipLocal) { Assert.Single(uniqArrays); } } }
public GrpcContentServer( ILogger logger, Capabilities serviceCapabilities, ISessionHandler <IContentSession> sessionHandler, IReadOnlyDictionary <string, IContentStore> storesByName, LocalServerConfiguration?localServerConfiguration = null) { Contract.RequiresNotNull(storesByName); _serviceCapabilities = serviceCapabilities; _contentStoreByCacheName = storesByName; _bufferSize = localServerConfiguration?.BufferSizeForGrpcCopies ?? ContentStore.Grpc.GrpcConstants.DefaultBufferSizeBytes; _gzipSizeBarrier = localServerConfiguration?.GzipBarrierSizeForGrpcCopies ?? (_bufferSize * 8); _ongoingPushCountLimit = localServerConfiguration?.ProactivePushCountLimit ?? LocalServerConfiguration.DefaultProactivePushCountLimit; _traceGrpcOperations = localServerConfiguration?.TraceGrpcOperations ?? false; _pool = new ByteArrayPool(_bufferSize); ContentSessionHandler = sessionHandler; _fileSystem = localServerConfiguration?.FileSystem ?? new PassThroughFileSystem(); _workingDirectory = (localServerConfiguration?.DataRootPath ?? _fileSystem.GetTempPath()) / "GrpcContentServer"; GrpcAdapter = new ContentServerAdapter(this); Logger = logger; }
internal static ServerClientEventEntry <TServerClient> Create <T>(DeserializePacketHandler <T> deserialize) { ServerClientEventEntry <TServerClient, T> entry = new ServerClientEventEntry <TServerClient, T>(); entry._deserializeAndRaise = (in Packet packet, IServer <TServerClient> server, TServerClient client, ushort responseID, out object?result) => { if (deserialize(in packet, out T value)) { ByteArrayPool.Return(packet.Buffer); entry.Raise(server, client, value, responseID); // ReSharper disable once HeapView.PossibleBoxingAllocation result = value; return(true); } ByteArrayPool.Return(packet.Buffer); result = null; return(false); }; return(entry); }
public void GetOrCreateTest(Type keyType) { void CoreLogic <TKey>() { BobKeySerializer <TKey> keySerializer = null; Assert.True(BobDefaultKeySerializers.TryGetKeySerializer <TKey>(out keySerializer)); ByteArrayPool pool = SharedKeySerializationArrayPools.GetOrCreateSharedPool(keySerializer); Assert.NotNull(pool); Assert.True(pool.MaxElementCount > 0); Assert.Equal(keySerializer.SerializedSize, pool.ByteArrayLength); var pool2 = SharedKeySerializationArrayPools.GetOrCreateSharedPool(keySerializer); Assert.True(object.ReferenceEquals(pool, pool2)); Assert.True(SharedKeySerializationArrayPools.TryGetSharedPool(keySerializer, out pool2)); Assert.True(object.ReferenceEquals(pool, pool2)); } var referenceDelegate = new Action(CoreLogic <int>); var typedDelegate = (Action)Delegate.CreateDelegate(typeof(Action), referenceDelegate.Method.GetGenericMethodDefinition().MakeGenericMethod(keyType)); typedDelegate(); }
/// <summary> /// Cache for <see cref="GrpcCopyClient"/>. /// </summary> public GrpcCopyClientCache(Context context, GrpcCopyClientCacheConfiguration?configuration = null, IClock?clock = null) { configuration ??= new GrpcCopyClientCacheConfiguration(); _configuration = configuration; _grpcCopyClientBufferPool = new ByteArrayPool(configuration.GrpcCopyClientConfiguration.ClientBufferSizeBytes); switch (_configuration.ResourcePoolVersion) { case GrpcCopyClientCacheConfiguration.PoolVersion.Disabled: break; case GrpcCopyClientCacheConfiguration.PoolVersion.V1: _resourcePool = new ResourcePool <GrpcCopyClientKey, GrpcCopyClient>( context, _configuration.ResourcePoolConfiguration, (key) => new GrpcCopyClient(key, _configuration.GrpcCopyClientConfiguration, sharedBufferPool: _grpcCopyClientBufferPool), clock); break; case GrpcCopyClientCacheConfiguration.PoolVersion.V2: _resourcePoolV2 = new ResourcePoolV2 <GrpcCopyClientKey, GrpcCopyClient>( context, _configuration.ResourcePoolConfiguration, (key) => new GrpcCopyClient(key, _configuration.GrpcCopyClientConfiguration, sharedBufferPool: _grpcCopyClientBufferPool), clock); break; } }
public ThreadAnonymousInnerClassHelper(TestPayloads outerInstance, int numDocs, ByteArrayPool pool, IndexWriter writer, string field) { this.outerInstance = outerInstance; this.numDocs = numDocs; this.pool = pool; this.writer = writer; this.field = field; }
public void Tests() { var pool = new ByteArrayPool(arraySize: 16, poolCapacity: 2); var idx1 = pool.New(); Assert.Equal(1, pool.Count); var buffer1 = pool.GetBuffer(idx1); Assert.Equal(16, buffer1.Length); var idx2 = pool.New(); Assert.Equal(2, pool.Count); var buffer2 = pool.GetBuffer(idx2); Assert.Equal(16, buffer2.Length); var idx3 = pool.New(); // out of capacity Assert.Equal(2, pool.Count); buffer1[0] = 1; buffer1[1] = 2; buffer2[0] = 3; buffer2[1] = 4; Assert.Equal(1, buffer1[0]); Assert.Equal(2, buffer1[1]); Assert.Equal(3, buffer2[0]); Assert.Equal(4, buffer2[1]); pool.Free(idx1); Assert.Equal(1, pool.Count); var idx4 = pool.New(); Assert.Equal(2, pool.Count); var buffer4 = pool.GetBuffer(idx4); buffer4[0] = 5; buffer4[1] = 6; Assert.Equal(3, buffer2[0]); Assert.Equal(4, buffer2[1]); Assert.Equal(5, buffer4[0]); Assert.Equal(6, buffer4[1]); pool.Free(idx2); pool.Free(idx4); Assert.Equal(0, pool.Count); }
/// <inheritdoc /> private protected override Buffer Create(TKey key, int length) { return(new Buffer.Time( ByteArrayPool.Rent(length), length, state => { if (Remove(key)) { ByteArrayPool.Return(((Buffer.Time)state !)._data); }
internal PoolingPayloadTokenStream(TestPayloads outerInstance, ByteArrayPool pool) { this.OuterInstance = outerInstance; this.Pool = pool; Payload = pool.Get(); OuterInstance.GenerateRandomData(Payload); Term = Encoding.UTF8.GetString((byte[])(Array)Payload); First = true; PayloadAtt = AddAttribute <IPayloadAttribute>(); TermAtt = AddAttribute <ICharTermAttribute>(); }
internal PoolingPayloadTokenStream(TestPayloads outerInstance, ByteArrayPool pool) { this.outerInstance = outerInstance; this.pool = pool; payload = pool.Get(); this.outerInstance.GenerateRandomData(payload); term = Encoding.UTF8.GetString(payload); first = true; payloadAtt = AddAttribute <IPayloadAttribute>(); termAtt = AddAttribute <ICharTermAttribute>(); }
internal PoolingPayloadTokenStream(TestPayloads enclosingInstance, ByteArrayPool pool) { InitBlock(enclosingInstance); this.pool = pool; payload = pool.Get(); Enclosing_Instance.GenerateRandomData(payload); term = pool.BytesToString(payload); first = true; payloadAtt = AddAttribute <IPayloadAttribute>(); termAtt = AddAttribute <ITermAttribute>(); }
public void RentReleaseConcurrentAsyncTest(int poolSize, bool skipLocal, int threadCount) { using (var pool = new ByteArrayPool(sizeof(ulong), poolSize)) { Assert.Equal(sizeof(ulong), pool.ByteArrayLength); Assert.Equal(poolSize, pool.MaxElementCount); HashSet <byte[]> uniqArrays = new HashSet <byte[]>(); Barrier bar = new Barrier(threadCount + 1); async Task act() { HashSet <byte[]> uniqArraysLocal = new HashSet <byte[]>(); bar.SignalAndWait(10000); for (int i = 0; i < Math.Max(poolSize * 2 + 101, 100000); i++) { byte[] array = pool.Rent(skipLocal: skipLocal); Assert.NotNull(array); Assert.Equal(0, array[0]); array[0] = 1; await Task.Yield(); uniqArraysLocal.Add(array); array[0] = 0; pool.Release(array, skipLocal: skipLocal); } lock (uniqArrays) { foreach (var item in uniqArraysLocal) { uniqArrays.Add(item); } } } Task[] tasks = new Task[threadCount]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Run(() => act().GetAwaiter().GetResult()); } bar.SignalAndWait(10000); Task.WaitAll(tasks); if (poolSize > threadCount) { Assert.True(uniqArrays.Count <= pool.FullCapacity + threadCount); } } }
private protected void DeserializeData(T arg0, uint commandid, byte[] data, int offset, int length, uint responseid) { switch (commandid) { case CommandID.PING: { SendTo(arg0, CommandID.PING, data, offset, length, responseid); break; } case CommandID.CONNECT: { InvokeClientConnected(arg0); SendTo(arg0, CommandID.CONNECT, data, offset, length, responseid); break; } case CommandID.DISCONNECT: { InvokeClientDisconnect(arg0, DisconnectReason.Graceful); break; } default: { if (_clients.TryGetValue(arg0, out TServerClient sClient)) { if (commandid <= Constants.USER_COMMAND_LIMIT && _dataReceivedCallbacks.TryGetValue( commandid, out ServerClientEventEntry <T, TServerClient> scee)) { sClient.SetLastReceivedPacketTimeStamp(); Packet packet = new Packet(data, offset, length); ThreadPool.QueueUserWorkItem( x => { object res = scee._deserialize(in packet); ByteArrayPool.Return(data); if (res != null) { scee.Raise(this, arg0, res, responseid, sClient); } }); return; } } break; } } ByteArrayPool.Return(data); }
private void SendDataToCallback(IAsyncResult iar) { try { _listener !.EndSendTo(iar); } finally { byte[] send = (byte[])iar.AsyncState !; ByteArrayPool.Return(send); } }
/// <summary> /// Receives. /// </summary> /// <param name="key"> The key. </param> /// <param name="src"> [in,out] If non-null, source for the. </param> /// <param name="chunkLength"> Length of the chunk. </param> /// <param name="chunkOffset"> The chunk offset. </param> /// <param name="length"> The length. </param> /// <returns> /// A byte[] or null. /// </returns> internal unsafe byte[] Receive(int key, byte *src, int chunkLength, int chunkOffset, int length) { if (!_bigDataBuffers.TryGetValue(key, out Buffer bdb)) { bool lockTaken = false; try { _bigDataBufferLock.Enter(ref lockTaken); if (!_bigDataBuffers.TryGetValue(key, out bdb)) { _bigDataBuffers.Add( key, bdb = new Buffer(ByteArrayPool.Rent(length), length)); } } finally { if (lockTaken) { _bigDataBufferLock.Exit(false); } } } fixed(byte *dst2 = bdb.Data) { Mem.Cpy(dst2 + chunkOffset, src, chunkLength); } if (bdb.AddBytes(chunkLength) == 0) { bool lockTaken = false; try { _bigDataBufferLock.Enter(ref lockTaken); _bigDataBuffers.Remove(key); } finally { if (lockTaken) { _bigDataBufferLock.Exit(false); } } return(bdb.Data); } return(null); }
/// <inheritdoc /> protected override Task <bool> SendInternalAsync(IByteStreamHandler byteStreamHandler, ReadOnlySpan <char> rawMessage) { byteStreamHandler.MustNotBeNull(nameof(byteStreamHandler)); // Check for valid message if (rawMessage.Length > _length) { throw new ArgumentException( "Given message too long. " + $"Maximum length: {(_length)}, " + $"given message length: {rawMessage.Length}", nameof(rawMessage)); } // Perform message formatting var sendBuffer = StringBuffer.Acquire(_length); byte[]? bytes = null; try { if (rawMessage.Length > 0) { sendBuffer.Append(rawMessage); } while (sendBuffer.Count < _length) { sendBuffer.Append(_fillSymbol); } sendBuffer.GetInternalData(out var buffer, out var currentCount); var sendMessageByteLength = _encoding.GetByteCount(buffer, 0, currentCount); bytes = ByteArrayPool.Take(sendMessageByteLength); _encoding.GetBytes(buffer, 0, currentCount, bytes, 0); StringBuffer.Release(sendBuffer); sendBuffer = null; return(byteStreamHandler.SendAsync( new ArraySegment <byte>(bytes, 0, sendMessageByteLength))); } finally { if (bytes != null) { ByteArrayPool.Return(bytes); } if (sendBuffer != null) { StringBuffer.Release(sendBuffer); } } }
public void InnerLogicTest(int poolSize) { using (var pool = new ByteArrayPool(sizeof(ulong), poolSize)) { Assert.Equal(sizeof(ulong), pool.ByteArrayLength); Assert.Equal(poolSize, pool.MaxElementCount); Assert.Equal(poolSize + ByteArrayPool.GapSize, pool.FullCapacity); Assert.Null(pool.TryRentGlobal()); for (ulong i = 0; i < (ulong)pool.FullCapacity; i++) { Assert.True(pool.TryReleaseGlobal(BitConverter.GetBytes(i))); } Assert.False(pool.TryReleaseGlobal(BitConverter.GetBytes((ulong)0))); for (ulong i = 0; i < (ulong)poolSize; i++) { Assert.Equal(BitConverter.GetBytes(i), pool.TryRentGlobal()); } Assert.Null(pool.TryRentGlobal()); ulong releaseStart = (ulong)pool.FullCapacity; ulong rentStart = (ulong)poolSize; for (int repeat = 0; repeat < 4; repeat++) { Assert.Null(pool.TryRentGlobal()); for (ulong i = releaseStart; i < releaseStart + (ulong)poolSize; i++) { Assert.True(pool.TryReleaseGlobal(BitConverter.GetBytes(i))); } Assert.False(pool.TryReleaseGlobal(BitConverter.GetBytes((ulong)0))); for (ulong i = rentStart; i < rentStart + (ulong)poolSize; i++) { Assert.Equal(BitConverter.GetBytes(i), pool.TryRentGlobal()); } Assert.Null(pool.TryRentGlobal()); releaseStart += (ulong)poolSize; rentStart += (ulong)poolSize; } } }
public ClientUdpPacketTransport( ILogger logger, IPacketEncryptor packetEncryption, NetworkTransportConfig transportConfig, IPEndPoint remoteEndPoint) { this._packetEncryption = packetEncryption ?? throw new ArgumentNullException(nameof(packetEncryption)); this._remoteEndPoint = remoteEndPoint; this._receiveBuffer = new ReceiveBuffer(transportConfig.MaxPacketSize, transportConfig.ReceivePacketQueueCapacity); this._socket = new ClientUdpSocket(logger, this._receiveBuffer); this._packetSerializationBytePool = new ByteArrayPool(transportConfig.MaxPacketSize, transportConfig.SendPacketQueueCapacity); }
public ServerUdpPacketTransport( ILogger logger, IPacketEncryptor packetEncryption, NetworkTransportConfig transportConfig, UdpServerConfig udpServerConfig) { this._logger = logger ?? throw new ArgumentNullException(nameof(logger)); this._packetEncryption = packetEncryption ?? throw new ArgumentNullException(nameof(packetEncryption)); this._endPoint = udpServerConfig.HostIpEndPoint; this._receiveBuffer = new ReceiveBuffer(transportConfig.MaxPacketSize, transportConfig.ReceivePacketQueueCapacity); this._udpSocket = new ServerUdpSocket(logger, this._receiveBuffer); this._packetSerializationBytePool = new ByteArrayPool(transportConfig.MaxPacketSize, transportConfig.SendPacketQueueCapacity); }
internal GrpcCopyClient(GrpcCopyClientKey key, GrpcCopyClientConfiguration configuration, IClock?clock = null, ByteArrayPool?sharedBufferPool = null) { Key = key; _configuration = configuration; _clock = clock ?? SystemClock.Instance; GrpcEnvironment.WaitUntilInitialized(); _channel = new Channel(key.Host, key.GrpcPort, ChannelCredentials.Insecure, options: GrpcEnvironment.GetClientOptions(_configuration.GrpcCoreClientOptions)); _client = new ContentServer.ContentServerClient(_channel); _bandwidthChecker = new BandwidthChecker(configuration.BandwidthCheckerConfiguration); _pool = sharedBufferPool ?? new ByteArrayPool(_configuration.ClientBufferSizeBytes); }
private void SendDataCallback(IAsyncResult iar) { try { if (_clientSocket.EndSend(iar) <= 0) { Disconnect(DisconnectReason.Error); } } catch (ObjectDisposedException) { Disconnect(DisconnectReason.Aborted); } catch (SocketException) { Disconnect(DisconnectReason.Error); } catch { Disconnect(DisconnectReason.Unspecified); } byte[] send = (byte[])iar.AsyncState; ByteArrayPool.Return(send); }
/// <nodoc /> public GrpcContentServer( ILogger logger, Capabilities serviceCapabilities, ISessionHandler <IContentSession> sessionHandler, Dictionary <string, IContentStore> storesByName, LocalServerConfiguration localServerConfiguration = null) { _logger = logger; _serviceCapabilities = serviceCapabilities; _sessionHandler = sessionHandler; _adapter = new ContentServerAdapter(this); _contentStoreByCacheName = storesByName; _bufferSize = localServerConfiguration?.BufferSizeForGrpcCopies ?? ContentStore.Grpc.CopyConstants.DefaultBufferSize; _gzipSizeBarrier = localServerConfiguration?.GzipBarrierSizeForGrpcCopies ?? _bufferSize * 8; _pool = new ByteArrayPool(_bufferSize); }
public virtual void TestThreadSafety() { rnd = NewRandom(); int numThreads = 5; int numDocs = 50; ByteArrayPool pool = new ByteArrayPool(numThreads, 5); Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED, null); System.String field = "test"; ThreadClass[] ingesters = new ThreadClass[numThreads]; for (int i = 0; i < numThreads; i++) { ingesters[i] = new AnonymousClassThread(numDocs, field, pool, writer, this); ingesters[i].Start(); } for (int i = 0; i < numThreads; i++) { ingesters[i].Join(); } writer.Close(); IndexReader reader = IndexReader.Open(dir, true, null); TermEnum terms = reader.Terms(null); while (terms.Next(null)) { TermPositions tp = reader.TermPositions(terms.Term, null); while (tp.Next(null)) { int freq = tp.Freq; for (int i = 0; i < freq; i++) { tp.NextPosition(null); Assert.AreEqual(pool.BytesToString(tp.GetPayload(new byte[5], 0, null)), terms.Term.Text); } } tp.Close(); } terms.Close(); reader.Close(); Assert.AreEqual(pool.Size(), numThreads); }
public virtual void TestThreadSafety() { const int numThreads = 5; int numDocs = AtLeast(50); ByteArrayPool pool = new ByteArrayPool(numThreads, 5); Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))); const string field = "test"; ThreadJob[] ingesters = new ThreadJob[numThreads]; for (int i = 0; i < numThreads; i++) { ingesters[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, pool, writer, field); ingesters[i].Start(); } for (int i = 0; i < numThreads; i++) { ingesters[i].Join(); } writer.Dispose(); IndexReader reader = DirectoryReader.Open(dir); TermsEnum terms = MultiFields.GetFields(reader).GetTerms(field).GetEnumerator(); IBits liveDocs = MultiFields.GetLiveDocs(reader); DocsAndPositionsEnum tp = null; while (terms.MoveNext()) { string termText = terms.Term.Utf8ToString(); tp = terms.DocsAndPositions(liveDocs, tp); while (tp.NextDoc() != DocIdSetIterator.NO_MORE_DOCS) { int freq = tp.Freq; for (int i = 0; i < freq; i++) { tp.NextPosition(); BytesRef payload = tp.GetPayload(); Assert.AreEqual(termText, payload.Utf8ToString()); } } } reader.Dispose(); dir.Dispose(); Assert.AreEqual(pool.Count, numThreads); }
/// <inheritdoc /> protected override Task <bool> SendInternalAsync(IByteStreamHandler byteStreamHandler, ReadOnlySpan <char> rawMessage) { byteStreamHandler.MustNotBeNull(nameof(byteStreamHandler)); var rawMessageLength = rawMessage.Length; var lengthDigitCount = TcpCommunicatorUtil.GetCountOfDigits(rawMessageLength); var sendBuffer = StringBuffer.Acquire(rawMessageLength + 3 + lengthDigitCount); byte[]? bytes = null; try { sendBuffer.Append(SYMBOL_START); sendBuffer.Append(rawMessageLength, StringView.Empty); sendBuffer.Append(SYMBOL_DELIMITER); if (rawMessage.Length > 0) { sendBuffer.Append(rawMessage); } sendBuffer.Append(SYMBOL_END); sendBuffer.GetInternalData(out var buffer, out var currentCount); var sendMessageByteLength = _encoding.GetByteCount(buffer, 0, currentCount); bytes = ByteArrayPool.Take(sendMessageByteLength); _encoding.GetBytes(buffer, 0, currentCount, bytes, 0); StringBuffer.Release(sendBuffer); sendBuffer = null; return(byteStreamHandler.SendAsync( new ArraySegment <byte>(bytes, 0, sendMessageByteLength))); } finally { if (bytes != null) { ByteArrayPool.Return(bytes); } if (sendBuffer != null) { StringBuffer.Release(sendBuffer); } } }
/// <inheritdoc /> protected override Task <bool> SendInternalAsync(IByteStreamHandler byteStreamHandler, ReadOnlySpan <char> rawMessage) { byteStreamHandler.MustNotBeNull(nameof(byteStreamHandler)); // Check for start- and endsymbols inside the message TcpCommunicatorUtil.EnsureNoEndsymbolsInMessage(rawMessage, _endSymbols); // Perform message formatting var sendBuffer = StringBuffer.Acquire(rawMessage.Length + _endSymbols.Length); byte[]? bytes = null; try { sendBuffer.Append(_startSymbols, 0, _startSymbols.Length); if (rawMessage.Length > 0) { sendBuffer.Append(rawMessage); } sendBuffer.Append(_endSymbols, 0, _endSymbols.Length); sendBuffer.GetInternalData(out var buffer, out var currentCount); var sendMessageByteLength = _encoding.GetByteCount(buffer, 0, currentCount); bytes = ByteArrayPool.Take(sendMessageByteLength); _encoding.GetBytes(buffer, 0, currentCount, bytes, 0); StringBuffer.Release(sendBuffer); sendBuffer = null; return(byteStreamHandler.SendAsync( new ArraySegment <byte>(bytes, 0, sendMessageByteLength))); } finally { if (bytes != null) { ByteArrayPool.Return(bytes); } if (sendBuffer != null) { StringBuffer.Release(sendBuffer); } } }
/// <inheritdoc /> public override SendError SendTo(Socket arg0, uint commandid, byte[] data, int offset, int length, uint responseid) { if (_listener == null) { return(SendError.Invalid); } if ((_state & SEND_FLAG) == SEND_FLAG) { Serialization.Serialization.SerializeTcp( commandid, data, offset, length, responseid, EncryptionMode.None, out byte[] send, out int size); try { SendStateObject state; state.Socket = arg0; state.Buffer = send; arg0.BeginSend( send, 0, size, SocketFlags.None, BeginSendCallback, state); return(SendError.None); } catch (ObjectDisposedException) { InvokeClientDisconnect(arg0, DisconnectReason.Aborted); ByteArrayPool.Return(send); return(SendError.Disposed); } catch (SocketException) { InvokeClientDisconnect(arg0, DisconnectReason.Error); ByteArrayPool.Return(send); return(SendError.Socket); } catch { InvokeClientDisconnect(arg0, DisconnectReason.Unspecified); ByteArrayPool.Return(send); return(SendError.Unknown); } } return(SendError.Invalid); }
private void BeginSendCallback(IAsyncResult iar) { SendStateObject state = (SendStateObject)iar.AsyncState !; try { if (state.Socket.EndSend(iar) <= 0) { InvokeClientDisconnect(state.Socket, DisconnectReason.Unspecified); } } catch { /* IGNORE */ } finally { ByteArrayPool.Return(state.Buffer); } }
public void RentReleasePerfTest(int poolSize, bool skipLocal) { using (var pool = new ByteArrayPool(sizeof(ulong), poolSize)) { Assert.Equal(sizeof(ulong), pool.ByteArrayLength); Assert.Equal(poolSize, pool.MaxElementCount); Stopwatch sw = Stopwatch.StartNew(); const int iterations = 1000000; for (int i = 0; i < iterations; i++) { byte[] array = pool.Rent(skipLocal: skipLocal); pool.Release(array, skipLocal: skipLocal); } sw.Stop(); Output?.WriteLine($"Perf: {iterations * 1000 / sw.ElapsedMilliseconds} op/sec ({sw.ElapsedMilliseconds} ms for {iterations})"); } }
public virtual void TestThreadSafety() { rnd = NewRandom(); int numThreads = 5; int numDocs = 50; ByteArrayPool pool = new ByteArrayPool(numThreads, 5); Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED); System.String field = "test"; SupportClass.ThreadClass[] ingesters = new SupportClass.ThreadClass[numThreads]; for (int i = 0; i < numThreads; i++) { ingesters[i] = new AnonymousClassThread(numDocs, field, pool, writer, this); ingesters[i].Start(); } for (int i = 0; i < numThreads; i++) { ingesters[i].Join(); } writer.Close(); IndexReader reader = IndexReader.Open(dir); TermEnum terms = reader.Terms(); while (terms.Next()) { TermPositions tp = reader.TermPositions(terms.Term()); while (tp.Next()) { int freq = tp.Freq(); for (int i = 0; i < freq; i++) { tp.NextPosition(); Assert.AreEqual(pool.BytesToString(tp.GetPayload(new byte[5], 0)), terms.Term().text_ForNUnit); } } tp.Close(); } terms.Close(); reader.Close(); Assert.AreEqual(pool.Size(), numThreads); }
internal PoolingPayloadTokenStream(ByteArrayPool pool) { this.pool = pool; payload = pool.Get(); Lucene.Net.Index.TestPayloads.GenerateRandomData(payload); first = true; }
public virtual void TestThreadSafety() { const int numThreads = 5; int numDocs = AtLeast(50); ByteArrayPool pool = new ByteArrayPool(numThreads, 5); Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); const string field = "test"; ThreadClass[] ingesters = new ThreadClass[numThreads]; for (int i = 0; i < numThreads; i++) { ingesters[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, pool, writer, field); ingesters[i].Start(); } for (int i = 0; i < numThreads; i++) { ingesters[i].Join(); } writer.Dispose(); IndexReader reader = DirectoryReader.Open(dir); TermsEnum terms = MultiFields.GetFields(reader).Terms(field).Iterator(null); Bits liveDocs = MultiFields.GetLiveDocs(reader); DocsAndPositionsEnum tp = null; while (terms.Next() != null) { string termText = terms.Term().Utf8ToString(); tp = terms.DocsAndPositions(liveDocs, tp); while (tp.NextDoc() != DocIdSetIterator.NO_MORE_DOCS) { int freq = tp.Freq(); for (int i = 0; i < freq; i++) { tp.NextPosition(); BytesRef payload = tp.Payload; Assert.AreEqual(termText, payload.Utf8ToString()); } } } reader.Dispose(); dir.Dispose(); Assert.AreEqual(pool.Size(), numThreads); }
internal PoolingPayloadTokenStream(TestPayloads outerInstance, ByteArrayPool pool) { this.OuterInstance = outerInstance; this.Pool = pool; Payload = pool.Get(); OuterInstance.GenerateRandomData(Payload); Term = Encoding.UTF8.GetString((byte[])(Array)Payload); First = true; PayloadAtt = AddAttribute<IPayloadAttribute>(); TermAtt = AddAttribute<ICharTermAttribute>(); }
internal PoolingPayloadTokenStream(TestPayloads enclosingInstance, ByteArrayPool pool) { InitBlock(enclosingInstance); this.pool = pool; payload = pool.Get(); Enclosing_Instance.GenerateRandomData(payload); term = pool.BytesToString(payload); first = true; payloadAtt = (PayloadAttribute) AddAttribute(typeof(PayloadAttribute)); termAtt = (TermAttribute) AddAttribute(typeof(TermAttribute)); }