Skip to content

Commit

Permalink
Merge pull request #785 from Cysharp/feature/FixHeartbeatLogAndTimer
Browse files Browse the repository at this point in the history
Fix heartbeat log and timer
  • Loading branch information
mayuki authored Jun 13, 2024
2 parents ac9fe02 + 22253b7 commit 10aaed4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/MagicOnion.Server/Diagnostics/MagicOnionServerLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ static string MethodTypeToString(MethodType type) =>
[LoggerMessage(EventId = 9, Level = LogLevel.Debug, EventName = nameof(InvokeHubBroadcast), Message = nameof(InvokeHubBroadcast) + " groupName:{groupName} size:{size} broadcastGroupCount:{broadcastGroupCount}")]
public static partial void InvokeHubBroadcast(ILogger logger, string groupName, int size, int broadcastGroupCount);

[LoggerMessage(EventId = 10, Level = LogLevel.Debug, EventName = nameof(BeginHeartbeat), Message = nameof(BeginHeartbeat) + " handlerName:{handlerName}")]
public static partial void BeginHeartbeat(ILogger logger, string handlerName);
[LoggerMessage(EventId = 10, Level = LogLevel.Debug, EventName = nameof(BeginHeartbeatTimer), Message = nameof(BeginHeartbeatTimer) + " method:{method}, heartbeatInterval:{heartbeatInterval}, timeoutDuration:{timeoutDuration}")]
public static partial void BeginHeartbeatTimer(ILogger logger, string method, TimeSpan heartbeatInterval, TimeSpan timeoutDuration);

[LoggerMessage(EventId = 11, Level = LogLevel.Debug, EventName = nameof(ShutdownHeartbeat), Message = nameof(ShutdownHeartbeat) + " handlerName:{handlerName}")]
public static partial void ShutdownHeartbeat(ILogger logger, string handlerName);
[LoggerMessage(EventId = 11, Level = LogLevel.Debug, EventName = nameof(ShutdownHeartbeatTimer), Message = nameof(ShutdownHeartbeatTimer) + " method:{method}")]
public static partial void ShutdownHeartbeatTimer(ILogger logger, string method);

[LoggerMessage(EventId = 12, Level = LogLevel.Debug, EventName = nameof(HeartbeatTimedOut), Message = nameof(HeartbeatTimedOut) + " handlerName:{handlerName}, connectionId:{connectionId}")]
public static partial void HeartbeatTimedOut(ILogger logger, string handlerName, Guid connectionId);
[LoggerMessage(EventId = 12, Level = LogLevel.Debug, EventName = nameof(HeartbeatTimedOut), Message = nameof(HeartbeatTimedOut) + " method:{method}, connectionId:{connectionId}")]
public static partial void HeartbeatTimedOut(ILogger logger, string method, Guid connectionId);

[LoggerMessage(EventId = 13, Level = LogLevel.Debug, EventName = nameof(SendHeartbeat), Message = nameof(SendHeartbeat))]
public static partial void SendHeartbeat(ILogger logger);
[LoggerMessage(EventId = 13, Level = LogLevel.Debug, EventName = nameof(SendHeartbeat), Message = nameof(SendHeartbeat) + " method:{method}")]
public static partial void SendHeartbeat(ILogger logger, string method);

[LoggerMessage(EventId = 90, Level = LogLevel.Error, EventName = nameof(ErrorOnServiceMethod), Message = "A service handler throws an exception occurred in {method}")]
public static partial void ErrorOnServiceMethod(ILogger logger, Exception ex, string method);
Expand Down
16 changes: 7 additions & 9 deletions src/MagicOnion.Server/Hubs/StreamingHubHeartbeatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ public StreamingHubHeartbeatHandle Register(IStreamingServiceContext<StreamingHu
var handle = new StreamingHubHeartbeatHandle(this, serviceContext, timeoutDuration);
if (contexts.TryAdd(serviceContext.ContextId, handle))
{
var timerTmp = Volatile.Read(ref timer);
if (Interlocked.Increment(ref registeredCount) == 1)
{
lock (timerGate)
{
if (timerTmp is null && Volatile.Read(ref timer) is null)
if (timer is null)
{
timer = new PeriodicTimer(heartbeatInterval);
MagicOnionServerLog.BeginHeartbeat(this.logger, serviceContext.CallContext.Method);
_ = StartHeartbeatAsync(timer);
MagicOnionServerLog.BeginHeartbeatTimer(this.logger, serviceContext.CallContext.Method, heartbeatInterval, timeoutDuration);
_ = StartHeartbeatAsync(timer, serviceContext.CallContext.Method);
}
}
}
Expand All @@ -113,7 +112,7 @@ public StreamingHubHeartbeatHandle Register(IStreamingServiceContext<StreamingHu
return contexts[serviceContext.ContextId];
}

async Task StartHeartbeatAsync(PeriodicTimer runningTimer)
async Task StartHeartbeatAsync(PeriodicTimer runningTimer, string method)
{
Debug.Assert(runningTimer != null);

Expand All @@ -128,7 +127,7 @@ async Task StartHeartbeatAsync(PeriodicTimer runningTimer)

var payload = StreamingHubPayloadPool.Shared.RentOrCreate(writer.WrittenSpan);

MagicOnionServerLog.SendHeartbeat(this.logger);
MagicOnionServerLog.SendHeartbeat(this.logger, method);
try
{
foreach (var (contextId, handle) in contexts)
Expand All @@ -147,14 +146,13 @@ public void Unregister(IStreamingServiceContext<StreamingHubPayload, StreamingHu
{
if (contexts.TryRemove(serviceContext.ContextId, out _))
{
var timerTmp = Volatile.Read(ref timer);
if (Interlocked.Decrement(ref registeredCount) == 0)
{
lock (timerGate)
{
if (timer is not null && Volatile.Read(ref timer) == timerTmp)
if (Volatile.Read(ref registeredCount) == 0 && timer is not null)
{
MagicOnionServerLog.ShutdownHeartbeat(this.logger, serviceContext.CallContext.Method);
MagicOnionServerLog.ShutdownHeartbeatTimer(this.logger, serviceContext.CallContext.Method);
timer.Dispose();
timer = null;
}
Expand Down

0 comments on commit 10aaed4

Please sign in to comment.