Skip to content

Commit

Permalink
chore: refactor after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
IronCore864 committed Oct 17, 2024
1 parent c68f72c commit cffde67
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions internals/servicelog/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ func (rb *RingBuffer) Close() error {
func (rb *RingBuffer) Closed() bool {
rb.rwlock.RLock()
defer rb.rwlock.RUnlock()
return rb.closed()
}

func (rb *RingBuffer) closed() bool {
return rb.writeClosed
}

Expand Down Expand Up @@ -166,10 +162,6 @@ func (rb *RingBuffer) Size() int {
func (rb *RingBuffer) Positions() (start RingPos, end RingPos) {
rb.rwlock.RLock()
defer rb.rwlock.RUnlock()
return rb.positions()
}

func (rb *RingBuffer) positions() (start RingPos, end RingPos) {
return rb.readIndex, rb.writeIndex
}

Expand Down Expand Up @@ -253,14 +245,14 @@ func (rb *RingBuffer) TailIterator() Iterator {
defer rb.rwlock.RUnlock()
rb.iteratorMutex.Lock()
defer rb.iteratorMutex.Unlock()
start, _ := rb.positions()
start := rb.readIndex
iter := &iterator{
rb: rb,
index: start,
nextChan: make(chan bool, 1),
closeChan: make(chan struct{}),
}
if rb.closed() {
if rb.writeClosed {
close(iter.closeChan)
}
rb.iteratorList = append(rb.iteratorList, iter)
Expand All @@ -271,9 +263,9 @@ func (rb *RingBuffer) TailIterator() Iterator {
// If lines is greater than zero, the iterator will start that many lines
// backwards from the head.
func (rb *RingBuffer) HeadIterator(lines int) Iterator {
firstLine := rb.reverseLinePosition(lines)
rb.rwlock.RLock()
defer rb.rwlock.RUnlock()
firstLine := rb.reverseLinePosition(lines)
rb.iteratorMutex.Lock()
defer rb.iteratorMutex.Unlock()
iter := &iterator{
Expand All @@ -282,16 +274,14 @@ func (rb *RingBuffer) HeadIterator(lines int) Iterator {
nextChan: make(chan bool, 1),
closeChan: make(chan struct{}),
}
if rb.closed() {
if rb.writeClosed {
close(iter.closeChan)
}
rb.iteratorList = append(rb.iteratorList, iter)
return iter
}

func (rb *RingBuffer) reverseLinePosition(n int) RingPos {
rb.rwlock.RLock()
defer rb.rwlock.RUnlock()
if n <= 0 {
return rb.writeIndex
}
Expand Down

0 comments on commit cffde67

Please sign in to comment.