/// <summary> /// 设置下一个buffer /// </summary> /// <param name="idBuffer"></param> public void SetNextBuffer(IdBuffer idBuffer) { //更新阈值 _loadNextBufferThresholdLv1 = idBuffer.Current + (long)((idBuffer.Max - idBuffer.Current) * 0.1); _nextBuffer = idBuffer; _loadingNextBuffer = false; }
public SequencedIdGenerator(SequencedIdGeneratorOptions options, IdBuffer idBuffer) { _options = options; options.MachineRoomId &= 0x1f; _currentBuffer = idBuffer ?? throw new ArgumentNullException(nameof(idBuffer)); _loadNextBufferThresholdLv1 = idBuffer.Current + (long)((idBuffer.Max - idBuffer.Current) * 0.1); }
private async Task LoadNextBufferAsync(string businessId) { var idSequenceBuffer = await _idSequenceStore.GetBufferAsync(businessId); IdBuffer idBuffer = new IdBuffer(idSequenceBuffer.CurrentId, idSequenceBuffer.MaxId); if (_idSequences.TryGetValue(businessId, out SequencedIdGenerator idSequence)) { idSequence.SetNextBuffer(idBuffer); } }
private SequencedIdGenerator CreateIdSequence(string businessId) { var idSequenceBuffer = _idSequenceStore.GetBufferAsync(businessId).ConfigureAwait(true).GetAwaiter().GetResult(); var options = new SequencedIdGeneratorOptions() { UseMachineRoomId = idSequenceBuffer.UseMachineRoomId, MachineRoomId = idSequenceBuffer.MachineRoomId, UseRandomTail = idSequenceBuffer.UseRandomTail, NotifyLoadNextBuffer = NotifyLoadNextBuffer }; IdBuffer idBuffer = new IdBuffer(idSequenceBuffer.CurrentId, idSequenceBuffer.MaxId); SequencedIdGenerator idSequence = new SequencedIdGenerator(options, idBuffer); return(idSequence); }
/// <summary> /// 使用下一个Buffer /// </summary> /// <param name="oldBuffer"></param> private void UseNextBuffer(IdBuffer oldBuffer) { if (oldBuffer == _currentBuffer) { lock (_changeBufferLocker) { if (oldBuffer == _currentBuffer) { if (_nextBuffer != null && _nextBuffer.Current > _currentBuffer.Max) { _currentBuffer = _nextBuffer; _nextBuffer = null; } else { throw new Exception("下一个Id缓冲未就绪!"); } } } } }
public long Next() { if (NeedPrepareNextBuffer()) { SendPrepareNextBufferMessage(); } IdBuffer buffer = _currentBuffer; long newId; if (buffer.TryNext(out newId)) { return(Process(newId)); } else { UseNextBuffer(buffer); buffer = _currentBuffer; if (buffer.TryNext(out newId)) { return(Process(newId)); } throw new Exception("缓冲没有可用Id"); } }