Skip to content

Commit

Permalink
optimization code
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 committed Dec 31, 2018
1 parent bb1346c commit c5b487b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static class ContainerBuilderExtensions
{
private static List<Assembly> _referenceAssembly = new List<Assembly>();
private static List<AbstractModule> _modules = new List<AbstractModule>();

/// <summary>
/// 添加Json序列化支持。
/// </summary>
Expand Down Expand Up @@ -486,7 +487,7 @@ public static IServiceBuilder RegisterServices(this IServiceBuilder builder, par
try
{
var services = builder.Services;
var referenceAssemblies = GetReferenceAssembly(virtualPaths);
var referenceAssemblies = GetAssemblies(virtualPaths);
foreach (var assembly in referenceAssemblies)
{
services.RegisterAssemblyTypes(assembly)
Expand Down Expand Up @@ -527,7 +528,7 @@ public static IServiceBuilder RegisterServiceBus
(this IServiceBuilder builder, params string[] virtualPaths)
{
var services = builder.Services;
var referenceAssemblies = GetReferenceAssembly(virtualPaths);
var referenceAssemblies = GetAssemblies(virtualPaths);

foreach (var assembly in referenceAssemblies)
{
Expand All @@ -548,7 +549,7 @@ public static IServiceBuilder RegisterRepositories
(this IServiceBuilder builder, params string[] virtualPaths)
{
var services = builder.Services;
var referenceAssemblies = GetReferenceAssembly(virtualPaths);
var referenceAssemblies = GetAssemblies(virtualPaths);

foreach (var assembly in referenceAssemblies)
{
Expand All @@ -562,19 +563,7 @@ public static IServiceBuilder RegisterModules(
this IServiceBuilder builder, params string[] virtualPaths)
{
var services = builder.Services;
List<Assembly> referenceAssemblies = new List<Assembly>();
if (virtualPaths.Any())
{
referenceAssemblies = GetReferenceAssembly(virtualPaths);
}
else
{
string[] assemblyNames = DependencyContext
.Default.GetDefaultAssemblyNames().Select(p => p.Name).ToArray();
assemblyNames = GetFilterAssemblies(assemblyNames);
foreach (var name in assemblyNames)
referenceAssemblies.Add(Assembly.Load(name));
}
var referenceAssemblies = GetAssemblies(virtualPaths);
if (builder == null) throw new ArgumentNullException("builder");
var packages = ConvertDictionary(AppConfig.ServerOptions.Packages);
foreach (var moduleAssembly in referenceAssemblies)
Expand Down Expand Up @@ -614,11 +603,11 @@ public static IEnumerable<string> GetDataContractName(this IServiceBuilder build
{
var namespaces = new List<string>();
var assemblies = builder.GetInterfaceService()
.Select(p=>p.Assembly)
.Select(p => p.Assembly)
.Union(GetSystemModules())
.Distinct()
.ToList();

assemblies.ForEach(assembly =>
{
namespaces.AddRange(assembly.GetTypes().Where(t => t.GetCustomAttribute<DataContractAttribute>() != null).Select(n => n.Namespace));
Expand All @@ -628,7 +617,7 @@ public static IEnumerable<string> GetDataContractName(this IServiceBuilder build

private static IDictionary<string, string> ConvertDictionary(List<ModulePackage> list)
{
var result = new Dictionary<string, string>();
var result = new Dictionary<string, string>();
list.ForEach(p =>
{
result.Add(p.TypeName, p.Using);
Expand All @@ -651,15 +640,15 @@ private static List<Assembly> GetReferenceAssembly(params string[] virtualPaths)
paths.ForEach(path =>
{
var assemblyFiles = GetAllAssemblyFiles(path);
foreach (var referencedAssemblyFile in assemblyFiles)
{
var referencedAssembly = Assembly.LoadFrom(referencedAssemblyFile);
if (!_referenceAssembly.Contains(referencedAssembly))
_referenceAssembly.Add(referencedAssembly);
refAssemblies.Add(referencedAssembly);
}
result = existsPath ? refAssemblies: _referenceAssembly;
result = existsPath ? refAssemblies : _referenceAssembly;
});
}
return result;
Expand All @@ -672,14 +661,33 @@ private static List<Assembly> GetSystemModules()
foreach (var referenceAssembly in referenceAssemblies)
{
var abstractModules = GetAbstractModules(referenceAssembly);
if(abstractModules.Any(p =>p.GetType().IsSubclassOf(typeof(SystemModule))))
if (abstractModules.Any(p => p.GetType().IsSubclassOf(typeof(SystemModule))))
{
assemblies.Add(referenceAssembly);
}
}
return assemblies;
}

private static List<Assembly> GetAssemblies(params string[] virtualPaths)
{
var referenceAssemblies = new List<Assembly>();
if (virtualPaths.Any())
{
referenceAssemblies = GetReferenceAssembly(virtualPaths);
}
else
{
string[] assemblyNames = DependencyContext
.Default.GetDefaultAssemblyNames().Select(p => p.Name).ToArray();
assemblyNames = GetFilterAssemblies(assemblyNames);
foreach (var name in assemblyNames)
referenceAssemblies.Add(Assembly.Load(name));
_referenceAssembly.AddRange(referenceAssemblies.Except(_referenceAssembly));
}
return referenceAssemblies;
}

private static List<AbstractModule> GetAbstractModules(Assembly assembly)
{
var abstractModules = new List<AbstractModule>();
Expand All @@ -694,9 +702,9 @@ private static List<AbstractModule> GetAbstractModules(Assembly assembly)
return abstractModules;
}

private static string[] GetFilterAssemblies(string[] assemblyNames)
private static string[] GetFilterAssemblies(string[] assemblyNames)
{
var notRelatedFile = AppConfig.ServerOptions.NotRelatedAssemblyFiles;
var notRelatedFile = AppConfig.ServerOptions.NotRelatedAssemblyFiles;
var relatedFile = AppConfig.ServerOptions.RelatedAssemblyFiles;
var pattern = string.Format("^Microsoft.\\w*|^System.\\w*|^DotNetty.\\w*|^runtime.\\w*|^ZooKeeperNetEx\\w*|^StackExchange.Redis\\w*|^Consul\\w*|^Newtonsoft.Json.\\w*|^Autofac.\\w*{0}",
string.IsNullOrEmpty(notRelatedFile) ? "" : $"|{notRelatedFile}");
Expand All @@ -713,7 +721,7 @@ private static string[] GetFilterAssemblies(string[] assemblyNames)
return
assemblyNames.Where(
name => !notRelatedRegex.IsMatch(name)).ToArray();
}
}
}

private static List<string> GetAllAssemblyFiles(string parentDir)
Expand All @@ -734,7 +742,7 @@ private static List<string> GetAllAssemblyFiles(string parentDir)
{
return
Directory.GetFiles(parentDir, "*.dll").Select(Path.GetFullPath).Where(
a => !notRelatedRegex.IsMatch(a)).ToList();
a => !notRelatedRegex.IsMatch(Path.GetFileName(a))).ToList();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ private void ServiceCacheManager_Removed(object sender, ServiceCacheEventArgs e)
if (hash != null)
foreach (var node in e.Cache.CacheEndpoint)
{
var hashNode = node as ConsistentHashNode;
hash.Remove(hashNode);
hash.Add(hashNode);

var hashNode = node as ConsistentHashNode;
var addr = string.Format("{0}:{1}", hashNode.Host, hashNode.Port);
hash.Remove(addr);
hash.Add(hashNode, addr);
}
}
}
Expand All @@ -122,8 +124,9 @@ private void ServiceCacheManager_Add(object sender, ServiceCacheEventArgs e)
foreach (var node in e.Cache.CacheEndpoint)
{
var hashNode = node as ConsistentHashNode;
hash.Remove(hashNode);
hash.Add(hashNode);
var addr = string.Format("{0}:{1}", hashNode.Host, hashNode.Port);
hash.Remove(addr);
hash.Add(hashNode, addr);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ public int VirtualNodeReplicationFactor
}
#endregion

/// <summary>
/// 初始化节点服务器
/// </summary>
/// <param name="nodes">节点</param>
/// <remarks>
/// <para>创建:范亮</para>
/// <para>日期:2016/4/2</para>
/// </remarks>
public void Initialize(IEnumerable<T> nodes)
{
foreach (var node in nodes)
{
AddNode(node);
}
_nodeKeysInRing = _ring.Keys.ToArray();
}

/// <summary>
/// 添加节点
Expand All @@ -71,12 +55,17 @@ public void Initialize(IEnumerable<T> nodes)
/// <para>创建:范亮</para>
/// <para>日期:2016/4/2</para>
/// </remarks>
public void Add(T node)
public void Add(T node, string value)
{
AddNode(node);
AddNode(node, value);
_nodeKeysInRing = _ring.Keys.ToArray();
}

public IEnumerable<T> GetNodes()
{
return _ring.Values.Distinct().ToList();
}

/// <summary>
/// 删除节点
/// </summary>
Expand All @@ -85,7 +74,7 @@ public void Add(T node)
/// <para>创建:范亮</para>
/// <para>日期:2016/4/2</para>
/// </remarks>
public void Remove(T node)
public void Remove(string node)
{
RemoveNode(node);
_nodeKeysInRing = _ring.Keys.ToArray();
Expand All @@ -107,11 +96,6 @@ public T GetItemNode(string item)
return _ring[_nodeKeysInRing[nearestNodePosition]];
}

public IEnumerable<T> GetNodes()
{
return _ring.Values.Distinct().ToList();
}

/// <summary>
/// 添加节点
/// </summary>
Expand All @@ -120,11 +104,11 @@ public IEnumerable<T> GetNodes()
/// <para>创建:范亮</para>
/// <para>日期:2016/4/2</para>
/// </remarks>
private void AddNode(T node)
private void AddNode(T node, string value)
{
for (var i = 0; i < _virtualNodeReplicationFactor; i++)
{
var hashOfVirtualNode = _hashAlgorithm.Hash(node.GetHashCode().ToString(CultureInfo.InvariantCulture) + i);
var hashOfVirtualNode = _hashAlgorithm.Hash(value.ToString(CultureInfo.InvariantCulture) + i);
_ring[hashOfVirtualNode] = node;
}
}
Expand All @@ -137,13 +121,12 @@ private void AddNode(T node)
/// <para>创建:范亮</para>
/// <para>日期:2016/4/2</para>
/// </remarks>
private void RemoveNode(T node)
private void RemoveNode(string value)
{
for (var i = 0; i < _virtualNodeReplicationFactor; i++)
{
var hashOfVirtualNode = _hashAlgorithm.Hash(node.GetHashCode().ToString() + i);
if (_ring.ContainsKey(hashOfVirtualNode))
_ring.Remove(hashOfVirtualNode);
var hashOfVirtualNode = _hashAlgorithm.Hash(value.ToString() + i);
_ring.Remove(hashOfVirtualNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void InitSettingHashStorage()
{
db = dbs[dbs.Length - 1];
}
hash.Add(new ConsistentHashNode()
var node = new ConsistentHashNode()
{
Type = targetType,
Host = endpoints[0],
Expand All @@ -216,7 +216,8 @@ private void InitSettingHashStorage()
MaxSize = this._maxSize,
MinSize = this._minSize,
Db = db.ToString()
});
};
hash.Add(node,string.Format("{0}:{1}",node.Host,node.Port));
dicHash.GetOrAdd(targetType.ToString(), hash);
});
}
Expand Down

0 comments on commit c5b487b

Please sign in to comment.