Coder Social home page Coder Social logo

Comments (11)

Caskia avatar Caskia commented on September 28, 2024 1

it's solved 😄

from idgen.

RobThree avatar RobThree commented on September 28, 2024

Which version are you using? (See #18)

The nodes aren't (usually) supposed to know about eachother; however: to synchronise their clocks you could/should use NTP.

from idgen.

Caskia avatar Caskia commented on September 28, 2024

I have read this post #18 before.
version: 2.1.0

You can try this code:

var idGenerator1 = new IdGen.IdGenerator(10, new DateTime(2017, 12, 8, 0, 0, 0, DateTimeKind.Utc));

await Task.Delay(20 * 1000);
var idGenerator2 = new IdGen.IdGenerator(9, new DateTime(2017, 12, 8, 0, 0, 0, DateTimeKind.Utc));

var list = new List<(int, long)>();
list.Add((0, idGenerator1.CreateId()));
list.Add((1, idGenerator2.CreateId()));

await Task.Delay(1);

list.Add((2, idGenerator2.CreateId()));
await Task.Delay(5);
list.Add((3, idGenerator1.CreateId()));

the data in list index2 is always greater than index3

from idgen.

RobThree avatar RobThree commented on September 28, 2024

Uh... you may have found a bug...

I'm currently looking into it. As a workaround, you may try this:

var ts = new DefaultTimeSource(new DateTime(2017, 12, 8, 0, 0, 0, DateTimeKind.Utc), TimeSpan.FromMilliseconds(1));
	
var idGenerator1 = new IdGen.IdGenerator(10, ts);
System.Threading.Thread.Sleep(20 * 1000);
var idGenerator2 = new IdGen.IdGenerator(9, ts);

This seems to ensure the same timesource is used.

from idgen.

RobThree avatar RobThree commented on September 28, 2024

Hmm, ok, so here's a bit of undefined behaviour I hadn't thought about...

It took me a while to realize... Every IdGenerator shares the same, underlying, stopwatch which is used as a timesource. However, each IdGenerator gets it's own instance of a (Default)TimeSource. And those stored, internally, an offset (when it is created) which was specific to the instance but should be shared among instances too.

In some perspective it's 'correct' behavior but I don't think it's very intuitive (hence your issue). So I've fixed it in 3935623 and will push an update to NuGet shortly.

from idgen.

RobThree avatar RobThree commented on September 28, 2024

I have pushed v2.2 to NuGet (may be a few minutes before it's available) should be available now. Please let me know if you still experience issues with this version.

Thanks for reporting this issue! 👍

from idgen.

Caskia avatar Caskia commented on September 28, 2024

That's great, thank you for such a quick response, I will have a try.

from idgen.

Caskia avatar Caskia commented on September 28, 2024

Why not use static constructor to start Stopwatch? Every DefaultTimeSource instances will start Stopwatch, although it will not have any impact.

from idgen.

RobThree avatar RobThree commented on September 28, 2024

Because CA1810: Initialize reference type static fields inline 😉
Yes, the stopwatch is 'started' (actually 'resumed') every new instance but, as you said, it doesn't have any impact.
If you have sound reasoning for moving to a static constructor I'm open for discussion 😉

from idgen.

Caskia avatar Caskia commented on September 28, 2024

Thank you for giving me a lesson.

from idgen.

RobThree avatar RobThree commented on September 28, 2024

You're welcome. But more importantly: Is your problem solved now? 😉

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.