-
Notifications
You must be signed in to change notification settings - Fork 46
Support Redis URI #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shacharPash
wants to merge
16
commits into
master
Choose a base branch
from
sp-SetIfnfoIsuues
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Support Redis URI #208
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
05b1ec0
first step
shacharPash adfb264
Merge branch 'master' into sp-SetIfnfoIsuues
shacharPash 468362b
add NRedisStackConfigurationOptions
shacharPash 8c96fc9
add test
shacharPash 36a5822
steve's review
shacharPash 8be3ff5
add resp
shacharPash 21e6a10
delete core test file
shacharPash 7d8c85a
add ToLower
shacharPash 48ac593
chayim's review
shacharPash d6c0c66
coverage
shacharPash 4645477
format & naming
shacharPash 34fb111
Merge branch 'master' into sp-SetIfnfoIsuues
shacharPash e127b4a
Merge branch 'master' into sp-SetIfnfoIsuues
shacharPash b666862
add Directory.Packages.props
shacharPash 477d9ce
fix location
shacharPash 18bb0e8
Merge branch 'master' into sp-SetIfnfoIsuues
shacharPash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,40 @@ | ||
using StackExchange.Redis; | ||
|
||
namespace NRedisStack | ||
{ | ||
public static class NRedisStackConnectionMultiplexer // check if I can use this name to ConnectionMultiplexer | ||
{ | ||
public static ConnectionMultiplexer Connect(string redisConnectionString) | ||
shacharPash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var options = RedisUriParser.ParseConfigFromUri(redisConnectionString); | ||
return Connect(options); | ||
} | ||
|
||
public static Task<ConnectionMultiplexer> ConnectAsync(string redisConnectionString) | ||
{ | ||
var options = RedisUriParser.ParseConfigFromUri(redisConnectionString); | ||
return ConnectAsync(options); | ||
} | ||
public static ConnectionMultiplexer Connect(ConfigurationOptions options) | ||
{ | ||
SetLibName(options); | ||
// TODO: set here the library version when it will be available | ||
return ConnectionMultiplexer.Connect(options); | ||
} | ||
public static Task<ConnectionMultiplexer> ConnectAsync(ConfigurationOptions options) | ||
{ | ||
SetLibName(options); | ||
// TODO: set here the library version when it will be available | ||
return ConnectionMultiplexer.ConnectAsync(options); | ||
} | ||
|
||
private static void SetLibName(ConfigurationOptions options) | ||
{ | ||
if (options.LibraryName != null) // the user set his own the library name | ||
options.LibraryName = $"NRedisStack({options.LibraryName});.NET-{Environment.Version})"; | ||
else // the default library name and version sending | ||
options.LibraryName = $"NRedisStack;.NET-{Environment.Version}"; | ||
|
||
} | ||
} | ||
} |
This file contains hidden or 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,113 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Text.RegularExpressions; | ||
using StackExchange.Redis; | ||
|
||
[assembly: InternalsVisibleTo("NRedisStack.Tests")] | ||
|
||
namespace NRedisStack | ||
{ | ||
/// <summary> | ||
/// URI parsing utility. | ||
/// </summary> | ||
internal static class RedisUriParser | ||
{ | ||
/// <summary> | ||
/// Parses a Config options for StackExchange Redis from the URI. | ||
/// </summary> | ||
/// <param name="redisUri">Redis Uri string</param> | ||
/// <returns>A configuration options result for SE.Redis.</returns> | ||
internal static ConfigurationOptions ParseConfigFromUri(string redisUri) | ||
{ | ||
var options = new ConfigurationOptions(); | ||
|
||
if (string.IsNullOrEmpty(redisUri)) | ||
{ | ||
options.EndPoints.Add("localhost:6379"); | ||
return options; | ||
} | ||
|
||
var uri = new Uri(redisUri); | ||
ParseHost(options, uri); | ||
ParseUserInfo(options, uri); | ||
ParseQueryArguments(options, uri); | ||
ParseDefaultDatabase(options, uri); | ||
options.Ssl = uri.Scheme == "rediss"; | ||
options.AbortOnConnectFail = false; | ||
return options; | ||
} | ||
|
||
private static void ParseDefaultDatabase(ConfigurationOptions options, Uri uri) | ||
{ | ||
if (string.IsNullOrEmpty(uri.AbsolutePath)) | ||
{ | ||
return; | ||
} | ||
|
||
var dbNumStr = Regex.Match(uri.AbsolutePath, "[0-9]+").Value; | ||
int dbNum; | ||
if (int.TryParse(dbNumStr, out dbNum)) | ||
{ | ||
options.DefaultDatabase = dbNum; | ||
} | ||
} | ||
|
||
private static IList<KeyValuePair<string, string>> ParseQuery(string query) => | ||
query.Split('&').Select(x => | ||
new KeyValuePair<string, string>(x.Split('=').First(), x.Split('=').Last())).ToList(); | ||
|
||
private static void ParseUserInfo(ConfigurationOptions options, Uri uri) | ||
{ | ||
if (!string.IsNullOrEmpty(uri.UserInfo)) | ||
{ | ||
var userInfo = uri.UserInfo.Split(':'); | ||
if (userInfo.Length > 1) | ||
shacharPash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
options.User = Uri.UnescapeDataString(userInfo[0]); | ||
options.Password = Uri.UnescapeDataString(userInfo[1]); | ||
} | ||
else | ||
{ | ||
throw new FormatException( | ||
"Username and password must be in the form username:password - if there is no username use the format :password"); | ||
} | ||
} | ||
} | ||
|
||
private static void ParseHost(ConfigurationOptions options, Uri uri) | ||
{ | ||
var port = uri.Port >= 0 ? uri.Port : 6379; | ||
shacharPash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var host = !string.IsNullOrEmpty(uri.Host) ? uri.Host : "localhost"; | ||
shacharPash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.EndPoints.Add($"{host}:{port}"); | ||
} | ||
|
||
private static void ParseQueryArguments(ConfigurationOptions options, Uri uri) | ||
{ | ||
if (!string.IsNullOrEmpty(uri.Query)) | ||
{ | ||
var queryArgs = ParseQuery(uri.Query.Substring(1)); | ||
if (queryArgs.Any(x => x.Key == "timeout")) | ||
{ | ||
var timeout = int.Parse(queryArgs.First(x => x.Key == "timeout").Value); | ||
options.AsyncTimeout = timeout; | ||
options.SyncTimeout = timeout; | ||
options.ConnectTimeout = timeout; | ||
} | ||
|
||
if (queryArgs.Any(x => x.Key.ToLower() == "clientname")) | ||
{ | ||
options.ClientName = queryArgs.First(x => x.Key.ToLower() == "clientname").Value; | ||
} | ||
|
||
if (queryArgs.Any(x => x.Key.ToLower() == "sentinel_primary_name")) | ||
{ | ||
options.ServiceName = queryArgs.First(x => x.Key.ToLower() == "sentinel_primary_name").Value; | ||
} | ||
|
||
foreach (var endpoint in queryArgs.Where(x => x.Key == "endpoint").Select(x => x.Value)) | ||
{ | ||
options.EndPoints.Add(endpoint); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.