Skip to content

Commit

Permalink
Akka.Actor: make ITimeProvider injectable into consuming classes (#…
Browse files Browse the repository at this point in the history
…7314)

* remove `DateTimeNowTimeProvider`

All of this functionality was duplicated on the `IScheduler` anyway - so removing this makes it possible to actually inject the `ITimeProvider` into downstream dependencies using the `ActorSystem`

* added `DateTimeNowTimeProvider` back but marked it as `Obsolete`
  • Loading branch information
Aaronontheweb authored Aug 7, 2024
1 parent acc8bd3 commit da3ded3
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ShardingProducerController(string producerId, IActorRef shardRegion, Opti
ShardRegion = shardRegion;
_durableQueueProps = durableQueueProps;
Settings = settings;
_timeProvider = timeProvider ?? DateTimeOffsetNowTimeProvider.Instance;
_timeProvider = timeProvider ?? Context.System.Scheduler;

WaitingForStart(Option<IActorRef>.None, CreateInitialState(_durableQueueProps.HasValue));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ namespace Akka.Actor
public CoordinatedShutdownExtension() { }
public override Akka.Actor.CoordinatedShutdown CreateExtension(Akka.Actor.ExtendedActorSystem system) { }
}
[System.ObsoleteAttribute("This class will be removed in Akka.NET v1.6.0 - use the IScheduler instead.")]
public class DateTimeOffsetNowTimeProvider : Akka.Actor.IDateTimeOffsetNowTimeProvider, Akka.Actor.ITimeProvider
{
public System.TimeSpan HighResMonotonicClock { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ namespace Akka.Actor
public CoordinatedShutdownExtension() { }
public override Akka.Actor.CoordinatedShutdown CreateExtension(Akka.Actor.ExtendedActorSystem system) { }
}
[System.ObsoleteAttribute("This class will be removed in Akka.NET v1.6.0 - use the IScheduler instead.")]
public class DateTimeOffsetNowTimeProvider : Akka.Actor.IDateTimeOffsetNowTimeProvider, Akka.Actor.ITimeProvider
{
public System.TimeSpan HighResMonotonicClock { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public EventSourcedProducerQueue(string persistenceId, EventSourcedProducerQueue
{
PersistenceId = persistenceId;
Settings = settings ?? EventSourcedProducerQueue.Settings.Create(Context.System);
_timeProvider = timeProvider ?? DateTimeOffsetNowTimeProvider.Instance;
_timeProvider = timeProvider ?? Context.System.Scheduler;
JournalPluginId = Settings.JournalPluginId;
SnapshotPluginId = Settings.SnapshotPluginId;
Self.Tell(EventSourcedProducerQueue.CleanupTick.Instance);
Expand Down
24 changes: 7 additions & 17 deletions src/core/Akka/Actor/Scheduler/DateTimeNowTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,19 @@
namespace Akka.Actor
{
/// <summary>
/// TBD
/// The default <see cref="ITimeProvider"/> implementation for Akka.NET when not testing.
/// </summary>
public class DateTimeOffsetNowTimeProvider : ITimeProvider, IDateTimeOffsetNowTimeProvider
[Obsolete("This class will be removed in Akka.NET v1.6.0 - use the IScheduler instead.")]
public class DateTimeOffsetNowTimeProvider : IDateTimeOffsetNowTimeProvider
{
private DateTimeOffsetNowTimeProvider() { }
/// <summary>
/// TBD
/// </summary>

public DateTimeOffset Now { get { return DateTimeOffset.UtcNow; } }

/// <summary>
/// TBD
/// </summary>

public TimeSpan MonotonicClock {get { return Util.MonotonicClock.Elapsed; }}

/// <summary>
/// TBD
/// </summary>

public TimeSpan HighResMonotonicClock{get { return Util.MonotonicClock.ElapsedHighRes; }}

/// <summary>
/// TBD
/// </summary>

public static DateTimeOffsetNowTimeProvider Instance { get; } = new();
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/core/Akka/Actor/Scheduler/ITimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
namespace Akka.Actor
{
/// <summary>
/// TBD
/// Time provider used by the scheduler to obtain the current time.
/// </summary>
/// <remarks>
/// Intended to be customizable to we can virtualize time for testing purposes.
///
/// In the future we will drop this in favor of the time provider built into .NET 8 and later.
/// </remarks>
public interface ITimeProvider
{
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka/Delivery/Internal/ProducerControllerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ProducerController(string producerId,
ProducerId = producerId;
Settings = settings ?? ProducerController.Settings.Create(Context.System);
_durableProducerQueueProps = durableProducerQueue;
_timeProvider = timeProvider ?? DateTimeOffsetNowTimeProvider.Instance;
_timeProvider = timeProvider ?? Context.System.Scheduler;
_fuzzingControl = fuzzingControl;

// this state gets overridden during the loading sequence, so it's not used at all really
Expand Down Expand Up @@ -86,7 +86,7 @@ public ProducerController(string producerId,
ProducerId = producerId;
Settings = settings ?? ProducerController.Settings.Create(Context.System);
_durableProducerQueueProps = durableProducerQueue;
_timeProvider = timeProvider ?? DateTimeOffsetNowTimeProvider.Instance;
_timeProvider = timeProvider ?? Context.System.Scheduler;
_fuzzingControl = fuzzingControl;

// this state gets overridden during the loading sequence, so it's not used at all really
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka/Delivery/ProducerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ private static Props ProducerControllerProps<T>(ActorSystem actorSystem, string
{
if (sendAdapter == null)
return Props.Create(() => new ProducerController<T>(producerId, durableProducerQueue, settings,
DateTimeOffsetNowTimeProvider.Instance, fuzzing));
actorSystem.Scheduler, fuzzing));
return Props.Create(() => new ProducerController<T>(producerId, durableProducerQueue, sendAdapter, settings,
DateTimeOffsetNowTimeProvider.Instance, fuzzing));
actorSystem.Scheduler, fuzzing));
}

public sealed record Settings
Expand Down

0 comments on commit da3ded3

Please sign in to comment.