Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,354 changes: 0 additions & 1,354 deletions src/LaunchDarkly.Client/App_Packages/LibLog.1.4/LibLog.cs

This file was deleted.

1,991 changes: 1,991 additions & 0 deletions src/LaunchDarkly.Client/App_Packages/LibLog.4.2/LibLog.cs

Large diffs are not rendered by default.

37 changes: 14 additions & 23 deletions src/LaunchDarkly.Client/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
using System.Linq;
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.Collections;
using System.Collections.Generic;

namespace LaunchDarkly.Client
{
public class EventProcessor : IDisposable, IStoreEvents
public sealed class EventProcessor : IDisposable, IStoreEvents
{
private static readonly ILog Logger = LogProvider.For<EventProcessor>();

private readonly Configuration _configuration;
private readonly HttpClient _httpClient;
private BlockingCollection<Event> _queue;
private System.Threading.Timer _timer;

Expand All @@ -21,6 +25,7 @@ public EventProcessor(Configuration configuration)
_configuration = configuration;
_queue = new BlockingCollection<Event>(_configuration.EventQueueCapacity);
_timer = new System.Threading.Timer(SubmitEvents, null, _configuration.EventQueueFrequency, _configuration.EventQueueFrequency);
_httpClient = new HttpClient { BaseAddress = _configuration.BaseUri };
}

public void Add(Event eventToLog)
Expand All @@ -34,42 +39,28 @@ public void SubmitEvents(object StateInfo)
var comsumer = _queue.GetConsumingEnumerable();
var taken = comsumer.Take<Event>(_queue.BoundedCapacity);

string json;
if (taken.Any())
{
json = JsonConvert.SerializeObject(taken);
BulkSubmit(json);
BulkSubmit(taken);
}
}

public void Dispose()
{
_queue.CompleteAdding();
_timer.Dispose();
_queue.Dispose();
_httpClient.Dispose();
}


private void BulkSubmit(string eventsJson)
private async void BulkSubmit(IEnumerable<Event> events)
{
var response = await _httpClient.PostAsJsonAsync("/api/events/bulk", events);

try
{
var url = new Uri(_configuration.BaseUri + "/api/events/bulk");

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "api_key " + _configuration.ApiKey);

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(eventsJson);
streamWriter.Flush();
streamWriter.Close();

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

if (httpResponse.StatusCode != HttpStatusCode.OK)
Logger.Error(string.Format("Error Submitting Events: '{0}'", httpResponse.StatusDescription));
}
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
Expand Down
19 changes: 15 additions & 4 deletions src/LaunchDarkly.Client/LaunchDarkly.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LaunchDarkly.Client</RootNamespace>
<AssemblyName>LaunchDarkly.Client</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,19 +30,26 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
</ItemGroup>
<ItemGroup>
<Compile Include="App_Packages\LibLog.1.4\LibLog.cs" />
<Compile Include="App_Packages\LibLog.4.2\LibLog.cs" />
<Compile Include="Event.cs" />
<Compile Include="EventProcessor.cs" />
<Compile Include="IStoreEvents.cs" />
Expand All @@ -53,7 +61,10 @@
<Compile Include="Feature.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
56 changes: 28 additions & 28 deletions src/LaunchDarkly.Client/LdClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
using System.Net;
using LaunchDarkly.Client.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace LaunchDarkly.Client
{
public class LdClient
public class LdClient : IDisposable
{
private static ILog Logger = LogProvider.For<LdClient>();


private readonly HttpClient _httpClient;
private readonly Configuration _configuration;
private readonly IStoreEvents _eventStore;

public LdClient(Configuration config, IStoreEvents eventStore)
{
_configuration = config;
_eventStore = eventStore;
_httpClient = new HttpClient { BaseAddress = _configuration.BaseUri };
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("api_key", _configuration.ApiKey);
}

public LdClient(Configuration config)
Expand All @@ -25,34 +31,13 @@ public LdClient(Configuration config)
_eventStore = new EventProcessor(_configuration);
}

HttpWebRequest CreateRequest(string key)
{
var url = new Uri(_configuration.BaseUri + string.Format("api/eval/features/{0}", key));
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization, "api_key " + _configuration.ApiKey);

return request;
}

Feature GetFeature(HttpWebResponse response)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var json = reader.ReadToEnd();
return JsonConvert.DeserializeObject<Feature>(json);
}
}

public bool GetFlag(string key, User user, bool defaultValue = false)
public async Task<bool> GetFlag(string key, User user, bool defaultValue = false)
{
try
{
var request = CreateRequest(key);

using (var response = request.GetResponse() as HttpWebResponse)
using (var response = await _httpClient.GetAsync(string.Format("api/eval/features/{0}", key)))
{
var feature = GetFeature(response);

if (response.StatusCode != HttpStatusCode.OK)
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
Expand All @@ -65,18 +50,19 @@ public bool GetFlag(string key, User user, bool defaultValue = false)
}
else
{
Logger.Error("Unexpected status code: " + response.StatusDescription);
Logger.Error("Unexpected status code: " + response.ReasonPhrase);
}
sendFlagRequestEvent(key, user, defaultValue, true);
return defaultValue;
}

var feature = await response.Content.ReadAsAsync<Feature>();
var value = feature.Evaluate(user, defaultValue);
sendFlagRequestEvent(key, user, value, false);
return value;
}
}

catch (Exception ex)
{
Logger.Error("Unhandled exception in LaunchDarkly client" + ex.Message);
Expand All @@ -85,7 +71,6 @@ public bool GetFlag(string key, User user, bool defaultValue = false)
}
}


public void SendEvent(string name, User user, string data)
{
_eventStore.Add(new CustomEvent(name, user, data));
Expand All @@ -95,5 +80,20 @@ private void sendFlagRequestEvent(string key, User user, Boolean value, Boolean
{
_eventStore.Add(new FeatureRequestEvent<Boolean>(key, user, value, usedDefaultValue));
}

protected virtual void Dispose(bool disposing)
{
//We do not have native resource, so the boolean parameter can be ignored.
if (_eventStore is EventProcessor)
((_eventStore) as IDisposable).Dispose();

_httpClient.Dispose();
}
public void Dispose()
{
Dispose(true);

GC.SuppressFinalize(this);
}
}
}
15 changes: 15 additions & 0 deletions src/LaunchDarkly.Client/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>
5 changes: 3 additions & 2 deletions src/LaunchDarkly.Client/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="LibLog" version="1.4.0" targetFramework="net40" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" />
<package id="LibLog" version="4.2.2" targetFramework="net451" developmentDependency="true" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
</packages>
11 changes: 6 additions & 5 deletions src/LaunchDarkly.Tests/LaunchDarkly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Moq">
<Reference Include="Moq, Version=4.2.1409.1722, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog">
<Reference Include="NLog, Version=3.1.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NLog.3.1.0.0\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System">
Expand All @@ -68,9 +71,7 @@
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="app.config" />
<None Include="NLog.xsd">
<SubType>Designer</SubType>
</None>
Expand Down
19 changes: 19 additions & 0 deletions src/LaunchDarkly.Tests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>