Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Fixed retry and device verification
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-dev004 committed Jun 17, 2022
1 parent 8225eaf commit 6958ff7
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 84 deletions.
33 changes: 33 additions & 0 deletions COPYRIGHT_THIRD_PARTY_SOFTWARE_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,39 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
## Polly
---

<pre>
New BSD License
=
Copyright (c) 2015-2020, App vNext
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of App vNext nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>

---
## (Sample Code) Exposure Notifications API: Android Reference Design
---
Expand Down
32 changes: 32 additions & 0 deletions Covid19Radar/Covid19Radar.Android/Assets/license.html
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,38 @@ <h2>System.IdentityModel.Tokens.Jwt</h2>
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr />
<h2>Polly</h2>
<hr />
<pre>
New BSD License
=
Copyright (c) 2015-2020, App vNext
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of App vNext nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>

<hr />
<h2>(Sample Code) Exposure Notifications API: Android Reference Design</h2>
<hr />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public override Result DoWork()
{
eventLogService.SendAllAsync(
AppConstants.EventLogMaxRequestSizeInBytes,
AppConstants.EventLogMaxRetry,
AppConstants.EventLogRetryInternval);
AppConstants.EventLogMaxRetry);
return Result.InvokeSuccess();
}
catch (Exception exception)
Expand Down
32 changes: 32 additions & 0 deletions Covid19Radar/Covid19Radar.iOS/Resources/license.html
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,38 @@ <h2>System.IdentityModel.Tokens.Jwt</h2>
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr />
<h2>Polly</h2>
<hr />
<pre>
New BSD License
=
Copyright (c) 2015-2020, App vNext
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of App vNext nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>

<hr />
<h2>(Sample Code) Exposure Notifications API: Android Reference Design</h2>
<hr />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ private void HandleSendLogAsync(BGAppRefreshTask task)
{
await _eventLogService.SendAllAsync(
AppConstants.EventLogMaxRequestSizeInBytes,
AppConstants.EventLogMaxRetry,
AppConstants.EventLogRetryInternval);
AppConstants.EventLogMaxRetry);
task.SetTaskCompleted(true);
}
catch (OperationCanceledException exception)
Expand Down
7 changes: 1 addition & 6 deletions Covid19Radar/Covid19Radar/Common/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@ public static readonly DateTime COCOA_FIRST_RELEASE_DATE
/// <summary>
/// Number of retries to send event log.
/// </summary>
public const int EventLogMaxRetry = 4;

/// <summary>
/// Retry interval for sending event logs.
/// </summary>
public const int EventLogRetryInternval = 1000;
public const int EventLogMaxRetry = 3;

#region Other Private Methods

Expand Down
2 changes: 2 additions & 0 deletions Covid19Radar/Covid19Radar/Covid19Radar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<EmbeddedResource Remove="Controls\**" />
<None Remove="Controls\**" />
<None Remove="ViewModels\ExposureCheckPage\" />
<None Remove="Polly" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -72,6 +73,7 @@
<PackageReference Include="Chino.Common" Version="1.0.0-rc11" />
<PackageReference Include="Prism.DryIoc.Extensions" Version="8.0.62" />
<PackageReference Include="TimeZoneConverter" Version="5.0.0" />
<PackageReference Include="Polly" Version="7.2.3" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\CustomDatePicker.cs" />
Expand Down
70 changes: 41 additions & 29 deletions Covid19Radar/Covid19Radar/Services/EventLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
using Covid19Radar.Model;
using Covid19Radar.Repository;
using Covid19Radar.Services.Logs;
using Polly;

namespace Covid19Radar.Services
{
public interface IEventLogService
{
public Task SendAllAsync(long maxSize, int maxRetry, int retryInterval);
public Task SendAllAsync(long maxSize, int maxRetry);
}

public class EventLogService : IEventLogService
Expand Down Expand Up @@ -47,21 +48,21 @@ ILoggerService loggerService
_loggerService = loggerService;
}

public async Task SendAllAsync(long maxSize, int maxRetry, int retryInterval)
public async Task SendAllAsync(long maxSize, int maxRetry)
{
await _semaphore.WaitAsync();

try
{
await SendAllInternalAsync(maxSize, maxRetry, retryInterval);
await SendAllInternalAsync(maxSize, maxRetry);
}
finally
{
_semaphore.Release();
}
}

private async Task SendAllInternalAsync(long maxSize, int maxRetry, int retryInterval)
private async Task SendAllInternalAsync(long maxSize, int maxRetry)
{
_loggerService.StartMethod();

Expand Down Expand Up @@ -98,33 +99,27 @@ List<EventLog> agreedEventLogList
return;
}

int tries = 0;
while (true)
{
bool isSuccess = await SendAsync(idempotencyKey, agreedEventLogList);

if (isSuccess)
{
_loggerService.Info($"Event log send successful.");

_loggerService.Info($"Clean up...");
foreach (var eventLog in eventLogList)
{
await _eventLogRepository.RemoveAsync(eventLog);
}
PolicyResult<bool> policyResult = await Policy
.HandleResult<bool>(result => !result)
.WaitAndRetryAsync(maxRetry, retryAttempt => {
double delay = Math.Pow(2, retryAttempt - 1);
_loggerService.Warning($"Event log send failed. retryAttempt:{retryAttempt} delay:{delay}sec");
return TimeSpan.FromSeconds(delay);
})
.ExecuteAndCaptureAsync(() => SendAsync(idempotencyKey, agreedEventLogList));

break;
}
else if (tries >= maxRetry)
{
_loggerService.Error("Event log send failed all.");
break;
}
if (policyResult.Outcome == OutcomeType.Failure)
{
_loggerService.Error("Event log send failed all.");
return;
}

_loggerService.Warning($"Event log send failed. tries:{tries + 1}");
await Task.Delay(retryInterval);
_loggerService.Info($"Event log send successful.");

tries++;
_loggerService.Info($"Clean up...");
foreach (var eventLog in eventLogList)
{
await _eventLogRepository.RemoveAsync(eventLog);
}

_loggerService.Info($"Done.");
Expand All @@ -149,7 +144,24 @@ private async Task<bool> SendAsync(string idempotencyKey, List<EventLog> eventLo
EventLogs = eventLogList,
};

request.DeviceVerificationPayload = await _deviceVerifier.VerifyAsync(request);
// Create device verification payload
PolicyResult<string> policyResult = await Policy
.HandleResult<string>(result => _deviceVerifier.IsErrorPayload(result))
.WaitAndRetryAsync(3, retryAttempt => {
double delay = Math.Pow(2, retryAttempt + 1);
_loggerService.Warning($"Payload creation failed. retryAttempt:{retryAttempt} delay:{delay}sec");
return TimeSpan.FromSeconds(delay);
})
.ExecuteAndCaptureAsync(() => _deviceVerifier.VerifyAsync(request));

if (policyResult.Outcome == OutcomeType.Failure)
{
_loggerService.Error("Payload creation failed all.");
return false;
}

_loggerService.Info("Payload creation successful.");
request.DeviceVerificationPayload = policyResult.Result;

ApiResponse<string> response = await _httpDataService.PutEventLog(request);
_loggerService.Info($"PutEventLog() StatusCode:{response.StatusCode}");
Expand Down
2 changes: 1 addition & 1 deletion Covid19Radar/Covid19Radar/Services/EventLogServiceNop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Covid19Radar.Services
{
public class EventLogServiceNop : IEventLogService
{
public Task SendAllAsync(long maxSize, int maxRetry, int retryInterval)
public Task SendAllAsync(long maxSize, int maxRetry)
{
// do nothing
return Task.FromResult(new List<EventLog>());
Expand Down
Loading

0 comments on commit 6958ff7

Please sign in to comment.