Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Akka.Actor: make ITimeProvider injectable into consuming classes #7314

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked the DateTimeNowTimeProvider as Obsolete and slated for removal in v1.6.

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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, I removed all references to this type (all of which were contained inside Akka.Delivery and Akka.Cluster.Sharding.Delivery) and preferred to use the IScheduler's implementation of these same methods instead, which are identical on the HashedWheelTimer base class.

}
}
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
Loading