Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeLyn committed Mar 29, 2019
1 parent ef91a65 commit b4e980b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 119 deletions.
50 changes: 25 additions & 25 deletions samples/GenericHostSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,31 @@ static async Task Main(string[] args)
}
}

//var hostBuilder = new HostBuilder().ConfigureHostConfiguration(builder =>
// {
// builder.SetBasePath(Directory.GetCurrentDirectory());
// }).ConfigureAppConfiguration((context, builder) =>
// {
// builder.AddJsonFile("uragano.json", false, true);
// //builder.AddEnvironmentVariables("uragano");
// builder.AddCommandLine(args);
// })
// .ConfigureServices((context, service) =>
// {
// service.AddUragano(context.Configuration, builder =>
// {
// builder.AddServer();
// builder.AddClient();
// builder.AddCircuitBreaker();
// builder.AddExceptionlessLogger();
// builder.AddConsul();
// });
// }).ConfigureLogging((context, builder) =>
// {
// builder.AddConfiguration(context.Configuration.GetSection("Logging"));
// builder.AddConsole();
// });
//await hostBuilder.RunConsoleAsync();
var hostBuilder = new HostBuilder().ConfigureHostConfiguration(builder =>
{
builder.SetBasePath(Directory.GetCurrentDirectory());
}).ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("uragano.json", false, true);
builder.AddCommandLine(args);
})
.ConfigureServices((context, service) =>
{
service.AddUragano(context.Configuration, builder =>
{
builder.AddServer();
builder.AddClient();
builder.AddCircuitBreaker();
builder.AddExceptionlessLogger();
builder.AddConsul();
});

}).ConfigureLogging((context, builder) =>
{
builder.AddConfiguration(context.Configuration.GetSection("Logging"));
builder.AddConsole();
});
await hostBuilder.RunConsoleAsync();
}
}
}
12 changes: 0 additions & 12 deletions samples/Sample.Server/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sample.Common;
using Sample.Service.Interfaces;
using Uragano.Abstractions;
using Uragano.Caching.Memory;
using Uragano.Caching.Redis;
using Uragano.Codec.MessagePack;
using Uragano.Consul;
using Uragano.Core;
using Uragano.Logging.Exceptionless;
using Uragano.Logging.Log4Net;
using Uragano.Logging.NLog;
using Uragano.Remoting.LoadBalancing;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;

Expand Down
2 changes: 0 additions & 2 deletions src/Uragano.Abstract/UraganoSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Uragano.Abstractions.CircuitBreaker;
Expand All @@ -10,7 +9,6 @@ namespace Uragano.Abstractions
{
public class UraganoSettings
{
public bool IsDevelopment { get; set; }
public ServerSettings ServerSettings { get; set; }

public IServiceDiscoveryClientConfiguration ServiceDiscoveryClientConfiguration { get; set; }
Expand Down
16 changes: 11 additions & 5 deletions src/Uragano.Remoting/IClient.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ public class Client : IClient

private ICodec Codec { get; }

public Client(IChannel channel, IEventLoopGroup eventLoopGroup, IMessageListener messageListener, ILogger logger, ICodec codec)
private string Node { get; }

public Client(IChannel channel, IEventLoopGroup eventLoopGroup, IMessageListener messageListener, ILogger logger, ICodec codec, string node)
{
Channel = channel;
MessageListener = messageListener;
MessageListener.OnReceived += MessageListener_OnReceived;
EventLoopGroup = eventLoopGroup;
Logger = logger;
Codec = codec;
Node = node;
}

private void MessageListener_OnReceived(TransportMessage<IServiceResult> message)
Expand All @@ -50,23 +53,26 @@ public async Task<IServiceResult> SendAsync(IInvokeMessage message)
Body = message
};
if (Logger.IsEnabled(LogLevel.Trace))
Logger.LogTrace($"\nSending message.\nMessage id:{transportMessage.Id}\nArgs:{Codec.ToJson(message.Args)}");
Logger.LogTrace($"\nSending message to node {Node}:\nMessage id:{transportMessage.Id}\nArgs:{Codec.ToJson(message.Args)}\n\n");
var tcs = new TaskCompletionSource<IServiceResult>(TaskCreationOptions.RunContinuationsAsynchronously);
using (var ct = new CancellationTokenSource(UraganoOptions.Remoting_Invoke_CancellationTokenSource_Timeout.Value))
{
ct.Token.Register(() =>
{
tcs.TrySetResult(new ServiceResult("Remoting invoke timeout!", RemotingStatus.Timeout));
Logger.LogWarning("\nRemoting invoke timeout,You can set the wait time with the Remoting_Invoke_CancellationTokenSource_Timeout option.\n[Message id:{0}]", transportMessage.Id);
Logger.LogWarning("\nRemoting invoke timeout,You can set the wait time with the Remoting_Invoke_CancellationTokenSource_Timeout option.\nSend to node:{1}\nMessage id:{0}\n\n", transportMessage.Id, Node);
}, false);

if (!_resultCallbackTask.TryAdd(transportMessage.Id, tcs)) throw new Exception("Failed to send.");
try
{
await Channel.WriteAndFlushAsync(transportMessage);
if (Logger.IsEnabled(LogLevel.Trace))
Logger.LogTrace($"\nSend completed, waiting for results.\nMessage id:{transportMessage.Id}");
return await tcs.Task;
Logger.LogTrace($"\nSend completed, waiting for node {Node} to return results:\nMessage id:{transportMessage.Id}\n\n");
var result = await tcs.Task;
if (Logger.IsEnabled(LogLevel.Trace))
Logger.LogTrace($"\nThe client received the return result of node {Node}:\nMessage id:{transportMessage.Id}\nBody:{Codec.ToJson(result)}\n\n");
return result;
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/Uragano.Remoting/IClientFactory.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task<IClient> CreateClientAsync(string host, int port)
});
var listener = new MessageListener();
channel.GetAttribute(MessageListenerAttributeKey).Set(listener);
return new Client(channel, group, listener, Logger, Codec);
return new Client(channel, group, listener, Logger, Codec, $"{host}:{port}");
});
}
catch
Expand Down
2 changes: 1 addition & 1 deletion src/Uragano.Remoting/ServerBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task StartAsync()
pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));
pipeline.AddLast(new MessageDecoder<IInvokeMessage>(Codec));
pipeline.AddLast(new MessageEncoder<IServiceResult>(Codec));
pipeline.AddLast(new ServerMessageHandler(ServiceFactory, ServiceProvider, Logger, Codec));
pipeline.AddLast(new ServerMessageHandler(ServiceFactory, ServiceProvider, Logger, Codec, ServerSettings));
}));

