-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Couchbase.Extensions.DnsDiscovery package for DNS SRV discovery (…
…#1)
- Loading branch information
1 parent
9887e81
commit 65da886
Showing
26 changed files
with
901 additions
and
26 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue"><data><IncludeFilters /><ExcludeFilters><Filter ModuleMask="Couchbase.Extensions.DependencyInjection.*Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /><Filter ModuleMask="Couchbase.Extensions.DependencyInjection" ModuleVersionMask="*" ClassMask="Couchbase.Extensions.DependencyInjection.Internal.ClusterProvider" FunctionMask="CreateCluster" IsEnabled="True" /><Filter ModuleMask="TestApp" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /></ExcludeFilters></data></s:String> | ||
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue"><data><IncludeFilters /><ExcludeFilters><Filter ModuleMask="*Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /><Filter ModuleMask="Couchbase.Extensions.DependencyInjection" ModuleVersionMask="*" ClassMask="Couchbase.Extensions.DependencyInjection.Internal.ClusterProvider" FunctionMask="CreateCluster" IsEnabled="True" /><Filter ModuleMask="TestApp" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /><Filter ModuleMask="Couchbase.Extensions.DnsDiscovery" ModuleVersionMask="*" ClassMask="Couchbase.Extensions.DnsDiscovery.Internal.LookupClientAdapter" FunctionMask="*" IsEnabled="True" /></ExcludeFilters></data></s:String> | ||
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue"><data><AttributeFilter ClassMask="Couchbase.Extensions.DependencyInjection.Internal.ExcludeFromCodeCoverageAttribute" IsEnabled="True" /></data></s:String></wpf:ResourceDictionary> |
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
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
21 changes: 21 additions & 0 deletions
21
src/Couchbase.Extensions.DnsDiscovery/Couchbase.Extensions.DnsDiscovery.xproj
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,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>71a8fc7a-a848-4414-bfae-86e8a08e59a0</ProjectGuid> | ||
<RootNamespace>Couchbase.Extensions.DnsDiscovery</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
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,9 @@ | ||
using Couchbase.Configuration.Client; | ||
|
||
namespace Couchbase.Extensions.DnsDiscovery | ||
{ | ||
public interface ICouchbaseDnsLookup | ||
{ | ||
void Apply(CouchbaseClientDefinition clientDefinition, string recordName); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/Couchbase.Extensions.DnsDiscovery/Internal/CouchbaseDnsLookup.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,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using Couchbase.Configuration.Client; | ||
using DnsClient; | ||
using DnsClient.Protocol; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Couchbase.Extensions.DnsDiscovery.Internal | ||
{ | ||
internal class CouchbaseDnsLookup : ICouchbaseDnsLookup | ||
{ | ||
private readonly ILookupClientAdapter _lookupClient; | ||
private readonly ILogger<CouchbaseDnsLookup> _logger; | ||
|
||
public CouchbaseDnsLookup(ILookupClientAdapter lookupClient, ILogger<CouchbaseDnsLookup> logger) | ||
{ | ||
if (lookupClient == null) | ||
{ | ||
throw new ArgumentNullException(nameof(lookupClient)); | ||
} | ||
if (logger == null) | ||
{ | ||
throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
_lookupClient = lookupClient; | ||
_logger = logger; | ||
} | ||
|
||
public void Apply(CouchbaseClientDefinition clientDefinition, string recordName) | ||
{ | ||
if (clientDefinition == null) | ||
{ | ||
throw new ArgumentNullException(nameof(clientDefinition)); | ||
} | ||
if (recordName == null) | ||
{ | ||
throw new ArgumentNullException(nameof(recordName)); | ||
} | ||
|
||
try | ||
{ | ||
// Ensure an empty collection of servers before resolving | ||
if (clientDefinition.Servers == null) | ||
{ | ||
clientDefinition.Servers = new List<Uri>(); | ||
} | ||
else | ||
{ | ||
clientDefinition.Servers.Clear(); | ||
} | ||
|
||
_logger.LogInformation("Looking up Couchbase servers using record '{0}'", recordName); | ||
|
||
List<SrvRecord> servers; | ||
var syncContextCache = SynchronizationContext.Current; | ||
try | ||
{ | ||
// Ensure that we're outside any sync context before waiting on an async result to prevent deadlocks | ||
SynchronizationContext.SetSynchronizationContext(null); | ||
|
||
servers = _lookupClient | ||
.QuerySrvAsync(recordName).Result | ||
.OrderBy(p => p.Priority) | ||
.ToList(); | ||
} | ||
finally | ||
{ | ||
if (syncContextCache != null) | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(syncContextCache); | ||
} | ||
} | ||
|
||
if (!servers.Any()) | ||
{ | ||
_logger.LogError("No SRV records returned for query '{0}'", recordName); | ||
return; | ||
} | ||
|
||
var firstPriority = servers.First().Priority; | ||
foreach (var server in servers.Where(p => p.Priority == firstPriority)) | ||
{ | ||
var uri = new Uri($"http://{FormatTargetDns(server.Target)}:{server.Port}/pools"); | ||
|
||
_logger.LogInformation("Got Couchbase server '{0}'", uri); | ||
|
||
clientDefinition.Servers.Add(uri); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(0, ex, "Exception getting Couchbase servers for '{0}'", recordName); | ||
} | ||
} | ||
|
||
private string FormatTargetDns(string target) | ||
{ | ||
if (target.EndsWith(".")) | ||
{ | ||
return target.Substring(0, target.Length - 1); | ||
} | ||
|
||
return target; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Couchbase.Extensions.DnsDiscovery/Internal/ILookupClientAdapter.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,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using DnsClient.Protocol; | ||
|
||
namespace Couchbase.Extensions.DnsDiscovery.Internal | ||
{ | ||
/// <summary> | ||
/// Mockable adapter for DnsClient.LookupClient | ||
/// </summary> | ||
internal interface ILookupClientAdapter | ||
{ | ||
Task<IEnumerable<SrvRecord>> QuerySrvAsync(string query); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Couchbase.Extensions.DnsDiscovery/Internal/LookupClientAdapter.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,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using DnsClient; | ||
using DnsClient.Protocol; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Couchbase.Extensions.DnsDiscovery.Internal | ||
{ | ||
/// <summary> | ||
/// Mockable adapter for DnsClient.LookupClient | ||
/// </summary> | ||
internal class LookupClientAdapter : ILookupClientAdapter | ||
{ | ||
private readonly ILogger<LookupClientAdapter> _logger; | ||
private readonly LookupClient _lookupClient = new LookupClient(); | ||
|
||
public LookupClientAdapter(ILogger<LookupClientAdapter> logger) | ||
{ | ||
if (logger == null) | ||
{ | ||
throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
_logger = logger; | ||
} | ||
|
||
public async Task<IEnumerable<SrvRecord>> QuerySrvAsync(string query) | ||
{ | ||
var response = await _lookupClient.QueryAsync(query, QueryType.SRV); | ||
|
||
if (response.HasError) | ||
{ | ||
_logger.LogError("DNS query error '{0}'", response.ErrorMessage); | ||
|
||
return new SrvRecord[] {}; | ||
} | ||
|
||
return response.Answers.SrvRecords(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Couchbase.Extensions.DnsDiscovery/Properties/AssemblyInfo.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,22 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Couchbase.Extensions.DnsDiscovery")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("71a8fc7a-a848-4414-bfae-86e8a08e59a0")] | ||
|
||
[assembly: InternalsVisibleTo("Couchbase.Extensions.DnsDiscovery.UnitTests")] | ||
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] |
Oops, something went wrong.