Started new testbed

This commit is contained in:
RogueException
2017-01-24 11:55:41 -04:00
parent fe35400498
commit d9802593ab
12 changed files with 685 additions and 620 deletions

View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Discord.Net
{
internal class CacheInfo
{
[JsonProperty("guild_id")]
public ulong? GuildId { get; set; }
[JsonProperty("version")]
public uint Version { get; set; }
}
}

View File

@@ -0,0 +1,120 @@
using Akavache;
using Akavache.Sqlite3;
using Discord.Net.Rest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Splat;
using System.Reactive.Concurrency;
namespace Discord.Net
{
internal class CachedRestClient : IRestClient
{
private readonly Dictionary<string, string> _headers;
private IBlobCache _blobCache;
private string _baseUrl;
private CancellationTokenSource _cancelTokenSource;
private CancellationToken _cancelToken, _parentToken;
private bool _isDisposed;
public CacheInfo Info { get; private set; }
public CachedRestClient()
{
_headers = new Dictionary<string, string>();
_cancelTokenSource = new CancellationTokenSource();
_cancelToken = CancellationToken.None;
_parentToken = CancellationToken.None;
Locator.CurrentMutable.Register(() => Scheduler.Default, typeof(IScheduler), "Taskpool");
Locator.CurrentMutable.Register(() => new FilesystemProvider(), typeof(IFilesystemProvider), null);
Locator.CurrentMutable.Register(() => new HttpMixin(), typeof(IAkavacheHttpMixin), null);
//new Akavache.Sqlite3.Registrations().Register(Locator.CurrentMutable);
_blobCache = new SQLitePersistentBlobCache("cache.db");
}
private void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
_blobCache.Dispose();
_isDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
public void SetUrl(string url)
{
_baseUrl = url;
}
public void SetHeader(string key, string value)
{
_headers[key] = value;
}
public void SetCancelToken(CancellationToken cancelToken)
{
_parentToken = cancelToken;
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
}
public async Task<RestResponse> SendAsync(string method, string endpoint, CancellationToken cancelToken, bool headerOnly)
{
if (method != "GET")
throw new InvalidOperationException("This RestClient only supports GET requests.");
string uri = Path.Combine(_baseUrl, endpoint);
var bytes = await _blobCache.DownloadUrl(uri, _headers);
return new RestResponse(HttpStatusCode.OK, _headers, new MemoryStream(bytes));
}
public Task<RestResponse> SendAsync(string method, string endpoint, string json, CancellationToken cancelToken, bool headerOnly)
{
throw new InvalidOperationException("This RestClient does not support payloads.");
}
public Task<RestResponse> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, CancellationToken cancelToken, bool headerOnly)
{
throw new InvalidOperationException("This RestClient does not support multipart requests.");
}
public async Task ClearAsync()
{
await _blobCache.InvalidateAll();
}
public async Task LoadInfoAsync(ulong guildId)
{
if (Info != null)
return;
bool needsReset = false;
try
{
Info = await _blobCache.GetObject<CacheInfo>("info");
if (Info.GuildId != guildId)
needsReset = true;
}
catch (KeyNotFoundException)
{
needsReset = true;
}
if (needsReset)
{
Info = new CacheInfo() { GuildId = guildId, Version = 0 };
await SaveInfoAsync().ConfigureAwait(false);
}
}
public async Task SaveInfoAsync()
{
await ClearAsync().ConfigureAwait(false); //Version changed, invalidate cache
await _blobCache.InsertObject<CacheInfo>("info", Info);
}
}
}

View File

@@ -0,0 +1,124 @@
using Akavache;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reflection;
namespace Discord
{
public class FilesystemProvider : IFilesystemProvider
{
public IObservable<Stream> OpenFileForReadAsync(string path, IScheduler scheduler)
{
return SafeOpenFileAsync(path, FileMode.Open, FileAccess.Read, FileShare.Read, scheduler);
}
public IObservable<Stream> OpenFileForWriteAsync(string path, IScheduler scheduler)
{
return SafeOpenFileAsync(path, FileMode.Create, FileAccess.Write, FileShare.None, scheduler);
}
public IObservable<Unit> CreateRecursive(string path)
{
CreateRecursive(new DirectoryInfo(path));
return Observable.Return(Unit.Default);
}
public IObservable<Unit> Delete(string path)
{
return Observable.Start(() => File.Delete(path), Scheduler.Default);
}
public string GetDefaultRoamingCacheDirectory()
{
throw new NotSupportedException();
}
public string GetDefaultSecretCacheDirectory()
{
throw new NotSupportedException();
}
public string GetDefaultLocalMachineCacheDirectory()
{
throw new NotSupportedException();
}
protected static string GetAssemblyDirectoryName()
{
var assemblyDirectoryName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Debug.Assert(assemblyDirectoryName != null, "The directory name of the assembly location is null");
return assemblyDirectoryName;
}
private static IObservable<Stream> SafeOpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share, IScheduler scheduler = null)
{
scheduler = scheduler ?? Scheduler.Default;
var ret = new AsyncSubject<Stream>();
Observable.Start(() =>
{
try
{
var createModes = new[]
{
FileMode.Create,
FileMode.CreateNew,
FileMode.OpenOrCreate,
};
// NB: We do this (even though it's incorrect!) because
// throwing lots of 1st chance exceptions makes debugging
// obnoxious, as well as a bug in VS where it detects
// exceptions caught by Observable.Start as Unhandled.
if (!createModes.Contains(mode) && !File.Exists(path))
{
ret.OnError(new FileNotFoundException());
return;
}
Observable.Start(() => new FileStream(path, mode, access, share, 4096, false), scheduler).Cast<Stream>().Subscribe(ret);
}
catch (Exception ex)
{
ret.OnError(ex);
}
}, scheduler);
return ret;
}
private static void CreateRecursive(DirectoryInfo info)
{
SplitFullPath(info).Aggregate((parent, dir) =>
{
var path = Path.Combine(parent, dir);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
});
}
private static IEnumerable<string> SplitFullPath(DirectoryInfo info)
{
var root = Path.GetPathRoot(info.FullName);
var components = new List<string>();
for (var path = info.FullName; path != root && path != null; path = Path.GetDirectoryName(path))
{
var filename = Path.GetFileName(path);
if (String.IsNullOrEmpty(filename))
continue;
components.Add(filename);
}
components.Add(root);
components.Reverse();
return components;
}
}
}

View File

@@ -0,0 +1,139 @@
using Akavache;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Reactive;
using System.Reactive.Threading.Tasks;
namespace Discord.Net
{
public class HttpMixin : IAkavacheHttpMixin
{
/// <summary>
/// Download data from an HTTP URL and insert the result into the
/// cache. If the data is already in the cache, this returns
/// a cached value. The URL itself is used as the key.
/// </summary>
/// <param name="url">The URL to download.</param>
/// <param name="headers">An optional Dictionary containing the HTTP
/// request headers.</param>
/// <param name="fetchAlways">Force a web request to always be issued, skipping the cache.</param>
/// <param name="absoluteExpiration">An optional expiration date.</param>
/// <returns>The data downloaded from the URL.</returns>
public IObservable<byte[]> DownloadUrl(IBlobCache This, string url, IDictionary<string, string> headers = null, bool fetchAlways = false, DateTimeOffset? absoluteExpiration = null)
{
return This.DownloadUrl(url, url, headers, fetchAlways, absoluteExpiration);
}
/// <summary>
/// Download data from an HTTP URL and insert the result into the
/// cache. If the data is already in the cache, this returns
/// a cached value. An explicit key is provided rather than the URL itself.
/// </summary>
/// <param name="key">The key to store with.</param>
/// <param name="url">The URL to download.</param>
/// <param name="headers">An optional Dictionary containing the HTTP
/// request headers.</param>
/// <param name="fetchAlways">Force a web request to always be issued, skipping the cache.</param>
/// <param name="absoluteExpiration">An optional expiration date.</param>
/// <returns>The data downloaded from the URL.</returns>
public IObservable<byte[]> DownloadUrl(IBlobCache This, string key, string url, IDictionary<string, string> headers = null, bool fetchAlways = false, DateTimeOffset? absoluteExpiration = null)
{
var doFetch = MakeWebRequest(new Uri(url), headers).SelectMany(x => ProcessWebResponse(x, url, absoluteExpiration));
var fetchAndCache = doFetch.SelectMany(x => This.Insert(key, x, absoluteExpiration).Select(_ => x));
var ret = default(IObservable<byte[]>);
if (!fetchAlways)
{
ret = This.Get(key).Catch(fetchAndCache);
}
else
{
ret = fetchAndCache;
}
var conn = ret.PublishLast();
conn.Connect();
return conn;
}
IObservable<byte[]> ProcessWebResponse(WebResponse wr, string url, DateTimeOffset? absoluteExpiration)
{
var hwr = (HttpWebResponse)wr;
Debug.Assert(hwr != null, "The Web Response is somehow null but shouldn't be.");
if ((int)hwr.StatusCode >= 400)
{
return Observable.Throw<byte[]>(new WebException(hwr.StatusDescription));
}
var ms = new MemoryStream();
using (var responseStream = hwr.GetResponseStream())
{
Debug.Assert(responseStream != null, "The response stream is somehow null");
responseStream.CopyTo(ms);
}
var ret = ms.ToArray();
return Observable.Return(ret);
}
static IObservable<WebResponse> MakeWebRequest(
Uri uri,
IDictionary<string, string> headers = null,
string content = null,
int retries = 3,
TimeSpan? timeout = null)
{
IObservable<WebResponse> request;
request = Observable.Defer(() =>
{
var hwr = CreateWebRequest(uri, headers);
if (content == null)
return Observable.FromAsyncPattern<WebResponse>(hwr.BeginGetResponse, hwr.EndGetResponse)();
var buf = Encoding.UTF8.GetBytes(content);
// NB: You'd think that BeginGetResponse would never block,
// seeing as how it's asynchronous. You'd be wrong :-/
var ret = new AsyncSubject<WebResponse>();
Observable.Start(() =>
{
Observable.FromAsyncPattern<Stream>(hwr.BeginGetRequestStream, hwr.EndGetRequestStream)()
.SelectMany(x => WriteAsyncRx(x, buf, 0, buf.Length))
.SelectMany(_ => Observable.FromAsyncPattern<WebResponse>(hwr.BeginGetResponse, hwr.EndGetResponse)())
.Multicast(ret).Connect();
}, BlobCache.TaskpoolScheduler);
return ret;
});
return request.Timeout(timeout ?? TimeSpan.FromSeconds(15), BlobCache.TaskpoolScheduler).Retry(retries);
}
private static WebRequest CreateWebRequest(Uri uri, IDictionary<string, string> headers)
{
var hwr = WebRequest.Create(uri);
if (headers != null)
{
foreach (var x in headers)
{
hwr.Headers[x.Key] = x.Value;
}
}
return hwr;
}
private static IObservable<Unit> WriteAsyncRx(Stream stream, byte[] data, int start, int length)
{
return stream.WriteAsync(data, start, length).ToObservable();
}
}
}