EndPoint endPoint;
Expand Down
11 changes: 7 additions & 4 deletions src/Uragano.Remoting/ServerMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public class ServerMessageHandler : ChannelHandlerAdapter

private ICodec Codec { get; }

public ServerMessageHandler(IServiceFactory serviceFactory, IServiceProvider serviceProvider, ILogger logger, ICodec codec)
private ServerSettings ServerSettings { get; }

public ServerMessageHandler(IServiceFactory serviceFactory, IServiceProvider serviceProvider, ILogger logger, ICodec codec, ServerSettings serverSettings)
{
ServiceFactory = serviceFactory;
ServiceProvider = serviceProvider;
Logger = logger;
Codec = codec;
ServerSettings = serverSettings;
}

public override void ChannelRead(IChannelHandlerContext context, object message)
Expand All @@ -37,7 +40,7 @@ public override void ChannelRead(IChannelHandlerContext context, object message)
try
{
if (Logger.IsEnabled(LogLevel.Trace))
Logger.LogTrace($"\nReceived the message:\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}\nArgs:{Codec.ToJson(transportMessage.Body.Args)}\nMeta:{Codec.ToJson(transportMessage.Body.Meta)}");
Logger.LogTrace($"\nThe server received the message:\nCurrent node:{ServerSettings}\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}\nArgs:{Codec.ToJson(transportMessage.Body.Args)}\nMeta:{Codec.ToJson(transportMessage.Body.Meta)}\n\n");
var result = await ServiceFactory.InvokeAsync(transportMessage.Body.Route, transportMessage.Body.Args,
transportMessage.Body.Meta);
await context.WriteAndFlushAsync(new TransportMessage<IServiceResult>
Expand All @@ -48,7 +51,7 @@ await context.WriteAndFlushAsync(new TransportMessage<IServiceResult>
}
catch (NotFoundRouteException e)
{
Logger.LogError(e, $"\nMessage processing failed:{e.Message}.\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}");
Logger.LogError(e, $"\nThe server message processing failed:{e.Message}.\nCurrent node:{ServerSettings}\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}\n\n");
await context.WriteAndFlushAsync(new TransportMessage<IServiceResult>
{
Id = transportMessage.Id,
Expand All @@ -57,7 +60,7 @@ await context.WriteAndFlushAsync(new TransportMessage<IServiceResult>
}
catch (Exception e)
{
Logger.LogError(e, $"\nMessage processing failed:{e.Message}.\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}");
Logger.LogError(e, $"\nThe server message processing failed:{e.Message}.\nCurrent node:{ServerSettings}\nRoute:{transportMessage.Body.Route}\nMessage id:{transportMessage.Id}\n\n");
await context.WriteAndFlushAsync(new TransportMessage<IServiceResult>
{
Id = transportMessage.Id,
Expand Down
31 changes: 0 additions & 31 deletions test/UnitTest/CodecTest.cs

This file was deleted.

17 changes: 0 additions & 17 deletions test/UnitTest/Consul/ConsulClientConfigureTest.cs

This file was deleted.

21 changes: 0 additions & 21 deletions test/UnitTest/UnitTest.csproj

This file was deleted.

0 comments on commit b4e980b

Please sign in to comment.