-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d784c17
commit 1b9ad7a
Showing
6 changed files
with
124 additions
and
44 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentWorkerHostBuilderExtension.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// GrpcAgentWorkerHostBuilderExtension.cs | ||
using System.Diagnostics; | ||
using Grpc.Core; | ||
using Grpc.Net.Client.Configuration; | ||
using Microsoft.AutoGen.Contracts; | ||
using Microsoft.AutoGen.Protobuf; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
namespace Microsoft.AutoGen.Core.Grpc; | ||
|
||
public static class GrpcAgentWorkerHostBuilderExtensions | ||
{ | ||
private const string _defaultAgentServiceAddress = "https://localhost:53071"; | ||
|
||
// TODO: How do we ensure AddGrpcAgentWorker and UseInProcessRuntime are mutually exclusive? | ||
public static AgentsAppBuilder AddGrpcAgentWorker(this AgentsAppBuilder builder, string? agentServiceAddress = null) | ||
{ | ||
builder.Services.AddGrpcClient<AgentRpc.AgentRpcClient>(options => | ||
{ | ||
options.Address = new Uri(agentServiceAddress ?? builder.Configuration["AGENT_HOST"] ?? _defaultAgentServiceAddress); | ||
options.ChannelOptionsActions.Add(channelOptions => | ||
{ | ||
var loggerFactory = new LoggerFactory(); | ||
if (Debugger.IsAttached) | ||
{ | ||
channelOptions.HttpHandler = new SocketsHttpHandler | ||
{ | ||
EnableMultipleHttp2Connections = false, | ||
KeepAlivePingDelay = TimeSpan.FromSeconds(200), | ||
KeepAlivePingTimeout = TimeSpan.FromSeconds(100), | ||
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always | ||
}; | ||
} | ||
else | ||
{ | ||
channelOptions.HttpHandler = new SocketsHttpHandler | ||
{ | ||
EnableMultipleHttp2Connections = true, | ||
KeepAlivePingDelay = TimeSpan.FromSeconds(20), | ||
KeepAlivePingTimeout = TimeSpan.FromSeconds(10), | ||
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.WithActiveRequests | ||
}; | ||
} | ||
|
||
var methodConfig = new MethodConfig | ||
{ | ||
Names = { MethodName.Default }, | ||
RetryPolicy = new RetryPolicy | ||
{ | ||
MaxAttempts = 5, | ||
InitialBackoff = TimeSpan.FromSeconds(1), | ||
MaxBackoff = TimeSpan.FromSeconds(5), | ||
BackoffMultiplier = 1.5, | ||
RetryableStatusCodes = { StatusCode.Unavailable } | ||
} | ||
}; | ||
|
||
channelOptions.ServiceConfig = new() { MethodConfigs = { methodConfig } }; | ||
channelOptions.ThrowOperationCanceledOnCancellation = true; | ||
}); | ||
}); | ||
builder.Services.TryAddSingleton(DistributedContextPropagator.Current); | ||
builder.Services.AddSingleton<IAgentRuntime, GrpcAgentRuntime>(); | ||
builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService<IAgentRuntime>()); | ||
return builder; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtoSerializationRegistry.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ProtoSerializationRegistry.cs | ||
|
||
namespace Microsoft.AutoGen.Core.Grpc; | ||
|
||
public class ProtoSerializationRegistry : IProtoSerializationRegistry | ||
{ | ||
private readonly Dictionary<string, IProtoMessageSerializer> _serializers | ||
= new Dictionary<string, IProtoMessageSerializer>(); | ||
|
||
public ITypeNameResolver TypeNameResolver => new ProtoTypeNameResolver(); | ||
|
||
public bool Exists(Type type) | ||
{ | ||
return _serializers.ContainsKey(TypeNameResolver.ResolveTypeName(type)); | ||
} | ||
|
||
public IProtoMessageSerializer? GetSerializer(Type type) | ||
{ | ||
return GetSerializer(TypeNameResolver.ResolveTypeName(type)); | ||
} | ||
|
||
public IProtoMessageSerializer? GetSerializer(string typeName) | ||
{ | ||
_serializers.TryGetValue(typeName, out var serializer); | ||
return serializer; | ||
} | ||
|
||
public void RegisterSerializer(Type type, IProtoMessageSerializer serializer) | ||
{ | ||
if (_serializers.ContainsKey(TypeNameResolver.ResolveTypeName(type))) | ||
{ | ||
throw new InvalidOperationException($"Serializer already registered for {type.FullName}"); | ||
} | ||
_serializers[TypeNameResolver.ResolveTypeName(type)] = serializer; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtoTypeNameResolver.cs
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
30 changes: 0 additions & 30 deletions
30
dotnet/src/Microsoft.AutoGen/Core.Grpc/SerializationRegistry.cs
This file was deleted.
Oops, something went wrong.
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