Created
July 6, 2020 18:20
-
-
Save alefcarlos/209d4f79006e188fd56bfbca61e96f1a to your computer and use it in GitHub Desktop.
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
public class HsmPool : IDisposable | |
{ | |
internal bool disposedValue; | |
internal SocketPool buffer; | |
private readonly HSMContext settings; | |
static object _initializeBufferLock = new object(); | |
public HsmPool(IOptions<HSMContext> context) | |
{ | |
settings = context.Value; | |
} | |
public int Available => buffer.Available; | |
public IAcquisitonController<Socket> Acquire() | |
{ | |
lock (_initializeBufferLock) | |
{ | |
var endpoint = new IPEndPoint(settings.Pool.IpAddress, settings.Pool.Port); | |
if (buffer == null) | |
{ | |
Func<Socket> getConnection = () => | |
{ | |
var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
s.ReceiveTimeout = settings.Pool.ConnectionTimeout; | |
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); | |
s.NoDelay = true; | |
s.Connect(endpoint); | |
return s; | |
}; | |
buffer = new SocketPool(settings.Pool.MinConnections, settings.Pool.MaxConnections, getConnection); | |
} | |
} | |
return buffer.Acquire(); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!disposedValue) | |
{ | |
if (disposing) | |
buffer.Dispose(); | |
disposedValue = true; | |
} | |
} | |
public void Dispose() | |
{ | |
Dispose(disposing: true); | |
GC.SuppressFinalize(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment