GetBufferedSamples returns the currently buffered metric samples and makes a new internal buffer with some hopefully realistic size. If the internal buffer is empty, it will return nil.
()
| 32 | // new internal buffer with some hopefully realistic size. If the internal |
| 33 | // buffer is empty, it will return nil. |
| 34 | func (sc *SampleBuffer) GetBufferedSamples() []metrics.SampleContainer { |
| 35 | sc.Lock() |
| 36 | defer sc.Unlock() |
| 37 | |
| 38 | buffered, bufferedLen := sc.buffer, len(sc.buffer) |
| 39 | if bufferedLen == 0 { |
| 40 | return nil |
| 41 | } |
| 42 | if bufferedLen > sc.maxLen { |
| 43 | sc.maxLen = bufferedLen |
| 44 | } |
| 45 | // Make the new buffer halfway between the previously allocated size and the |
| 46 | // maximum buffer size we've seen so far, to hopefully reduce copying a bit. |
| 47 | sc.buffer = make([]metrics.SampleContainer, 0, (bufferedLen+sc.maxLen)/2) |
| 48 | |
| 49 | return buffered |
| 50 | } |
| 51 | |
| 52 | // PeriodicFlusher is a small helper for asynchronously flushing buffered metric |
| 53 | // samples on regular intervals. The biggest benefit is having a Stop() method |