【优化】优化Luban.ClientServer性能,GenServer使用LocalAgent直接读取本地文件,而不是通过网络从GenClient获得。

main
walon 2021-08-27 10:54:38 +08:00
parent 2685a4d5d4
commit f6e4f6377a
6 changed files with 62 additions and 40 deletions

View File

@ -162,7 +162,7 @@ Options:
} }
StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates"));
GenServer.Ins.Start(options.Port, ProtocolStub.Factories); GenServer.Ins.Start(true, options.Port, ProtocolStub.Factories);
GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController()); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController());
GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController()); GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController());

View File

@ -0,0 +1,27 @@
using Luban.Common.Protos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Luban.Server.Common
{
public interface IAgent
{
Task<byte[]> GetFromCacheOrReadAllBytesAsync(string file, string md5);
Task<byte[]> ReadAllBytesAsync(string file);
Task<GetImportFileOrDirectoryRes> GetFileOrDirectoryAsync(string file, params string[] searchPatterns);
Task<QueryFilesExistsRes> QueryFileExistsAsync(QueryFilesExistsArg arg);
Task<XElement> OpenXmlAsync(string xmlFile);
void Error(string fmt, params object[] objs);
void Info(string fmt, params object[] objs);
}
}

View File

@ -0,0 +1,24 @@
using Bright.Net.ServiceModes.Managers;
using Luban.Common.Protos;
using Luban.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Luban.Server.Common
{
public class LocalAgent : RemoteAgent
{
public LocalAgent(SessionBase session) : base(session)
{
}
public override Task<byte[]> ReadAllBytesAsync(string file)
{
return FileUtil.ReadAllBytesAsync(file);
}
}
}

View File

@ -31,22 +31,19 @@ namespace Luban.Server.Common
} }
} }
public class RemoteAgent public class RemoteAgent : IAgent
{ {
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
public SessionBase Session { get; } public SessionBase Session { get; }
private readonly bool _trace;
private readonly ConcurrentDictionary<string, Task<byte[]>> _remoteReadAllBytesTasks = new(); private readonly ConcurrentDictionary<string, Task<byte[]>> _remoteReadAllBytesTasks = new();
private readonly ConcurrentDictionary<string, Task<GetImportFileOrDirectoryRes>> _getImportFileOrDirTasks = new(); private readonly ConcurrentDictionary<string, Task<GetImportFileOrDirectoryRes>> _getImportFileOrDirTasks = new();
public RemoteAgent(SessionBase session, bool trace) public RemoteAgent(SessionBase session)
{ {
Session = session; Session = session;
_trace = trace;
} }
private const int GET_INPUT_FILE_TIMEOUT = 10; private const int GET_INPUT_FILE_TIMEOUT = 10;
@ -63,7 +60,7 @@ namespace Luban.Server.Common
return content; return content;
} }
public Task<byte[]> ReadAllBytesAsync(string file) public virtual Task<byte[]> ReadAllBytesAsync(string file)
{ {
return _remoteReadAllBytesTasks.GetOrAdd(file, f => return _remoteReadAllBytesTasks.GetOrAdd(file, f =>
{ {
@ -129,7 +126,6 @@ namespace Luban.Server.Common
} }
} }
#region log #region log
public void Error(string fmt, params object[] objs) public void Error(string fmt, params object[] objs)
@ -137,43 +133,15 @@ namespace Luban.Server.Common
Log("error", string.Format(fmt, objs)); Log("error", string.Format(fmt, objs));
} }
public void Error(Exception e, string s)
{
LogException(e, s);
}
public void Error(Exception e, string fmt, params object[] objs)
{
LogException(e, string.Format(fmt, objs));
}
public void Info(string fmt, params object[] objs) public void Info(string fmt, params object[] objs)
{ {
Log("info", string.Format(fmt, objs)); Log("info", string.Format(fmt, objs));
} }
public void Info(string s)
{
Log("info", s);
}
public void Trace(string fmt, params object[] objs)
{
if (_trace)
{
Log("trace", string.Format(fmt, objs));
}
}
private void Log(string level, string content) private void Log(string level, string content)
{ {
Session.Send(new PushLog() { Level = level, LogContent = content }); Session.Send(new PushLog() { Level = level, LogContent = content });
} }
private void LogException(Exception e, string content)
{
Session.Send(new PushException() { LogContent = content, Message = e.Message, StackTrace = e.StackTrace });
}
#endregion #endregion
} }
} }

View File

@ -33,15 +33,18 @@ namespace Luban.Server
private readonly Dictionary<string, IJobController> _jobs = new Dictionary<string, IJobController>(); private readonly Dictionary<string, IJobController> _jobs = new Dictionary<string, IJobController>();
public void Start(int port, Dictionary<int, ProtocolCreator> factories) private bool _localServer;
public void Start(bool localhost, int port, Dictionary<int, ProtocolCreator> factories)
{ {
_localServer = localhost;
_handlers.Add(GetOutputFile.ID, (s, p) => OnGetOutputFile(s, (GetOutputFile)p)); _handlers.Add(GetOutputFile.ID, (s, p) => OnGetOutputFile(s, (GetOutputFile)p));
_handlers.Add(GenJob.ID, (s, p) => OnGenJob(s, (GenJob)p)); _handlers.Add(GenJob.ID, (s, p) => OnGenJob(s, (GenJob)p));
var worker = new EventLoopGroup(4, 16); var worker = new EventLoopGroup(4, 16);
var server = new TcpServerBootstrap var server = new TcpServerBootstrap
{ {
LocalAddress = new IPEndPoint(IPAddress.Any, port), LocalAddress = new IPEndPoint(localhost ? IPAddress.Loopback : IPAddress.Any, port),
ChildrenEventLoopGroup = worker, ChildrenEventLoopGroup = worker,
EventLoop = worker.ChooseEventLoop(), EventLoop = worker.ChooseEventLoop(),
@ -102,7 +105,7 @@ namespace Luban.Server
if (_jobs.TryGetValue(rpc.Arg.JobType, out var jobController)) if (_jobs.TryGetValue(rpc.Arg.JobType, out var jobController))
{ {
_ = jobController.GenAsync(new RemoteAgent(session, rpc.Arg.Verbose), rpc); _ = jobController.GenAsync(_localServer ? new LocalAgent(session) : new RemoteAgent(session), rpc);
} }
else else
{ {

View File

@ -59,7 +59,7 @@ namespace Luban.Server
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
GenServer.Ins.Start(options.Port, ProtocolStub.Factories); GenServer.Ins.Start(false, options.Port, ProtocolStub.Factories);
GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController()); GenServer.Ins.RegisterJob("cfg", new Luban.Job.Cfg.JobController());
GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController()); GenServer.Ins.RegisterJob("proto", new Luban.Job.Proto.JobController());