storj/cmd/statreceiver/copy.go
JT Olio 362f447d9f
cmd/statreceiver: lua-scriptable stat receiver (#636)
* cmd/statreceiver: lua-scriptable stat receiver

Change-Id: I3ce0fe3f1ef4b1f4f27eed90bac0e91cfecf22d7

* some updates

Change-Id: I7c3485adcda1278fce01ae077b4761b3ddb9fb7a

* more comments

Change-Id: I0bb22993cd934c3d40fc1da80d07e49e686b80dd

* linter fixes

Change-Id: Ied014304ecb9aadcf00a6b66ad28f856a428d150

* catch errors

Change-Id: I6e1920f1fd941e66199b30bc427285c19769fc70

* review feedback

Change-Id: I9d4051851eab18970c5f5ddcf4ff265508e541d3

* errorgroup improvements

Change-Id: I4699dda3022f0485fbb50c9dafe692d3921734ff

* too tricky

the previous thing was better for memory with lots of errors at a time
but https://play.golang.org/p/RweTMRjoSCt is too much of a foot gun

Change-Id: I23f0b3d77dd4288fcc20b3756a7110359576bf44
2018-12-11 11:24:31 -07:00

174 lines
4.6 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"fmt"
"log"
"time"
"storj.io/storj/pkg/utils"
)
// PacketCopier sends the same packet to multiple destinations
type PacketCopier struct {
d []PacketDest
}
// NewPacketCopier creates a packet copier that sends the same packets to
// the provided different destinations
func NewPacketCopier(d ...PacketDest) *PacketCopier {
return &PacketCopier{d: d}
}
// Packet implements the PacketDest interface
func (p *PacketCopier) Packet(data []byte, ts time.Time) (ferr error) {
var errs utils.ErrorGroup
for _, d := range p.d {
errs.Add(d.Packet(data, ts))
}
return errs.Finish()
}
// MetricCopier sends the same metric to multiple destinations
type MetricCopier struct {
d []MetricDest
}
// NewMetricCopier creates a metric copier that sends the same metrics to
// the provided different destinations
func NewMetricCopier(d ...MetricDest) *MetricCopier {
return &MetricCopier{d: d}
}
// Metric implements the MetricDest interface
func (m *MetricCopier) Metric(application, instance string,
key []byte, val float64, ts time.Time) (ferr error) {
var errs utils.ErrorGroup
for _, d := range m.d {
errs.Add(d.Metric(application, instance, key, val, ts))
}
return errs.Finish()
}
// Packet represents a single packet
type Packet struct {
Data []byte
TS time.Time
}
// PacketBuffer is a packet buffer. It has a given buffer size and allows
// packets to buffer in memory to deal with potentially variable processing
// speeds. PacketBuffers drop packets if the buffer is full.
type PacketBuffer struct {
ch chan Packet
}
// NewPacketBuffer makes a packet buffer with a buffer size of bufsize
func NewPacketBuffer(p PacketDest, bufsize int) *PacketBuffer {
ch := make(chan Packet, bufsize)
go func() {
for pkt := range ch {
err := p.Packet(pkt.Data, pkt.TS)
if err != nil {
log.Printf("failed delivering buffered packet: %v", err)
}
}
}()
return &PacketBuffer{ch: ch}
}
// Packet implements the PacketDest interface
func (p *PacketBuffer) Packet(data []byte, ts time.Time) error {
select {
case p.ch <- Packet{Data: data, TS: ts}:
return nil
default:
return fmt.Errorf("packet buffer overrun")
}
}
// Metric represents a single metric
type Metric struct {
Application string
Instance string
Key []byte
Val float64
TS time.Time
}
// MetricBuffer is a metric buffer. It has a given buffer size and allows
// metrics to buffer in memory to deal with potentially variable processing
// speeds. MetricBuffers drop metrics if the buffer is full.
type MetricBuffer struct {
ch chan Metric
}
// NewMetricBuffer makes a metric buffer with a buffer size of bufsize
func NewMetricBuffer(p MetricDest, bufsize int) *MetricBuffer {
ch := make(chan Metric, bufsize)
go func() {
for pkt := range ch {
err := p.Metric(pkt.Application, pkt.Instance, pkt.Key, pkt.Val, pkt.TS)
if err != nil {
log.Printf("failed delivering buffered metric: %v", err)
}
}
}()
return &MetricBuffer{ch: ch}
}
// Metric implements the MetricDest interface
func (p *MetricBuffer) Metric(application, instance string, key []byte,
val float64, ts time.Time) error {
select {
case p.ch <- Metric{
Application: application,
Instance: instance,
Key: key,
Val: val,
TS: ts}:
return nil
default:
return fmt.Errorf("metric buffer overrun")
}
}
// PacketBufPrep prepares a packet destination for a packet buffer.
// By default, packet memory is reused, which would cause data race conditions
// when a buffer is also used. PacketBufPrep copies the memory to make sure
// there are no data races
type PacketBufPrep struct {
d PacketDest
}
// NewPacketBufPrep creates a PacketBufPrep
func NewPacketBufPrep(d PacketDest) *PacketBufPrep {
return &PacketBufPrep{d: d}
}
// Packet implements the PacketDest interface
func (p *PacketBufPrep) Packet(data []byte, ts time.Time) error {
return p.d.Packet(append([]byte(nil), data...), ts)
}
// MetricBufPrep prepares a metric destination for a metric buffer.
// By default, metric key memory is reused, which would cause data race
// conditions when a buffer is also used. MetricBufPrep copies the memory to
// make sure there are no data races
type MetricBufPrep struct {
d MetricDest
}
// NewMetricBufPrep creates a MetricBufPrep
func NewMetricBufPrep(d MetricDest) *MetricBufPrep {
return &MetricBufPrep{d: d}
}
// Metric implements the MetricDest interface
func (p *MetricBufPrep) Metric(application, instance string, key []byte,
val float64, ts time.Time) error {
return p.d.Metric(application, instance, append([]byte(nil), key...), val, ts)
}