forked from microsoft/garnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
4,487 additions
and
600 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
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
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
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
38 changes: 38 additions & 0 deletions
38
benchmark/BDN.benchmark/Embedded/EmbeddedNetworkHandler.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,38 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Garnet.common; | ||
using Garnet.networking; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Embedded.server | ||
{ | ||
internal class EmbeddedNetworkHandler : NetworkHandler<GarnetServerEmbedded, EmbeddedNetworkSender> | ||
{ | ||
public EmbeddedNetworkHandler(GarnetServerEmbedded serverHook, EmbeddedNetworkSender networkSender, NetworkBufferSettings networkBufferSettings, LimitedFixedBufferPool networkPool, bool useTLS, IMessageConsumer messageConsumer = null, ILogger logger = null) : base(serverHook, networkSender, networkBufferSettings, networkPool, useTLS, messageConsumer, logger) | ||
{ | ||
} | ||
|
||
public override string RemoteEndpointName => throw new NotImplementedException(); | ||
public override string LocalEndpointName => throw new NotImplementedException(); | ||
public override void Dispose() | ||
{ | ||
DisposeImpl(); | ||
} | ||
|
||
public override bool TryClose() => throw new NotImplementedException(); | ||
|
||
public unsafe void Send(byte[] buffer, byte* bufferPtr, int length) | ||
{ | ||
networkReceiveBuffer = buffer; | ||
networkReceiveBufferPtr = bufferPtr; | ||
OnNetworkReceive(length); | ||
|
||
// We should have consumed the entire buffer | ||
Debug.Assert(networkBytesRead == 0); | ||
Debug.Assert(networkReadHead == 0); | ||
} | ||
} | ||
} |
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
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
101 changes: 101 additions & 0 deletions
101
benchmark/BDN.benchmark/Embedded/GarnetServerEmbedded.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,101 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Net.Security; | ||
using System.Threading; | ||
using Garnet.common; | ||
using Garnet.networking; | ||
using Garnet.server; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Embedded.server | ||
{ | ||
internal class GarnetServerEmbedded : GarnetServerBase, IServerHook | ||
{ | ||
public GarnetServerEmbedded() : base("0.0.0.0", 0, 1 << 10) | ||
{ | ||
} | ||
|
||
public EmbeddedNetworkHandler CreateNetworkHandler(SslClientAuthenticationOptions tlsOptions = null, string remoteEndpointName = null) | ||
{ | ||
var networkSender = new EmbeddedNetworkSender(); | ||
var networkSettings = new NetworkBufferSettings(); | ||
var networkPool = networkSettings.CreateBufferPool(); | ||
EmbeddedNetworkHandler handler = null; | ||
|
||
if (activeHandlerCount >= 0) | ||
{ | ||
var currentActiveHandlerCount = Interlocked.Increment(ref activeHandlerCount); | ||
if (currentActiveHandlerCount > 0) | ||
{ | ||
try | ||
{ | ||
handler = new EmbeddedNetworkHandler(this, networkSender, networkSettings, networkPool, tlsOptions != null); | ||
if (!activeHandlers.TryAdd(handler, default)) | ||
throw new Exception("Unable to add handler to dictionary"); | ||
|
||
handler.Start(tlsOptions, remoteEndpointName); | ||
incr_conn_recv(); | ||
return handler; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.LogError(ex, "Error starting network handler"); | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
handler?.Dispose(); | ||
} | ||
} | ||
else | ||
{ | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
} | ||
} | ||
return handler; | ||
} | ||
|
||
public void DisposeMessageConsumer(INetworkHandler session) | ||
{ | ||
if (activeHandlers.TryRemove(session, out _)) | ||
{ | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
incr_conn_disp(); | ||
try | ||
{ | ||
session.Session?.Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.LogError(ex, "Error disposing RespServerSession"); | ||
} | ||
} | ||
} | ||
|
||
public override void Start() | ||
{ | ||
} | ||
|
||
public bool TryCreateMessageConsumer(Span<byte> bytes, INetworkSender networkSender, out IMessageConsumer session) | ||
{ | ||
session = null; | ||
|
||
// We need at least 4 bytes to determine session | ||
if (bytes.Length < 4) | ||
return false; | ||
|
||
WireFormat protocol = WireFormat.ASCII; | ||
|
||
if (!GetSessionProviders().TryGetValue(protocol, out var provider)) | ||
{ | ||
var input = System.Text.Encoding.ASCII.GetString(bytes); | ||
logger?.LogError("Cannot identify wire protocol {bytes}", input); | ||
throw new Exception($"Unsupported incoming wire format {protocol} {input}"); | ||
} | ||
|
||
if (!AddSession(protocol, ref provider, networkSender, out session)) | ||
throw new Exception($"Unable to add session"); | ||
|
||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.