Comments (5)
Basically, the MockTimeSource is what you want
The MockTimeSource
doesn't support an epoch/tickDuration, and is also in a separate assembly related to tests.
I think your example code is a little more complicated than necessary
Sure, I can hard-code defaults if necessary.
The problem with this approach is that there's a concurrency issue; when two threads set the time of the mytimesource you're likely to get incorrect results.
This is for a console app performing data imports, so it shouldn't be an issue unless/until I decide to run something concurrently or in parallel. At the moment I'm passing each importer its own IdGenerator instance, so they shouldn't interfere with each other, and I shouldn't see InvalidSystemClockException
s if I keep the imports correctly ordered (unless I'm forgetting something).
I'll see how it goes. 👍
from idgen.
I'm afraid it's a little more complicated than that. We don't use a DateTime
but 'ticks' that can be defined to be anything; be it a millisecond (default), a second or even a day or nanosecond (hardware support etc. permitting). The ITimeSource
returns the number of these 'ticks' since an epoch. With your proposed IdGenerator#CreateFromDateTime(DateTime dateTime)
method it implies the timesource given during initialization (constructor) would be ignored and 'overridden' with the given dateTime
. However, there's no way to 'convert' that value to 'ticks' since a 'tick' can be anything.
I do see a few options on how to 'fix' this. The simplest would be to change the proposed method to IdGenerator#CreateFromDateTime(long ticks)
. That way, we leave it to the caller to figure out how the date/time should be converted to ticks. However, it would still mean a (dummy) timesource would still need to be provided to the constructor (or a default timesource will still be created but unused).
Another option would be to pass an ITimeSource
to the proposed method: IdGenerator#CreateFromDateTime(ITimeSource timeSource)
but then you could just as well pass the timesource to the constructor and 'set' it's date/time to whatever value you want before calling CreateId()
. But that would introduce thread-safety issues.
I see a few other maybe-possibilities but most would require quite a big refactor and breaking changes to the current API IdGen currently provides.
If you have any suggestions they're more than welcome!
from idgen.
I was initially going to suggest IdGenerator#CreateFromTicks
, and then I changed it to CreateFromDateTime
, but I see what the problem is.
or a default timesource will still be created but unused
Not ideal, but I wonder if it's a show stopper?
but then you could just as well pass the timesource to the constructor
The problem with this is that you have to create a new IdGenerator for every ID that you generate, which seems inefficient.
I thought of an alternative: Allow the date-time to be set on the time source directly:
var epoch = new DateTime(2002, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var nextDate = new DateTime(2024, 2, 2, 2, 2, 2, DateTimeKind.Utc);
var idGeneratorOptions = new IdGeneratorOptions(
null,
new TimeSource(epoch, TimeSpan.FromMilliseconds(1)),
SequenceOverflowStrategy.SpinWait);
var generator = new IdGenerator(0, idGeneratorOptions);
var id1 = generator.CreateId();
var decoded1 = generator.FromId(id1);
// Change date-time.
((TimeSource)generator.Options.TimeSource).SetDateTime(nextDate);
var id2 = generator.CreateId();
var decoded2 = generator.FromId(id2);
Implementation
public class TimeSource : ITimeSource
{
public DateTimeOffset Epoch { get; }
public TimeSpan TickDuration { get; }
private DateTimeOffset? dateTime;
private DateTimeOffset DateTime { get => this.dateTime ?? DateTimeOffset.UtcNow; }
public TimeSource(DateTimeOffset epoch, TimeSpan tickDuration)
{
this.Epoch = epoch;
this.TickDuration = tickDuration;
}
public void SetDateTime(DateTimeOffset dateTime)
{
this.dateTime = dateTime;
}
public long GetTicks() => (this.DateTime - this.Epoch).Ticks / this.TickDuration.Ticks;
}
Seems to work.
You can close this issue if you don't think that something similar should be built into the library.
Thanks! 🙂
from idgen.
The problem with this is that you have to create a new IdGenerator for every ID that you generate, which seems inefficient.
No you don't as you figured out yourself. As long as you have an ITimeSource
that lets you set the time for (like MyTimeSource.SetTime(<somedate>)
) you should be good to go. (Basically, the MockTimeSource
is what you want).
I think your example code is a little more complicated than necessary:
var mytimesource = new MyTimeSource();
var idGeneratorOptions = new IdGeneratorOptions(
null,
mytimesource,
SequenceOverflowStrategy.SpinWait
);
var generator = new IdGenerator(0, idGeneratorOptions);
// For each ID to generate "set the time" and create the ID:
mytimesource.SetDateTime(/* <somevalue> */);
var id = generator.CreateId();
var decoded = generator.FromId(id1);
Implementation
public class MyTimeSource : ITimeSource
{
public DateTimeOffset Epoch { get; } = new DateTime(2002, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public TimeSpan TickDuration { get; } = TimeSpan.FromMilliseconds(1);
private DateTimeOffset _dateTime;
public void SetDateTime(DateTimeOffset dateTime) => _dateTime = dateTime;
public long GetTicks() => (_dateTime - Epoch).Ticks / TickDuration.Ticks;
}
Above code is untested.
The problem with this approach is that there's a concurrency issue; when two threads set the time of the mytimesource
you're likely to get incorrect results.
from idgen.
Great 👍
from idgen.
Related Issues (20)
- Drop support for netstandard 1.1 HOT 5
- Non-generic IIdGenerator HOT 5
- Avoid spending a sign bit by using ulongs HOT 13
- Tag 3.0.3 is missing HOT 1
- Documentation about generatorId collisions when id structure changes HOT 9
- IdGen is generating 17bit Id instead of 16bit? What should we change in the method so that it generate 16 bit Id instead of 17 bit now and in the future it continuous to generate 16 bit Id? HOT 11
- Is there a way to shorten the number of digits generated for an id? HOT 5
- Handling uniqueness ID across multiple instance of same service HOT 6
- IdGenerator does not contain a definition for 'Take' HOT 1
- Public Method Comments HOT 1
- SpinWait drastically slows continous ID generation rate for small tick length HOT 10
- Could we log how often we overflow sequence IDs and have to spinwait? HOT 1
- Concurrency issues with instances scale up HOT 2
- Use dependabot to keep references up to date HOT 1
- Sequence input for CreateId() HOT 3
- IIdGenerator is registered as a singleton, but duplicate IDs are still generated HOT 4
- Debugging InvalidSystemClockExceptions HOT 15
- Is there a way to generate id from the past HOT 1
- How to use this in distributed computing ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idgen.