func printRing(r *ring.Ring) { elements := make([]interface{}, 0, r.Len()) r.Do(func(i interface{}) { elements = append(elements, i) }) log.Printf("%v", elements) }
func iterateRing(rng *ring.Ring, out chan Pair) { i := 0 rng.Do(func(ringCurrent interface{}) { out <- Pair{i, ringCurrent} i++ }) }
func removeFromRing(r *ring.Ring, s string) *ring.Ring { newRing := ring.New(0) r.Do(func(v interface{}) { if v != s { newRing = addToRing(newRing, v.(string)) } }) return newRing }
func (rb *ringBuffer) read(lines int) []string { if lines <= 0 { return []string{} } // Only need a read lock because nothing we're about to do affects the internal state of the // ringBuffer. Mutliple reads can happen in parallel. Only writing requires an exclusive lock. rb.mutex.RLock() defer rb.mutex.RUnlock() var start *ring.Ring if lines < rb.ring.Len() { start = rb.ring.Move(-1 * (lines - 1)) } else { start = rb.ring.Next() } data := make([]string, 0, lines) start.Do(func(line interface{}) { if line == nil || lines <= 0 { return } lines-- data = append(data, line.(string)) }) return data }