-
Notifications
You must be signed in to change notification settings - Fork 4
/
inputstreams.go
76 lines (64 loc) · 1.77 KB
/
inputstreams.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package inputstreams
import (
"context"
"strconv"
"github.com/arquivei/foundationkit/app"
"github.com/arquivei/goduck"
"github.com/arquivei/goduck/middleware/streammiddleware"
)
// New returns a new slice of goduck streams.
// By default it returns 1 stream. This can be configured by passing WithNumberOfStreams option.
// The new streams uses the default foundationkit app. This can be configured by passing WithApp option.
// The shudown priority defaults to zero. This can be configured by passing WithShutdownPriority option.
func New(opts ...Option) ([]goduck.Stream, error) {
o := options{
streams: 1,
}
for _, opt := range opts {
if err := opt(&o); err != nil {
return nil, err
}
}
if o.provider == nil {
return nil, ErrNoProvider
}
if o.streams < 1 {
return nil, ErrZeroStreams
}
goduckStreams := make([]goduck.Stream, o.streams)
var err error
for i := range goduckStreams {
name := "goduck_stream_" + strconv.Itoa(i)
if goduckStreams[i], err = makeSingleStream(name, o); err != nil {
return nil, err
}
}
return goduckStreams, nil
}
func makeSingleStream(name string, o options) (goduck.Stream, error) {
stream, err := o.provider.MakeStream()
if err != nil {
return nil, err
}
stream = streammiddleware.WrapWithLogging(stream)
closeFunc := stream.Close
shutdownHandler := &app.ShutdownHandler{
Name: name,
Handler: func(_ context.Context) error { return closeFunc() },
Priority: o.app.shutdownPriority,
}
if o.app.app != nil {
o.app.app.RegisterShutdownHandler(shutdownHandler)
} else {
app.RegisterShutdownHandler(shutdownHandler)
}
return stream, nil
}
// MustNew calls New but panics in case of error.
func MustNew(opts ...Option) []goduck.Stream {
s, err := New(opts...)
if err != nil {
panic(err)
}
return s
}