Пример #1
0
func New(slotSize, slotCount int) (ringbuffer.RingBuffer, error) {
	if slotSize < MinSlotSize {
		return nil, ErrSlotSizeTooSmall
	}

	if slotCount > MaxDataSlots {
		return nil, ErrMaxDataSlotsExceeded
	}

	if !ringbuffer.PowerOfTwo(slotCount) {
		return nil, ErrNotPowerOfTwo
	}

	slotSize += SlotOverhead

	d := &byteBuffer{
		slotSize:   slotSize,
		slotCount:  slotCount,
		slotMask:   slotCount - 1,
		bufferSize: int64(slotSize * slotCount),
		buffer:     make([]byte, slotSize*slotCount),
		producers:  make([]*producer, 0),
		consumers:  make([]*consumer, 0),
	}

	return d, nil
}
Пример #2
0
func NewConsumer(bufferSize int) (ringbuffer.Sequencer, error) {
	if !ringbuffer.PowerOfTwo(int(bufferSize)) {
		return nil, ErrNotPowerOfTwo
	}

	s := &Consumer{}
	s.cursor = InitialSequenceValue
	s.cachedGate = InitialSequenceValue
	s.bufferSize = bufferSize

	return s, nil
}