1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-18 22:17:44 +02:00

add debug buffer time based access

This commit is contained in:
Asim Aslam
2019-11-27 13:57:19 +00:00
parent 5932dd753c
commit 266b6dbc64
2 changed files with 70 additions and 6 deletions

View File

@ -3,12 +3,18 @@ package buffer
import (
"sync"
"time"
)
type Buffer struct {
size int
sync.RWMutex
vals []interface{}
vals []*Entry
}
type Entry struct {
Value interface{}
Timestamp time.Time
}
func (b *Buffer) Put(v interface{}) {
@ -16,7 +22,10 @@ func (b *Buffer) Put(v interface{}) {
defer b.Unlock()
// append to values
b.vals = append(b.vals, v)
b.vals = append(b.vals, &Entry{
Value: v,
Timestamp: time.Now(),
})
// trim if bigger than size required
if len(b.vals) > b.size {
@ -25,7 +34,7 @@ func (b *Buffer) Put(v interface{}) {
}
// Get returns the last n entries
func (b *Buffer) Get(n int) []interface{} {
func (b *Buffer) Get(n int) []*Entry {
// reset any invalid values
if n > b.size || n < 0 {
n = b.size
@ -46,6 +55,34 @@ func (b *Buffer) Get(n int) []interface{} {
return b.vals[delta:]
}
// Return the entries since a specific time
func (b *Buffer) Since(t time.Time) []*Entry {
b.RLock()
defer b.RUnlock()
// return all the values
if t.IsZero() {
return b.vals
}
// if its in the future return nothing
if time.Since(t).Seconds() < 0.0 {
return nil
}
for i, v := range b.vals {
// find the starting point
d := v.Timestamp.Sub(t)
// return the values
if d.Seconds() > 0.0 {
return b.vals[i:]
}
}
return nil
}
func (b *Buffer) Size() int {
return b.size
}