Commit 31006371 authored by yangxiaodong's avatar yangxiaodong

add server

parent 982069f7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Cap.Consistency.Server
{
public class ConsistencyServerOptions
{
/// <summary>
/// Enables the Consistency Server options callback to resolve and use services registered by the application during startup.
/// Typically initialized by <see cref="Cap.Consistency.UseConsistency(Action{ConsistencyServerOptions})"/>.
/// </summary>
public IServiceProvider ApplicationServices { get; set; }
/// <summary>
/// The amount of time after the server begins shutting down before connections will be forcefully closed.
/// Kestrel will wait for the duration of the timeout for any ongoing request processing to complete before
/// terminating the connection. No new connections or requests will be accepted during this time.
/// </summary>
/// <remarks>
/// Defaults to 5 seconds.
/// </remarks>
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>
/// The number of libuv I/O threads used to process requests.
/// </summary>
/// <remarks>
/// Defaults to half of <see cref="Environment.ProcessorCount" /> rounded down and clamped between 1 and 16.
/// </remarks>
public int ThreadCount { get; set; } = ProcessorThreadCount;
private static int ProcessorThreadCount {
get {
// Actual core count would be a better number
// rather than logical cores which includes hyper-threaded cores.
// Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving).
var threadCount = Environment.ProcessorCount >> 1;
if (threadCount < 1) {
// Ensure shifted value is at least one
return 1;
}
if (threadCount > 16) {
// Receive Side Scaling RSS Processor count currently maxes out at 16
// would be better to check the NIC's current hardware queues; but xplat...
return 16;
}
return threadCount;
}
}
}
}
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Cap.Consistency.Server
{
public interface IConsumer : IDisposable
{
void Start();
void Start(int count);
void Stop();
ILogger Log { get; set; }
ConsistencyServerOptions ServerOptions { get; set; }
IApplicationLifetime AppLifetime { get; set; }
}
}
using System;
using Microsoft.Extensions.Logging;
namespace Cap.Consistency.Server.Internal.Infrastructure
{
/// <summary>
/// Summary description for KestrelTrace
/// </summary>
public class ConsistencyTrace
{
private static readonly Action<ILogger, string, Exception> _connectionStart;
private static readonly Action<ILogger, string, Exception> _connectionStop;
private static readonly Action<ILogger, string, Exception> _connectionPause;
private static readonly Action<ILogger, string, Exception> _connectionResume;
private static readonly Action<ILogger, string, Exception> _connectionReadFin;
private static readonly Action<ILogger, string, Exception> _connectionWriteFin;
private static readonly Action<ILogger, string, int, Exception> _connectionWroteFin;
private static readonly Action<ILogger, string, Exception> _connectionKeepAlive;
private static readonly Action<ILogger, string, Exception> _connectionDisconnect;
private static readonly Action<ILogger, string, Exception> _applicationError;
private static readonly Action<ILogger, string, Exception> _connectionError;
private static readonly Action<ILogger, string, int, Exception> _connectionDisconnectedWrite;
private static readonly Action<ILogger, string, long, Exception> _connectionHeadResponseBodyWrite;
private static readonly Action<ILogger, Exception> _notAllConnectionsClosedGracefully;
private static readonly Action<ILogger, string, Exception> _connectionReset;
private static readonly Action<ILogger, string, Exception> _requestProcessingError;
protected readonly ILogger _logger;
static ConsistencyTrace()
{
_connectionStart = LoggerMessage.Define<string>(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started.");
_connectionStop = LoggerMessage.Define<string>(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped.");
// ConnectionRead: Reserved: 3
_connectionPause = LoggerMessage.Define<string>(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused.");
_connectionResume = LoggerMessage.Define<string>(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed.");
_connectionReadFin = LoggerMessage.Define<string>(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN.");
_connectionWriteFin = LoggerMessage.Define<string>(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN.");
_connectionWroteFin = LoggerMessage.Define<string, int>(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}"".");
_connectionKeepAlive = LoggerMessage.Define<string>(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response.");
_connectionDisconnect = LoggerMessage.Define<string>(LogLevel.Debug, 10, @"Connection id ""{ConnectionId}"" disconnecting.");
// ConnectionWrite: Reserved: 11
// ConnectionWriteCallback: Reserved: 12
_applicationError = LoggerMessage.Define<string>(LogLevel.Error, 13, @"Connection id ""{ConnectionId}"": An unhandled exception was thrown by the application.");
_connectionError = LoggerMessage.Define<string>(LogLevel.Information, 14, @"Connection id ""{ConnectionId}"" communication error.");
_connectionDisconnectedWrite = LoggerMessage.Define<string, int>(LogLevel.Debug, 15, @"Connection id ""{ConnectionId}"" write of ""{count}"" bytes to disconnected client.");
_notAllConnectionsClosedGracefully = LoggerMessage.Define(LogLevel.Debug, 16, "Some connections failed to close gracefully during server shutdown.");
_connectionHeadResponseBodyWrite = LoggerMessage.Define<string, long>(LogLevel.Debug, 18, @"Connection id ""{ConnectionId}"" write of ""{count}"" body bytes to non-body HEAD response.");
_connectionReset = LoggerMessage.Define<string>(LogLevel.Debug, 19, @"Connection id ""{ConnectionId}"" reset.");
_requestProcessingError = LoggerMessage.Define<string>(LogLevel.Information, 20, @"Connection id ""{ConnectionId}"" request processing ended abnormally.");
}
public ConsistencyTrace(ILogger logger)
{
_logger = logger;
}
public virtual void ConnectionStart(string connectionId)
{
_connectionStart(_logger, connectionId, null);
}
public virtual void ConnectionStop(string connectionId)
{
_connectionStop(_logger, connectionId, null);
}
public virtual void ConnectionRead(string connectionId, int count)
{
}
public virtual void ConnectionPause(string connectionId)
{
_connectionPause(_logger, connectionId, null);
}
public virtual void ConnectionResume(string connectionId)
{
_connectionResume(_logger, connectionId, null);
}
public virtual void ConnectionReadFin(string connectionId)
{
_connectionReadFin(_logger, connectionId, null);
}
public virtual void ConnectionWriteFin(string connectionId)
{
_connectionWriteFin(_logger, connectionId, null);
}
public virtual void ConnectionWroteFin(string connectionId, int status)
{
_connectionWroteFin(_logger, connectionId, status, null);
}
public virtual void ConnectionKeepAlive(string connectionId)
{
_connectionKeepAlive(_logger, connectionId, null);
}
public virtual void ConnectionDisconnect(string connectionId)
{
_connectionDisconnect(_logger, connectionId, null);
}
public virtual void ConnectionWrite(string connectionId, int count)
{
// Don't log for now since this could be *too* verbose.
// Reserved: Event ID 11
}
public virtual void ConnectionWriteCallback(string connectionId, int status)
{
// Don't log for now since this could be *too* verbose.
// Reserved: Event ID 12
}
public virtual void ApplicationError(string connectionId, Exception ex)
{
_applicationError(_logger, connectionId, ex);
}
public virtual void ConnectionError(string connectionId, Exception ex)
{
_connectionError(_logger, connectionId, ex);
}
public virtual void ConnectionDisconnectedWrite(string connectionId, int count, Exception ex)
{
_connectionDisconnectedWrite(_logger, connectionId, count, ex);
}
public virtual void ConnectionHeadResponseBodyWrite(string connectionId, long count)
{
_connectionHeadResponseBodyWrite(_logger, connectionId, count, null);
}
public virtual void NotAllConnectionsClosedGracefully()
{
_notAllConnectionsClosedGracefully(_logger, null);
}
public virtual void ConnectionReset(string connectionId)
{
_connectionReset(_logger, connectionId, null);
}
public virtual void RequestProcessingError(string connectionId, Exception ex)
{
_requestProcessingError(_logger, connectionId, ex);
}
public virtual void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
_logger.Log(logLevel, eventId, state, exception, formatter);
}
public virtual bool IsEnabled(LogLevel logLevel)
{
return _logger.IsEnabled(logLevel);
}
public virtual IDisposable BeginScope<TState>(TState state)
{
return _logger.BeginScope(state);
}
}
}
\ No newline at end of file
using System;
using Microsoft.Extensions.Logging;
namespace Cap.Consistency.Server.Internal.Infrastructure
{
public interface IConsistencyTrace : ILogger
{
void ConnectionStart(string connectionId);
void ConnectionStop(string connectionId);
void ConnectionRead(string connectionId, int count);
void ConnectionPause(string connectionId);
void ConnectionResume(string connectionId);
void ConnectionReadFin(string connectionId);
void ConnectionWriteFin(string connectionId);
void ConnectionWroteFin(string connectionId, int status);
void ConnectionKeepAlive(string connectionId);
void ConnectionDisconnect(string connectionId);
void ConnectionWrite(string connectionId, int count);
void ConnectionWriteCallback(string connectionId, int status);
void ConnectionError(string connectionId, Exception ex);
void ConnectionReset(string connectionId);
void RequestProcessingError(string connectionId, Exception ex);
void ConnectionDisconnectedWrite(string connectionId, int count, Exception ex);
void ConnectionHeadResponseBodyWrite(string connectionId, long count);
void NotAllConnectionsClosedGracefully();
void ApplicationError(string connectionId, Exception ex);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment