Skip to content

Commit

Permalink
OnRequestBegin/OnRequestEnd -> OnMethodInvoke
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Mar 21, 2024
1 parent cf5e50a commit e49b11a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,38 +118,24 @@ static void EmitHelperMethods(StreamingHubClientBuildContext ctx)
if (ctx.EnableStreamingHubDiagnosticHandler)
{
ctx.Writer.AppendLineWithFormat($$"""
async global::System.Threading.Tasks.Task<TResponse> WriteMessageWithResponseDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
global::System.Threading.Tasks.Task<TResponse> WriteMessageWithResponseDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
{
var requestId = global::System.Guid.NewGuid();
diagnosticHandler?.OnRequestBegin(this, requestId, callerMemberName, message, isFireAndForget: false);
try
if (diagnosticHandler is null)
{
var result = await base.WriteMessageWithResponseAsync<TRequest, TResponse>(methodId, message).ConfigureAwait(false);
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, result, default);
return result;
}
catch (global::System.Exception e)
{
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, default(TResponse), e);
throw;
return base.WriteMessageWithResponseAsync<TRequest, TResponse>(methodId, message);
}
return diagnosticHandler.OnMethodInvoke(this, methodId, callerMemberName, message, isFireAndForget: true, base.WriteMessageWithResponseAsync<TRequest, TResponse>);
}
async global::System.Threading.Tasks.Task<TResponse> WriteMessageFireAndForgetDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
global::System.Threading.Tasks.Task<TResponse> WriteMessageFireAndForgetDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
{
var requestId = global::System.Guid.NewGuid();
diagnosticHandler?.OnRequestBegin(this, requestId, callerMemberName, message, isFireAndForget: true);
try
if (diagnosticHandler is null)
{
var result = await base.WriteMessageFireAndForgetAsync<TRequest, TResponse>(methodId, message).ConfigureAwait(false);
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, result, default);
return result;
}
catch (global::System.Exception e)
{
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, default(TResponse), e);
throw;
return base.WriteMessageFireAndForgetAsync<TRequest, TResponse>(methodId, message);
}
return diagnosticHandler.OnMethodInvoke(this, methodId, callerMemberName, message, isFireAndForget: true, base.WriteMessageFireAndForgetAsync<TRequest, TResponse>);
}
""");
ctx.Writer.AppendLine();
Expand Down Expand Up @@ -193,7 +179,7 @@ class FireAndForgetClient : {{ctx.Hub.ServiceType.FullName}}
public FireAndForgetClient({{ctx.Hub.GetClientFullName()}} parent)
=> this.parent = parent;
public {{ctx.Hub.ServiceType.FullName}} FireAndForget() => this;
public global::System.Threading.Tasks.Task DisposeAsync() => throw new global::System.NotSupportedException();
public global::System.Threading.Tasks.Task WaitForDisconnect() => throw new global::System.NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace MagicOnion.Client
{
Expand All @@ -7,29 +8,21 @@ namespace MagicOnion.Client
/// </summary>
public interface IStreamingHubDiagnosticHandler
{
public delegate Task<TResponse> InvokeMethodDelegate<TRequest, TResponse>(int methodId, TRequest value);

/// <summary>
/// The callback method at the beginning of a Hub method request. This API may change in the future.
/// </summary>
/// <typeparam name="THub"></typeparam>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="hubInstance"></param>
/// <param name="requestId"></param>
/// <param name="methodId"></param>
/// <param name="methodName"></param>
/// <param name="request"></param>
/// <param name="isFireAndForget"></param>
void OnRequestBegin<THub, TRequest>(THub hubInstance, Guid requestId, string methodName, TRequest request, bool isFireAndForget);

/// <summary>
/// [Preview] The callback method at the end of a Hub method request. This API may change in the future.
/// </summary>
/// <typeparam name="THub"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="hubInstance"></param>
/// <param name="requestId"></param>
/// <param name="methodName"></param>
/// <param name="response"></param>
/// <param name="exception"></param>
void OnRequestEnd<THub, TResponse>(THub hubInstance, Guid requestId, string methodName, TResponse response, Exception? exception);
/// <param name="invokeMethod"></param>
Task<TResponse> OnMethodInvoke<THub, TRequest, TResponse>(THub hubInstance, int methodId, string methodName, TRequest request, bool isFireAndForget, InvokeMethodDelegate<TRequest, TResponse> invokeMethod);

/// <summary>
/// [Preview] The callback method when a method of HubReceiver is invoked. This API may change in the future.
Expand Down
21 changes: 7 additions & 14 deletions src/MagicOnion.Client/IStreamingHubDiagnosticHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace MagicOnion.Client
{
Expand All @@ -7,29 +8,21 @@ namespace MagicOnion.Client
/// </summary>
public interface IStreamingHubDiagnosticHandler
{
public delegate Task<TResponse> InvokeMethodDelegate<TRequest, TResponse>(int methodId, TRequest value);

/// <summary>
/// The callback method at the beginning of a Hub method request. This API may change in the future.
/// </summary>
/// <typeparam name="THub"></typeparam>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="hubInstance"></param>
/// <param name="requestId"></param>
/// <param name="methodId"></param>
/// <param name="methodName"></param>
/// <param name="request"></param>
/// <param name="isFireAndForget"></param>
void OnRequestBegin<THub, TRequest>(THub hubInstance, Guid requestId, string methodName, TRequest request, bool isFireAndForget);

/// <summary>
/// [Preview] The callback method at the end of a Hub method request. This API may change in the future.
/// </summary>
/// <typeparam name="THub"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="hubInstance"></param>
/// <param name="requestId"></param>
/// <param name="methodName"></param>
/// <param name="response"></param>
/// <param name="exception"></param>
void OnRequestEnd<THub, TResponse>(THub hubInstance, Guid requestId, string methodName, TResponse response, Exception? exception);
/// <param name="invokeMethod"></param>
Task<TResponse> OnMethodInvoke<THub, TRequest, TResponse>(THub hubInstance, int methodId, string methodName, TRequest request, bool isFireAndForget, InvokeMethodDelegate<TRequest, TResponse> invokeMethod);

/// <summary>
/// [Preview] The callback method when a method of HubReceiver is invoked. This API may change in the future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,24 @@ public TempProject_MyHubClient(global::Grpc.Core.CallInvoker callInvoker, global
this.diagnosticHandler = diagnosticHandler;
}

async global::System.Threading.Tasks.Task<TResponse> WriteMessageWithResponseDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
global::System.Threading.Tasks.Task<TResponse> WriteMessageWithResponseDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
{
var requestId = global::System.Guid.NewGuid();
diagnosticHandler?.OnRequestBegin(this, requestId, callerMemberName, message, isFireAndForget: false);
try
if (diagnosticHandler is null)
{
var result = await base.WriteMessageWithResponseAsync<TRequest, TResponse>(methodId, message).ConfigureAwait(false);
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, result, default);
return result;
}
catch (global::System.Exception e)
{
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, default(TResponse), e);
throw;
return base.WriteMessageWithResponseAsync<TRequest, TResponse>(methodId, message);
}

return diagnosticHandler.OnMethodInvoke(this, methodId, callerMemberName, message, isFireAndForget: true, base.WriteMessageWithResponseAsync<TRequest, TResponse>);
}
async global::System.Threading.Tasks.Task<TResponse> WriteMessageFireAndForgetDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)

global::System.Threading.Tasks.Task<TResponse> WriteMessageFireAndForgetDiagnosticAsync<TRequest, TResponse>(int methodId, TRequest message, [global::System.Runtime.CompilerServices.CallerMemberName] string callerMemberName = default!)
{
var requestId = global::System.Guid.NewGuid();
diagnosticHandler?.OnRequestBegin(this, requestId, callerMemberName, message, isFireAndForget: true);
try
if (diagnosticHandler is null)
{
var result = await base.WriteMessageFireAndForgetAsync<TRequest, TResponse>(methodId, message).ConfigureAwait(false);
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, result, default);
return result;
}
catch (global::System.Exception e)
{
diagnosticHandler?.OnRequestEnd(this, requestId, callerMemberName, default(TResponse), e);
throw;
return base.WriteMessageFireAndForgetAsync<TRequest, TResponse>(methodId, message);
}

return diagnosticHandler.OnMethodInvoke(this, methodId, callerMemberName, message, isFireAndForget: true, base.WriteMessageFireAndForgetAsync<TRequest, TResponse>);
}

public global::System.Threading.Tasks.Task MethodA()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public async Task Request_Response()

Assert.Equal(nameof(IStreamingHubTestHub.Parameter_Many), beginEvent.MethodName);
Assert.Equal(nameof(IStreamingHubTestHub.Parameter_Many), endEvent.MethodName);
Assert.Equal(beginEvent.RequestId, endEvent.RequestId);
Assert.Equal(new DynamicArgumentTuple<int, string, bool>(12345, "Hello✨", true), beginEvent.Request);
Assert.Equal(result, endEvent.Response);
}
Expand Down Expand Up @@ -69,7 +68,6 @@ public async Task Request_Parameterless_Response()

Assert.Equal(nameof(IStreamingHubTestHub.Parameter_Zero), beginEvent.MethodName);
Assert.Equal(nameof(IStreamingHubTestHub.Parameter_Zero), endEvent.MethodName);
Assert.Equal(beginEvent.RequestId, endEvent.RequestId);
Assert.Equal(Nil.Default, beginEvent.Request);
Assert.Equal(result, endEvent.Response);
}
Expand Down Expand Up @@ -100,7 +98,6 @@ public async Task Request_Response_Void()

Assert.Equal(nameof(IStreamingHubTestHub.NoReturn_Parameter_Many), beginEvent.MethodName);
Assert.Equal(nameof(IStreamingHubTestHub.NoReturn_Parameter_Many), endEvent.MethodName);
Assert.Equal(beginEvent.RequestId, endEvent.RequestId);
Assert.Equal(new DynamicArgumentTuple<int, string, bool>(12345, "Hello✨", true), beginEvent.Request);
Assert.Equal(Nil.Default, endEvent.Response);
}
Expand Down Expand Up @@ -131,9 +128,8 @@ public async Task Request_Throw()

Assert.Equal(nameof(IStreamingHubTestHub.Throw), beginEvent.MethodName);
Assert.Equal(nameof(IStreamingHubTestHub.Throw), endEvent.MethodName);
Assert.Equal(beginEvent.RequestId, endEvent.RequestId);
Assert.Equal(Nil.Default, beginEvent.Request);
Assert.Equal(Nil.Default, endEvent.Response);
Assert.Null(endEvent.Response);
Assert.Equal(ex, endEvent.Exception);
}

Expand Down Expand Up @@ -168,7 +164,6 @@ public async Task Receiver()

Assert.Equal(nameof(IStreamingHubTestHub.CallReceiver_Parameter_Many), beginEvent.MethodName);
Assert.Equal(nameof(IStreamingHubTestHub.CallReceiver_Parameter_Many), endEvent.MethodName);
Assert.Equal(beginEvent.RequestId, endEvent.RequestId);
Assert.Equal(new DynamicArgumentTuple<int, string, bool>(12345, "Hello✨", true), beginEvent.Request);
Assert.Null(beginEvent.Response);

Expand All @@ -185,21 +180,27 @@ public enum EventType
OnBroadcastEvent,
}

public List<(EventType EventType, object HubInstance, Guid RequestId, string MethodName, object? Request, object? Response, Exception? Exception)> Events { get; } = new ();

public void OnRequestBegin<THub, TRequest>(THub hubInstance, Guid requestId, string methodName, TRequest request, bool isFireAndForget)
{
Events.Add((EventType.OnRequestBegin, hubInstance!, requestId, methodName, request, default, default));
}
public List<(EventType EventType, object HubInstance, string MethodName, object? Request, object? Response, Exception? Exception)> Events { get; } = new ();

public void OnRequestEnd<THub, TResponse>(THub hubInstance, Guid requestId, string methodName, TResponse response, Exception? exception)
public async Task<TResponse> OnMethodInvoke<THub, TRequest, TResponse>(THub hubInstance, int methodId, string methodName, TRequest request, bool isFireAndForget, IStreamingHubDiagnosticHandler.InvokeMethodDelegate<TRequest, TResponse> invokeMethod)
{
Events.Add((EventType.OnRequestEnd, hubInstance!, requestId, methodName, default, response, exception));
Events.Add((EventType.OnRequestBegin, hubInstance!, methodName, request, default, default));
try
{
var result = await invokeMethod(methodId, request).ConfigureAwait(false);
Events.Add((EventType.OnRequestEnd, hubInstance!, methodName, default, result, default));
return result;
}
catch (Exception e)
{
Events.Add((EventType.OnRequestEnd, hubInstance!, methodName, default, default, e));
throw;
}
}

public void OnBroadcastEvent<THub, T>(THub hubInstance, string methodName, T value)
{
Events.Add((EventType.OnBroadcastEvent, hubInstance!, Guid.Empty, methodName, default, value, default));
Events.Add((EventType.OnBroadcastEvent, hubInstance!, methodName, default, value, default));
}
}
}
Expand Down

0 comments on commit e49b11a

Please sign in to comment.