Skip to content

Commit 7749925

Browse files
committed
separate dispose from close and configure socket to allow rapid connect
1 parent c75d911 commit 7749925

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

benchmark/BDN.benchmark/Embedded/GarnetServerEmbedded.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public override void Start()
110110
{
111111
}
112112

113+
/// <inheritdoc />
114+
public override void Close()
115+
{
116+
}
117+
113118
public bool TryCreateMessageConsumer(Span<byte> bytes, INetworkSender networkSender, out IMessageConsumer session)
114119
{
115120
session = null;

libs/host/GarnetServer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,14 @@ public void Dispose(bool deleteDir = true)
478478

479479
private void InternalDispose()
480480
{
481+
// Phase 1: Stop listening on all servers to free ports immediately.
482+
for (var i = 0; i < servers.Length; i++)
483+
servers[i]?.Close();
484+
485+
// Phase 2: Dispose the provider (storage engine shutdown — may take time).
481486
Provider?.Dispose();
487+
488+
// Phase 3: Drain active handlers and clean up remaining resources.
482489
for (var i = 0; i < servers.Length; i++)
483490
servers[i]?.Dispose();
484491
subscribeBroker?.Dispose();

libs/server/Servers/GarnetServerBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public bool AddSession(WireFormat protocol, ref ISessionProvider provider, INetw
154154
/// <inheritdoc />
155155
public abstract void Start();
156156

157+
/// <inheritdoc />
158+
public abstract void Close();
159+
157160
/// <inheritdoc />
158161
public virtual void Dispose()
159162
{

libs/server/Servers/GarnetServerTcp.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,47 @@ public GarnetServerTcp(
8282
this.unixSocketPath = unixSocketPath;
8383
this.unixSocketPermission = unixSocketPermission;
8484

85-
listenSocket = endpoint switch
85+
if (endpoint is UnixDomainSocketEndPoint unix)
8686
{
87-
UnixDomainSocketEndPoint unix => new Socket(unix.AddressFamily, SocketType.Stream, ProtocolType.Unspecified),
87+
// UDS Initialization & Cleanup
88+
listenSocket = new Socket(unix.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
89+
var socketPath = unix.ToString();
90+
if (File.Exists(socketPath))
91+
{
92+
File.Delete(socketPath);
93+
}
94+
}
95+
else
96+
{
97+
// TCP Initialization & Port Reuse
98+
listenSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
8899

89-
_ => new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
90-
};
100+
// Set reuse BEFORE Bind to handle TIME_WAIT states
101+
listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
102+
}
91103

92104
acceptEventArg = new SocketAsyncEventArgs();
93105
acceptEventArg.Completed += AcceptEventArg_Completed;
94106
}
95107

108+
/// <summary>
109+
/// Stop listening for new connections. Frees the listening port
110+
/// without waiting for active connections to drain.
111+
/// </summary>
112+
public override void Close()
113+
{
114+
listenSocket.Close();
115+
}
116+
96117
/// <summary>
97118
/// Dispose
98119
/// </summary>
99120
public override void Dispose()
100121
{
101-
base.Dispose();
122+
// Close listening socket to free the port and stop accepting new connections.
123+
// This also prevents new connections from arriving while DisposeActiveHandlers drains existing ones.
102124
listenSocket.Dispose();
125+
base.Dispose();
103126
acceptEventArg.UserToken = null;
104127
acceptEventArg.Dispose();
105128
networkPool?.Dispose();

libs/server/Servers/IGarnetServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ public interface IGarnetServer : IDisposable
4646
/// Start server
4747
/// </summary>
4848
public void Start();
49+
50+
/// <summary>
51+
/// Stop listening for new connections. Frees the listening port
52+
/// without waiting for active connections to drain.
53+
/// </summary>
54+
public void Close();
4955
}
5056
}

0 commit comments

Comments
 (0)