Skip to content

Commit 6017f3e

Browse files
authored
fix identify right after init no effect (#19)
* fix identify right after init has no effect * make startup queue thread safe * add console sample * update coroutine version
1 parent c3cba95 commit 6017f3e

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

Analytics-CSharp.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XamarinSample.Droid", "Samp
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XamarinSample.iOS", "Samples\XamarinSample\XamarinSample.iOS\XamarinSample.iOS.csproj", "{39FCFC71-411C-419E-97BB-7C4281764B10}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleSample", "Samples\ConsoleSample\ConsoleSample.csproj", "{51261FC5-F6E0-478C-A466-A026A6CFC972}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -36,6 +38,9 @@ Global
3638
{39FCFC71-411C-419E-97BB-7C4281764B10}.Release|Any CPU.ActiveCfg = Release|iPhoneSimulator
3739
{39FCFC71-411C-419E-97BB-7C4281764B10}.Release|Any CPU.Build.0 = Release|iPhoneSimulator
3840
{39FCFC71-411C-419E-97BB-7C4281764B10}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator
41+
{51261FC5-F6E0-478C-A466-A026A6CFC972}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{51261FC5-F6E0-478C-A466-A026A6CFC972}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{51261FC5-F6E0-478C-A466-A026A6CFC972}.Release|Any CPU.Build.0 = Release|Any CPU
3944
EndGlobalSection
4045
GlobalSection(SolutionProperties) = preSolution
4146
HideSolutionNode = FALSE

Analytics-CSharp/Analytics-CSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<Folder Include="Segment\Analytics\Utilities\" />
3939
</ItemGroup>
4040
<ItemGroup>
41-
<PackageReference Include="Coroutine.NET" Version="1.0.0" />
41+
<PackageReference Include="Coroutine.NET" Version="1.0.1" />
4242
<PackageReference Include="Serialization.NET" Version="1.0.0" />
4343
<PackageReference Include="Sovran.NET" Version="1.0.0" />
4444
</ItemGroup>

Analytics-CSharp/Segment/Analytics/Analytics.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,22 @@ private void Startup(HTTPClient httpClient = null)
241241
Add(new StartupQueue());
242242
Add(new ContextPlugin());
243243

244+
// use Wait() for this coroutine to force completion,
245+
// since Store must be setup before any event call happened.
246+
// Note: Task.Wait() forces internal async methods to run in a synchronized way,
247+
// we should avoid of doing it whenever possible.
244248
analyticsScope.Launch(analyticsDispatcher, async () =>
245249
{
246250
await store.Provide(UserInfo.DefaultState(configuration, storage));
247251
await store.Provide(System.DefaultState(configuration, storage));
248252
await storage.SubscribeToStore();
249-
253+
}).Wait();
254+
255+
// check settings over the network,
256+
// we don't have to Wait() here, because events are piped in
257+
// StartupQueue until settings is ready
258+
analyticsScope.Launch(analyticsDispatcher, async () =>
259+
{
250260
if (configuration.autoAddSegmentDestination)
251261
{
252262
Add(new SegmentDestination());

Analytics-CSharp/Segment/Analytics/Plugins/StartupQueue.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
22
using Segment.Concurrent;
33
using Segment.Sovran;
44

@@ -13,7 +13,7 @@ public class StartupQueue: Plugin, ISubscriber
1313
{
1414
private static int maxSize = 1000;
1515
private readonly AtomicBool _running = new AtomicBool(false);
16-
private readonly Queue<RawEvent> _queuedEvents = new Queue<RawEvent>();
16+
private readonly ConcurrentQueue<RawEvent> _queuedEvents = new ConcurrentQueue<RawEvent>();
1717

1818
public override PluginType type => PluginType.Before;
1919

@@ -34,7 +34,7 @@ public override RawEvent Execute(RawEvent incomingEvent)
3434
if (_queuedEvents.Count >= maxSize)
3535
{
3636
// We've exceeded the max size and need to start dropping events
37-
_queuedEvents.Dequeue();
37+
_queuedEvents.TryDequeue(out _);
3838
}
3939
_queuedEvents.Enqueue(incomingEvent);
4040
return null;
@@ -54,9 +54,12 @@ private void RunningUpdate(System state)
5454

5555
private void ReplayEvents()
5656
{
57-
while (_queuedEvents.Count != 0)
57+
while (!_queuedEvents.IsEmpty)
5858
{
59-
analytics.Process(_queuedEvents.Dequeue());
59+
if (_queuedEvents.TryDequeue(out var e))
60+
{
61+
analytics.Process(e);
62+
}
6063
}
6164
}
6265
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\Analytics-CSharp\Analytics-CSharp.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

Samples/ConsoleSample/Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using System;
4+
using System.Threading;
5+
using Segment.Analytics;
6+
using Segment.Concurrent;
7+
8+
9+
var configuration = new Configuration("YOUR WRITE KEY",
10+
persistentDataPath: "temp",
11+
flushAt: 1,
12+
flushInterval: 10,
13+
exceptionHandler: new ErrorHandler());
14+
var analytics = new Analytics(configuration);
15+
16+
analytics.Identify("foo");
17+
analytics.Track("track right after identify");
18+
19+
Console.ReadLine();
20+
21+
22+
class ErrorHandler : ICoroutineExceptionHandler
23+
{
24+
public void OnExceptionThrown(Exception e)
25+
{
26+
Console.WriteLine(e.StackTrace);
27+
}
28+
}

0 commit comments

Comments
 (0)