Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Optimized portion of id used for millis #206
Browse files Browse the repository at this point in the history
  • Loading branch information
waldeinburg authored and mp911de committed Sep 11, 2019
1 parent 73be0bc commit 174cb94
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,11 @@ long generateMsgId() {
// will collide with old timestamps? Every second Graylog will evict expired messaged (5
// seconds old) from the pool:
// https://github.com/Graylog2/graylog2-server/blob/master/graylog2-server/src/main/java/org/graylog2/inputs/codecs/GelfChunkAggregator.java
// Thus, we just need six seconds which will require two bytes. Then we can spend six bytes
// on a random number.
// Thus, we just need six seconds which will require 13 bits. Then we can spend the rest on
// a random number.

return (getRandomLong() & 0xFFFFFFFFFFFF0000L) |
(getCurrentTimeMillis() & 0xFFFFL);
return (getRandomLong() & 0xFFFFFFFFFFFFE000L) |
(getCurrentTimeMillis() & 0x1FFFL);
}

long getRandomLong() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@ void testGenerateMsgId() {
GelfMessage gelfMessage = new GelfMessage() {
@Override
long getRandomLong() {
return 0x8040201008040201L;
return 0x804020100804A201L;
}

@Override
long getCurrentTimeMillis() {
return 0x90C06030090C8683L;
return 0x90C06030090C1683L;
}
};

assertThat(gelfMessage.generateMsgId()).isEqualTo(0x8040201008048683L);
assertThat(gelfMessage.generateMsgId()).isEqualTo(0x804020100804B683L);
}

String toString(ByteBuffer allocate) {
Expand Down

0 comments on commit 174cb94

Please sign in to comment.