-
Notifications
You must be signed in to change notification settings - Fork 402
/
common.go
56 lines (46 loc) · 1.17 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"io"
"log"
"sync/atomic"
"time"
)
type closerfunc func() error
func (f closerfunc) Close() error { return f() }
// Deliver kicks off a goroutine that reads packets from source and delivers them
// to dest. To stop delivery, call Close on the return value then close the source.
func Deliver(source Source, dest PacketDest) io.Closer {
done := new(uint32)
go func() {
for atomic.LoadUint32(done) == 0 {
data, ts, err := source.Next()
if err != nil {
log.Printf("failed getting packet: %v", err)
continue
}
err = dest.Packet(data, ts)
if err != nil {
log.Printf("failed delivering packet: %v", err)
continue
}
}
}()
return closerfunc(func() error {
atomic.StoreUint32(done, 1)
return nil
})
}
// Source reads incoming packets
type Source interface {
Next() (data []byte, ts time.Time, err error)
}
// PacketDest handles packets
type PacketDest interface {
Packet(data []byte, ts time.Time) error
}
// MetricDest handles metrics
type MetricDest interface {
Metric(application, instance string, key []byte, val float64, ts time.Time) error
}