Skip to content

Commit

Permalink
chore: yggdrasil (#224)
Browse files Browse the repository at this point in the history
Swaps out the core logic for evaluating features with the Yggdrasil engine
  • Loading branch information
nunogois authored Oct 28, 2024
1 parent 2288065 commit 07e45bc
Show file tree
Hide file tree
Showing 37 changed files with 314 additions and 741 deletions.
5 changes: 2 additions & 3 deletions src/Unleash/ClientFactory/IUnleashClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Unleash.Strategies;
using System.Threading.Tasks;
using Yggdrasil;

namespace Unleash.ClientFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Unleash/ClientFactory/UnleashClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Unleash.Strategies;
using Yggdrasil;

namespace Unleash.ClientFactory
{
Expand Down
6 changes: 2 additions & 4 deletions src/Unleash/Communication/FetchTogglesResult.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Unleash.Internal;

namespace Unleash.Communication
namespace Unleash.Communication
{
internal class FetchTogglesResult
{
public ToggleCollection ToggleCollection { get; set; }
public bool HasChanged { get; set; }
public string Etag { get; set; }
public string State { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Unleash/Communication/IUnleashApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal interface IUnleashApiClient
{
Task<FetchTogglesResult> FetchToggles(string etag, CancellationToken cancellationToken, bool throwOnFail = false);
Task<bool> RegisterClient(ClientRegistration registration, CancellationToken cancellationToken);
Task<bool> SendMetrics(ThreadSafeMetricsBucket metrics, CancellationToken cancellationToken);
// TODO: Can be simplified to `using Yggdrasil;` once MetricsBucket is dropped from Unleash.Metrics
Task<bool> SendMetrics(Yggdrasil.MetricsBucket metrics, CancellationToken cancellationToken);
}
}
25 changes: 10 additions & 15 deletions src/Unleash/Communication/UnleashApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,12 @@ private async Task<FetchTogglesResult> HandleSuccessResponse(HttpResponseMessage
{
HasChanged = false,
Etag = newEtag,
ToggleCollection = null,
};
}

var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
var toggleCollection = jsonSerializer.Deserialize<ToggleCollection>(stream);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

if (toggleCollection == null)
if (string.IsNullOrEmpty(content))
{
return new FetchTogglesResult
{
Expand All @@ -177,7 +175,7 @@ private async Task<FetchTogglesResult> HandleSuccessResponse(HttpResponseMessage
{
HasChanged = true,
Etag = newEtag,
ToggleCollection = toggleCollection
State = content
};
}

Expand Down Expand Up @@ -212,7 +210,7 @@ public async Task<bool> RegisterClient(ClientRegistration registration, Cancella
}
}

public async Task<bool> SendMetrics(ThreadSafeMetricsBucket metrics, CancellationToken cancellationToken)
public async Task<bool> SendMetrics(Yggdrasil.MetricsBucket metrics, CancellationToken cancellationToken)
{
if (metricsRequestsToSkip > metricsRequestsSkipped)
{
Expand All @@ -226,16 +224,13 @@ public async Task<bool> SendMetrics(ThreadSafeMetricsBucket metrics, Cancellatio

var memoryStream = new MemoryStream();

using (metrics.StopCollectingMetrics(out var bucket))
jsonSerializer.Serialize(memoryStream, new ClientMetrics
{
jsonSerializer.Serialize(memoryStream, new ClientMetrics
{
AppName = clientRequestHeaders.AppName,
InstanceId = clientRequestHeaders.InstanceTag,
Bucket = bucket
});
memoryStream.Position = 0;
}
AppName = clientRequestHeaders.AppName,
InstanceId = clientRequestHeaders.InstanceTag,
Bucket = metrics
});
memoryStream.Position = 0;

const int bufferSize = 1024 * 4;

Expand Down
Loading

0 comments on commit 07e45bc

Please sign in to comment.