auto commit
This commit is contained in:
@@ -54,9 +54,9 @@ namespace Yitter.OrgSystem.TestA
|
||||
|
||||
while (true)
|
||||
{
|
||||
//RunSingle();
|
||||
RunSingle();
|
||||
// Go(options);
|
||||
CallDll();
|
||||
//CallDll();
|
||||
Thread.Sleep(1000); // 每隔1秒执行一次Go
|
||||
}
|
||||
}
|
||||
|
||||
5
ZeOthers/Vlang/source/contract/IIdGenerator.v
Normal file
5
ZeOthers/Vlang/source/contract/IIdGenerator.v
Normal file
@@ -0,0 +1,5 @@
|
||||
module contract
|
||||
|
||||
pub interface IIdGenerator {
|
||||
new_long() u64
|
||||
}
|
||||
5
ZeOthers/Vlang/source/contract/ISnowWorker.v
Normal file
5
ZeOthers/Vlang/source/contract/ISnowWorker.v
Normal file
@@ -0,0 +1,5 @@
|
||||
module contract
|
||||
|
||||
pub interface ISnowWorker {
|
||||
next_id() u64
|
||||
}
|
||||
13
ZeOthers/Vlang/source/contract/IdGeneratorOptions.v
Normal file
13
ZeOthers/Vlang/source/contract/IdGeneratorOptions.v
Normal file
@@ -0,0 +1,13 @@
|
||||
module contract
|
||||
|
||||
pub struct IdGeneratorOptions {
|
||||
pub mut:
|
||||
method u16 =1// 雪花计算方法,(1-漂移算法|2-传统算法),默认1
|
||||
base_time i64// 基础时间,不能超过当前系统时间
|
||||
worker_id u16 =1// 机器码,与 workerid_bitlength 有关系
|
||||
workerid_bitlength byte=6// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22)
|
||||
seq_bitlength byte=6// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22)
|
||||
max_seqnumber u32// 最大序列数(含),(由seq_bitlength计算的最大值)
|
||||
min_seqnumber u32// 最小序列数(含),默认5,不小于1,不大于max_seqnumber
|
||||
top_over_cost_count u32 =2000// 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关)
|
||||
}
|
||||
198
ZeOthers/Vlang/source/core/SnowWorkerM1.v
Normal file
198
ZeOthers/Vlang/source/core/SnowWorkerM1.v
Normal file
@@ -0,0 +1,198 @@
|
||||
module core
|
||||
|
||||
import contract
|
||||
import time
|
||||
import sync
|
||||
|
||||
pub struct SnowWorkerM1{
|
||||
mut:
|
||||
method u16 // 雪花计算方法,(1-漂移算法|2-传统算法),默认1
|
||||
base_time i64 // 基础时间,不能超过当前系统时间
|
||||
worker_id u16 // 机器码,与 workerid_bitlength 有关系
|
||||
workerid_bitlength byte// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22)
|
||||
seq_bitlength byte// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22)
|
||||
max_seqnumber u32 // 最大序列数(含),(由seq_bitlength计算的最大值)
|
||||
min_seqnumber u32 // 最小序列数(含),默认5,不小于1,不大于max_seqnumber
|
||||
top_over_cost_count u32 // 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关)
|
||||
|
||||
timestamp_shift byte
|
||||
current_seqnumber u32
|
||||
last_time_tick i64
|
||||
turn_back_timetick i64
|
||||
turnback_index byte
|
||||
is_over_cost bool
|
||||
overcostcount_inoneterm u32
|
||||
gencount_inoneterm u32
|
||||
term_index u32
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
pub fn make_sf_m1(options &contract.IdGeneratorOptions) &contract.ISnowWorker{
|
||||
worker_id := options.worker_id
|
||||
|
||||
mut workerid_bitlength:=byte(6)
|
||||
if options.workerid_bitlength != 0 {
|
||||
workerid_bitlength = options.workerid_bitlength
|
||||
}
|
||||
mut seq_bitlength:=byte(6)
|
||||
if options.seq_bitlength != 0 {
|
||||
seq_bitlength = options.seq_bitlength
|
||||
}
|
||||
mut max_seqnumber:=u32(0)
|
||||
if options.max_seqnumber > 0 {
|
||||
max_seqnumber = options.max_seqnumber
|
||||
} else {
|
||||
max_seqnumber = (1<<options.seq_bitlength) - 1
|
||||
}
|
||||
min_seqnumber := options.min_seqnumber
|
||||
top_over_cost_count := options.top_over_cost_count
|
||||
|
||||
mut base_time:=i64(0)
|
||||
if options.base_time != 0 {
|
||||
base_time = options.base_time
|
||||
} else {
|
||||
base_time = 1582136402000
|
||||
}
|
||||
timestamp_shift := byte(options.workerid_bitlength + options.seq_bitlength)
|
||||
current_seqnumber := options.min_seqnumber
|
||||
return &SnowWorkerM1{
|
||||
base_time: base_time,
|
||||
worker_id: worker_id,
|
||||
workerid_bitlength: workerid_bitlength,
|
||||
seq_bitlength: seq_bitlength,
|
||||
max_seqnumber: max_seqnumber,
|
||||
min_seqnumber: min_seqnumber,
|
||||
top_over_cost_count: top_over_cost_count,
|
||||
timestamp_shift: timestamp_shift,
|
||||
current_seqnumber: current_seqnumber}
|
||||
}
|
||||
|
||||
// fn (m1 SnowWorkerM1) do_gen_id_action(arg &contract.over_cost_action_arg) {
|
||||
|
||||
// }
|
||||
|
||||
// fn (m1 &SnowWorkerM1) begin_over_cost_action(use_time_tick i64) {
|
||||
|
||||
// }
|
||||
|
||||
fn (mut m1 SnowWorkerM1) end_over_cost_action() {
|
||||
if m1.term_index > 10000 {
|
||||
m1.term_index = 0
|
||||
}
|
||||
}
|
||||
|
||||
// fn (m1 &SnowWorkerM1) begin_turn_back_action(use_time_tick i64) {
|
||||
|
||||
// }
|
||||
|
||||
// fn (m1 &SnowWorkerM1) end_turn_back_action(use_time_tick i64) {
|
||||
|
||||
// }
|
||||
|
||||
fn (mut m1 SnowWorkerM1) next_over_cost_id() u64 {
|
||||
current_time_tick := m1.get_current_time_tick()
|
||||
if current_time_tick > m1.last_time_tick {
|
||||
m1.end_over_cost_action()
|
||||
m1.last_time_tick = current_time_tick
|
||||
m1.current_seqnumber = m1.min_seqnumber
|
||||
m1.is_over_cost = false
|
||||
m1.overcostcount_inoneterm = 0
|
||||
m1.gencount_inoneterm = 0
|
||||
return m1.calc_id()
|
||||
}
|
||||
if m1.overcostcount_inoneterm >= m1.top_over_cost_count {
|
||||
m1.end_over_cost_action()
|
||||
m1.last_time_tick = m1.get_next_time_tick()
|
||||
m1.current_seqnumber = m1.min_seqnumber
|
||||
m1.is_over_cost = false
|
||||
m1.overcostcount_inoneterm = 0
|
||||
m1.gencount_inoneterm = 0
|
||||
return m1.calc_id()
|
||||
}
|
||||
if m1.current_seqnumber > m1.max_seqnumber {
|
||||
m1.last_time_tick++
|
||||
m1.current_seqnumber = m1.min_seqnumber
|
||||
m1.is_over_cost = true
|
||||
m1.overcostcount_inoneterm++
|
||||
m1.gencount_inoneterm++
|
||||
|
||||
return m1.calc_id()
|
||||
}
|
||||
m1.gencount_inoneterm++
|
||||
return m1.calc_id()
|
||||
}
|
||||
|
||||
fn (mut m1 SnowWorkerM1) next_normal_id() u64 {
|
||||
current_time_tick := m1.get_current_time_tick()
|
||||
if current_time_tick < m1.last_time_tick {
|
||||
if m1.turn_back_timetick < 1 {
|
||||
m1.turn_back_timetick = m1.last_time_tick - 1
|
||||
m1.turnback_index++
|
||||
// 每毫秒序列数的前5位是预留位,0用于手工新值,1-4是时间回拨次序
|
||||
// 最多4次回拨(防止回拨重叠)
|
||||
if m1.turnback_index > 4 {
|
||||
m1.turnback_index = 1
|
||||
}
|
||||
// m1.begin_turn_back_action(m1.turn_back_timetick)
|
||||
}
|
||||
return m1.calc_turn_back_id()
|
||||
}
|
||||
// 时间追平时,turn_back_timetick清零
|
||||
if m1.turn_back_timetick > 0 {
|
||||
// m1.end_turn_back_action(m1.turn_back_timetick)
|
||||
m1.turn_back_timetick = 0
|
||||
}
|
||||
if current_time_tick > m1.last_time_tick {
|
||||
m1.last_time_tick = current_time_tick
|
||||
m1.current_seqnumber = m1.min_seqnumber
|
||||
return m1.calc_id()
|
||||
}
|
||||
if m1.current_seqnumber > m1.max_seqnumber {
|
||||
// m1.begin_over_cost_action(current_time_tick)
|
||||
m1.term_index++
|
||||
m1.last_time_tick++
|
||||
m1.current_seqnumber = m1.min_seqnumber
|
||||
m1.is_over_cost = true
|
||||
m1.overcostcount_inoneterm = 1
|
||||
m1.gencount_inoneterm = 1
|
||||
|
||||
return m1.calc_id()
|
||||
}
|
||||
return m1.calc_id()
|
||||
}
|
||||
|
||||
fn (mut m1 SnowWorkerM1) calc_id() u64 {
|
||||
result := u64(m1.last_time_tick<<m1.timestamp_shift) | u64(m1.worker_id<<m1.seq_bitlength) | u64(m1.current_seqnumber)
|
||||
m1.current_seqnumber++
|
||||
return result
|
||||
}
|
||||
|
||||
fn (mut m1 SnowWorkerM1) calc_turn_back_id() u64 {
|
||||
result := u64(m1.turn_back_timetick<<m1.timestamp_shift) | u64(m1.worker_id<<m1.seq_bitlength) | u64(m1.turnback_index)
|
||||
m1.turn_back_timetick--
|
||||
return result
|
||||
}
|
||||
|
||||
fn (m1 &SnowWorkerM1) get_current_time_tick() i64 {
|
||||
return i64(time.now().unix_time_milli()) - m1.base_time
|
||||
}
|
||||
|
||||
fn (m1 &SnowWorkerM1) get_next_time_tick() i64 {
|
||||
mut temp_time_ticker := m1.get_current_time_tick()
|
||||
for temp_time_ticker <= m1.last_time_tick {
|
||||
temp_time_ticker = m1.get_current_time_tick()
|
||||
}
|
||||
return temp_time_ticker
|
||||
}
|
||||
|
||||
pub fn (mut m1 SnowWorkerM1) next_id() u64 {
|
||||
m1.mu.@lock()
|
||||
mut id:=u64(0)
|
||||
if m1.is_over_cost {
|
||||
id= m1.next_over_cost_id()
|
||||
} else {
|
||||
id= m1.next_normal_id()
|
||||
}
|
||||
m1.mu.unlock()
|
||||
return id
|
||||
}
|
||||
38
ZeOthers/Vlang/source/core/SnowWorkerM2.v
Normal file
38
ZeOthers/Vlang/source/core/SnowWorkerM2.v
Normal file
@@ -0,0 +1,38 @@
|
||||
module core
|
||||
|
||||
import contract
|
||||
|
||||
struct SnowWorkerM2{
|
||||
SnowWorkerM1
|
||||
}
|
||||
|
||||
pub fn make_sf_m2(options &contract.IdGeneratorOptions) &contract.ISnowWorker{
|
||||
m1:=make_sf_m1(options)
|
||||
if m1 is SnowWorkerM1{
|
||||
return &SnowWorkerM2{
|
||||
m1
|
||||
}
|
||||
}
|
||||
return &SnowWorkerM2{}
|
||||
}
|
||||
|
||||
pub fn (mut m2 SnowWorkerM2) next_id()u64{
|
||||
m2.mu.@lock()
|
||||
mut current_time_tick:=m2.get_current_time_tick()
|
||||
if m2.last_time_tick==current_time_tick{
|
||||
m2.current_seqnumber=(m2.current_seqnumber+1) & m2.max_seqnumber
|
||||
if m2.current_seqnumber==0{
|
||||
m2.current_seqnumber=m2.min_seqnumber
|
||||
current_time_tick=m2.get_next_time_tick()
|
||||
}
|
||||
}else{
|
||||
m2.current_seqnumber=m2.min_seqnumber
|
||||
}
|
||||
if current_time_tick < m2.last_time_tick {
|
||||
println("Time error for "+(m2.last_time_tick-current_time_tick).str()+" milliseconds")
|
||||
}
|
||||
m2.last_time_tick=current_time_tick
|
||||
id:= u64(current_time_tick<<m2.timestamp_shift) | u64(m2.worker_id<<m2.seq_bitlength) | u64(m2.current_seqnumber)
|
||||
m2.mu.unlock()
|
||||
return id
|
||||
}
|
||||
67
ZeOthers/Vlang/source/gen/DefaultIdGenerator.v
Normal file
67
ZeOthers/Vlang/source/gen/DefaultIdGenerator.v
Normal file
@@ -0,0 +1,67 @@
|
||||
module gen
|
||||
|
||||
import contract
|
||||
import core
|
||||
import time
|
||||
|
||||
pub struct DefaultIdGenerator {
|
||||
mut:
|
||||
options &contract.IdGeneratorOptions
|
||||
snow_worker &contract.ISnowWorker
|
||||
}
|
||||
|
||||
pub fn make_generator(options &contract.IdGeneratorOptions) &DefaultIdGenerator {
|
||||
min_time := i64(631123200000)
|
||||
if options.base_time < min_time || options.base_time > time.now().unix_time_milli() {
|
||||
panic("base_time error.")
|
||||
}
|
||||
|
||||
if options.seq_bitlength+options.workerid_bitlength > 22 {
|
||||
panic("error:workerid_bitlength + seq_bitlength <= 22")
|
||||
}
|
||||
|
||||
max_workerid_number := 1<<options.workerid_bitlength - 1
|
||||
if options.worker_id > max_workerid_number {
|
||||
panic("WorkerId error. (range:[1, " + max_workerid_number.str() + "]")
|
||||
}
|
||||
|
||||
if options.seq_bitlength < 2 || options.seq_bitlength > 21 {
|
||||
panic("seq_bitlength error. (range:[2, 21])")
|
||||
}
|
||||
|
||||
max_seqnumber := 1<<options.seq_bitlength - 1
|
||||
if options.max_seqnumber > max_seqnumber {
|
||||
panic("MaxSeqNumber error. (range:[1, " + max_seqnumber.str() + "]")
|
||||
}
|
||||
|
||||
if options.min_seqnumber > max_seqnumber {
|
||||
panic("MinSeqNumber error. (range:[1, " + max_seqnumber.str() + "]")
|
||||
}
|
||||
|
||||
match options.method {
|
||||
1 {
|
||||
return &DefaultIdGenerator{
|
||||
options: options,
|
||||
snow_worker: core.make_sf_m1(options),
|
||||
}
|
||||
}
|
||||
2 {
|
||||
return &DefaultIdGenerator{
|
||||
options: options,
|
||||
snow_worker: core.make_sf_m2(options),
|
||||
}
|
||||
}
|
||||
else {
|
||||
return &DefaultIdGenerator{
|
||||
options: options,
|
||||
snow_worker: core.make_sf_m1(options),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
pub fn (mut dig DefaultIdGenerator) new_long() u64 {
|
||||
return dig.snow_worker.next_id()
|
||||
}
|
||||
12
ZeOthers/Vlang/source/gen/YitIdHelper.v
Normal file
12
ZeOthers/Vlang/source/gen/YitIdHelper.v
Normal file
@@ -0,0 +1,12 @@
|
||||
module gen
|
||||
|
||||
import contract
|
||||
|
||||
pub struct YitIdHelper {
|
||||
mut:
|
||||
id_gen contract.IIdGenerator
|
||||
}
|
||||
|
||||
pub fn (yih &YitIdHelper) next_id() u64 {
|
||||
return yih.id_gen.new_long()
|
||||
}
|
||||
BIN
ZeOthers/Vlang/source/test
Normal file
BIN
ZeOthers/Vlang/source/test
Normal file
Binary file not shown.
31
ZeOthers/Vlang/source/test.v
Normal file
31
ZeOthers/Vlang/source/test.v
Normal file
@@ -0,0 +1,31 @@
|
||||
module main
|
||||
import time
|
||||
import contract
|
||||
import gen
|
||||
|
||||
fn main(){
|
||||
// 方法一:直接采用默认方法生成一个Id
|
||||
yid := gen.YitIdHelper{
|
||||
id_gen: gen.make_generator(&contract.IdGeneratorOptions{
|
||||
method: 1
|
||||
base_time: 1582136402000
|
||||
workerid_bitlength:6
|
||||
seq_bitlength:6
|
||||
})
|
||||
}
|
||||
println(yid.next_id())
|
||||
|
||||
// 方法二:自定义参数
|
||||
|
||||
times := 50000
|
||||
|
||||
for {
|
||||
begin := time.now().unix_time_milli()
|
||||
for i := 0; i < times; i++ {
|
||||
yid.next_id()
|
||||
}
|
||||
end := time.now().unix_time_milli()
|
||||
println("漂移,总共:"+times.str()+","+(end-begin).str()+" ms")
|
||||
time.sleep(1 * time.second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user