-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add startup deadline timer * startup deadline
- Loading branch information
1 parent
1230a66
commit be5ce7b
Showing
4 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Proto.Actor/Context/StartupDeadlineContextDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="DeadlineContextDecorator.cs" company="Asynkron AB"> | ||
// Copyright (C) 2015-2022 Asynkron AB All rights reserved | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging; | ||
using Proto.Utils; | ||
|
||
namespace Proto.Context; | ||
|
||
[PublicAPI] | ||
public static class StartupDeadlineContextExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a decorator for a <see cref="ActorContext" /> that logs warning message if Receive takes more time than | ||
/// specified timeout. | ||
/// </summary> | ||
/// <param name="props"></param> | ||
/// <param name="deadline">The timeout for Receive to complete</param> | ||
/// <param name="logger"></param> | ||
/// <returns></returns> | ||
public static Props WithStartupDeadlineDecorator( | ||
this Props props, | ||
TimeSpan deadline, | ||
ILogger logger | ||
) => | ||
props.WithContextDecorator(ctx => new StartupDeadlineContextDecorator(ctx, deadline, logger)); | ||
} | ||
|
||
/// <summary> | ||
/// A decorator for a <see cref="ActorContext" /> that logs warning message if Receive takes more time than specified | ||
/// timeout. | ||
/// </summary> | ||
public class StartupDeadlineContextDecorator : ActorContextDecorator | ||
{ | ||
private readonly IContext _context; | ||
private readonly TimeSpan _deadline; | ||
private readonly ILogger _logger; | ||
|
||
public StartupDeadlineContextDecorator(IContext context, TimeSpan deadline, ILogger logger) : base(context) | ||
{ | ||
_deadline = deadline; | ||
_logger = logger; | ||
_context = context; | ||
} | ||
|
||
public override async Task Receive(MessageEnvelope envelope) | ||
{ | ||
var (m,_,_) = MessageEnvelope.Unwrap(envelope); | ||
if (m is Started) | ||
{ | ||
var t = base.Receive(envelope); | ||
|
||
if (t.IsCompleted) | ||
{ | ||
return; | ||
} | ||
|
||
var ok = await t.WaitUpTo(_deadline).ConfigureAwait(false); | ||
|
||
if (!ok) | ||
{ | ||
_logger.LogWarning("Actor {Self} deadline {Deadline}, exceeded on actor Started", | ||
_context.Self, _deadline); | ||
|
||
// keep waiting, we cannot just ignore and continue as an async task might still be running and updating state of the actor | ||
// if we return here, actor concurrency guarantees could break | ||
await t.ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters