Coder Social home page Coder Social logo

Comments (11)

stuart-beattie avatar stuart-beattie commented on August 18, 2024 1

Ah I wasnt, its just we had two nodes populating records in a shared database table, using this component to generate the ids. Fetching the records, ordering by id and then ordering by creation date yielded different results (despite the clocks being synced). Took me a while to work it out :)

from idgen.

RobThree avatar RobThree commented on August 18, 2024 1

I just noticed Stopwatch.Start() "starts or resumes" a stopwatch. So I could just call _sw.Start() from the StopwatchTimeSource ctor I guess. And then I could initialize the _sw field with just new Stopwatch()...

from idgen.

RobThree avatar RobThree commented on August 18, 2024 1

Will be in 2.1 release soon to be released.

🎉 Yay! See version 2.1 now also on NuGet along with the new IdGen.Configuration package 🎉

from idgen.

RobThree avatar RobThree commented on August 18, 2024

Can you please give an example / testcase so I can reproduce?

This is how I tested:

Console.WriteLine($"Time is now: {DateTime.Now}");
var g = new IdGenerator(0);
var shiftbits = g.MaskConfig.GeneratorIdBits + g.MaskConfig.SequenceBits;
var offset = g.TimeSource.Epoch.ToLocalTime();

Console.WriteLine($"Waiting 10 seconds...");
Thread.Sleep(1000 * 10);

var id = g.CreateId();
Console.WriteLine($"Time is now: {DateTime.Now}, generating ID...");
Console.WriteLine($"Generated id: {id}");

var date = offset + TimeSpan.FromMilliseconds(id >> shiftbits);
Console.WriteLine($"Extracted timestamp: {date}");

And this is my output:

Time is now: 19-2-2020 14:36:59
Waiting 10 seconds...
Time is now: 19-2-2020 14:37:09, generating ID...
Generated id: 679682892149817344
Extracted timestamp: 19-2-2020 14:37:09

As you can see, the generator is instantiated at 14:36:59, an ID is generated at 14:37:09 and when I 'extract' the timestamp from the ID I get that exact value?

from idgen.

stuart-beattie avatar stuart-beattie commented on August 18, 2024

Thanks for getting back on this.

I have just tried your code and I get this following output...

Time is now: 19/02/2020 14:11:24
Waiting 10 seconds...
Time is now: 19/02/2020 14:11:35, generating ID...
Generated id: 679691514317635584
Extracted timestamp: 19/02/2020 14:11:25 +00:00

from idgen.

stuart-beattie avatar stuart-beattie commented on August 18, 2024

Running in a .net core 3.1 console app

from idgen.

RobThree avatar RobThree commented on August 18, 2024

Hmmm. appears to be a difference between referencing the IDGen project and using the NuGet package and between release/debug builds. Your initial reasoning is sound and your suggested workaround works. I'll look into fixing this.

Build Project Reference NuGet package
Debug ✔️
Release

Out of curiosity: why are you 'extracting' the timestamp from the generated ID's? Even though it's documented how an ID is generated, you're supposed to treat is as a "blob" 😉

from idgen.

RobThree avatar RobThree commented on August 18, 2024

That must've been a pain to debug, I bet! Sorry 'bout that! I'll make sure this gets fixed; not sure how yet (I could call CreateID() just before the end of the ctor but that's ugly and may have unintentional side-effects...). I'm going to think about this. Suggestions welcome!

from idgen.

stuart-beattie avatar stuart-beattie commented on August 18, 2024

You could try adding a static constructor to the StopwatchTimeSource class, and instantiating the StopWatch there

from idgen.

RobThree avatar RobThree commented on August 18, 2024

See 1417890

Build Project Reference NuGet package (2.1)
Debug ✔️ ✔️
Release ✔️ ✔️

That seems to fix it. I'll create and release a new NuGet package soon. Thanks for reporting, working and thinking with me on this one!

from idgen.

stuart-beattie avatar stuart-beattie commented on August 18, 2024

Great! no problem, thanks.

from idgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.