-
Notifications
You must be signed in to change notification settings - Fork 791
/
output.go
191 lines (166 loc) · 4.61 KB
/
output.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package nanomsg
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"go.nanomsg.org/mangos/v3"
"go.nanomsg.org/mangos/v3/protocol/pub"
"go.nanomsg.org/mangos/v3/protocol/push"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/component/output/processors"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
// Import all transport types
_ "go.nanomsg.org/mangos/v3/transport/all"
)
func init() {
err := bundle.AllOutputs.Add(processors.WrapConstructor(newNanomsgOutput), docs.ComponentSpec{
Name: "nanomsg",
Summary: `Send messages over a Nanomsg socket.`,
Description: output.Description(true, false, `Currently only PUSH and PUB sockets are supported.`),
Config: docs.FieldComponent().WithChildren(
docs.FieldString("urls", "A list of URLs to connect to. If an item of the list contains commas it will be expanded into multiple URLs.", []string{"tcp://localhost:5556"}).Array(),
docs.FieldBool("bind", "Whether the URLs listed should be bind (otherwise they are connected to)."),
docs.FieldString("socket_type", "The socket type to send with.").HasOptions("PUSH", "PUB"),
docs.FieldString("poll_timeout", "The maximum period of time to wait for a message to send before the request is abandoned and reattempted."),
docs.FieldInt("max_in_flight", "The maximum number of messages to have in flight at a given time. Increase this to improve throughput."),
).ChildDefaultAndTypesFromStruct(output.NewNanomsgConfig()),
Categories: []string{
"Network",
},
})
if err != nil {
panic(err)
}
}
func newNanomsgOutput(conf output.Config, mgr bundle.NewManagement) (output.Streamed, error) {
s, err := newNanomsgWriter(conf.Nanomsg, mgr.Logger())
if err != nil {
return nil, err
}
a, err := output.NewAsyncWriter("nanomsg", conf.Nanomsg.MaxInFlight, s, mgr)
if err != nil {
return nil, err
}
return output.OnlySinglePayloads(a), nil
}
type nanomsgWriter struct {
log log.Modular
urls []string
conf output.NanomsgConfig
timeout time.Duration
socket mangos.Socket
sockMut sync.RWMutex
}
func newNanomsgWriter(conf output.NanomsgConfig, log log.Modular) (*nanomsgWriter, error) {
s := nanomsgWriter{
log: log,
conf: conf,
}
for _, u := range conf.URLs {
for _, splitU := range strings.Split(u, ",") {
if len(splitU) > 0 {
s.urls = append(s.urls, strings.Replace(splitU, "//*:", "//0.0.0.0:", 1))
}
}
}
if tout := conf.PollTimeout; len(tout) > 0 {
var err error
if s.timeout, err = time.ParseDuration(tout); err != nil {
return nil, fmt.Errorf("failed to parse poll timeout string: %v", err)
}
}
socket, err := getOutputSocketFromType(conf.SocketType)
if err != nil {
return nil, err
}
socket.Close()
return &s, nil
}
func getOutputSocketFromType(t string) (mangos.Socket, error) {
switch t {
case "PUSH":
return push.NewSocket()
case "PUB":
return pub.NewSocket()
}
return nil, errors.New("invalid Scalability Protocols socket type")
}
func (s *nanomsgWriter) ConnectWithContext(ctx context.Context) error {
s.sockMut.Lock()
defer s.sockMut.Unlock()
if s.socket != nil {
return nil
}
socket, err := getSocketFromType(s.conf.SocketType)
if err != nil {
return err
}
// Set timeout to prevent endless lock.
if s.conf.SocketType == "PUSH" {
if err := socket.SetOption(
mangos.OptionSendDeadline, s.timeout,
); err != nil {
return err
}
}
if s.conf.Bind {
for _, addr := range s.urls {
if err = socket.Listen(addr); err != nil {
break
}
}
} else {
for _, addr := range s.urls {
if err = socket.Dial(addr); err != nil {
break
}
}
}
if err != nil {
return err
}
if s.conf.Bind {
s.log.Infof(
"Sending nanomsg messages to bound URLs: %s\n",
s.urls,
)
} else {
s.log.Infof(
"Sending nanomsg messages to connected URLs: %s\n",
s.urls,
)
}
s.socket = socket
return nil
}
func (s *nanomsgWriter) WriteWithContext(ctx context.Context, msg message.Batch) error {
s.sockMut.RLock()
socket := s.socket
s.sockMut.RUnlock()
if socket == nil {
return component.ErrNotConnected
}
return output.IterateBatchedSend(msg, func(i int, p *message.Part) error {
return socket.Send(p.AsBytes())
})
}
func (s *nanomsgWriter) CloseAsync() {
go func() {
s.sockMut.Lock()
if s.socket != nil {
s.socket.Close()
s.socket = nil
}
s.sockMut.Unlock()
}()
}
func (s *nanomsgWriter) WaitForClose(timeout time.Duration) error {
return nil
}