Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
f1f88d82
Commit
f1f88d82
authored
Oct 07, 2018
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed SnowflakeId workerId to random id.
parent
77a0ba30
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
6 deletions
+13
-6
SnowflakeId.cs
src/DotNetCore.CAP/Infrastructure/SnowflakeId.cs
+13
-6
No files found.
src/DotNetCore.CAP/Infrastructure/SnowflakeId.cs
View file @
f1f88d82
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
// An object that generates IDs. This is broken into a separate class in case we ever want to support multiple worker threads per process
// An object that generates IDs. This is broken into a separate class in case we ever want to support multiple worker threads per process
using
System
;
using
System
;
using
System.Threading
;
namespace
DotNetCore.CAP.Infrastructure
namespace
DotNetCore.CAP.Infrastructure
{
{
...
@@ -24,10 +23,10 @@ namespace DotNetCore.CAP.Infrastructure
...
@@ -24,10 +23,10 @@ namespace DotNetCore.CAP.Infrastructure
private
static
SnowflakeId
_snowflakeId
;
private
static
SnowflakeId
_snowflakeId
;
private
readonly
object
_lock
=
new
object
();
private
readonly
object
_lock
=
new
object
();
private
static
readonly
object
s_l
ock
=
new
object
();
private
static
readonly
object
SL
ock
=
new
object
();
private
long
_lastTimestamp
=
-
1L
;
private
long
_lastTimestamp
=
-
1L
;
p
rivate
SnowflakeId
(
long
workerId
,
long
datacenterId
,
long
sequence
=
0L
)
p
ublic
SnowflakeId
(
long
workerId
,
long
datacenterId
,
long
sequence
=
0L
)
{
{
WorkerId
=
workerId
;
WorkerId
=
workerId
;
DatacenterId
=
datacenterId
;
DatacenterId
=
datacenterId
;
...
@@ -46,11 +45,19 @@ namespace DotNetCore.CAP.Infrastructure
...
@@ -46,11 +45,19 @@ namespace DotNetCore.CAP.Infrastructure
public
long
Sequence
{
get
;
internal
set
;
}
public
long
Sequence
{
get
;
internal
set
;
}
public
static
SnowflakeId
Default
(
long
datacenterId
=
0
)
public
static
SnowflakeId
Default
()
{
{
lock
(
s_l
ock
)
lock
(
SL
ock
)
{
{
return
_snowflakeId
??
(
_snowflakeId
=
new
SnowflakeId
(
Thread
.
CurrentThread
.
ManagedThreadId
,
datacenterId
));
if
(
_snowflakeId
!=
null
)
{
return
_snowflakeId
;
}
var
random
=
new
Random
();
var
workerId
=
random
.
Next
((
int
)
MaxWorkerId
);
var
datacenterId
=
random
.
Next
((
int
)
MaxDatacenterId
);
return
_snowflakeId
=
new
SnowflakeId
(
workerId
,
datacenterId
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment