Skip to content

Commit 11a8a44

Browse files
authored
Fix tcoudp trigger take much of CPU (#143)
* FIx tcoudp trigger take much of CPU * listen only once if no delimiter * revert to keep previous code * Fixed that we need send data first as long as data is not empty
1 parent 1dc6403 commit 11a8a44

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

trigger/tcpudp/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ flogo install github.com/project-flogo/contrib/trigger/tcpudp
2020
| network | string | Network type. Supported types: tcp,tcp4,tcp6,udp,udp4,udp6 - ***REQUIRED***
2121
| host | string | Host IP or DNS resolvable name
2222
| port | string | Port to listen on - ***REQUIRED***
23-
| delimiter | string | Delimiter for read and write. If not set, trigger will read data until EOF
23+
| delimiter | string | Delimiter for read and write. If not set, trigger will take line delimiter '\n' as default value
2424
| timeout | integer | Read and Write timeout in milliseconds. To disable timeout, set value to 0.
2525

2626

trigger/tcpudp/trigger.go

+37-36
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tcpudp
22

33
import (
44
"bufio"
5-
"bytes"
65
"context"
76
"errors"
87
"io"
@@ -111,9 +110,7 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
111110

112111
//Gather connection list for later cleanup
113112
t.connections = append(t.connections, conn)
114-
115113
for {
116-
117114
if t.settings.TimeOut > 0 {
118115
t.logger.Info("Setting timeout: ", t.settings.TimeOut)
119116
conn.SetDeadline(time.Now().Add(time.Duration(t.settings.TimeOut) * time.Millisecond))
@@ -123,9 +120,13 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
123120

124121
if t.delimiter != 0 {
125122
data, err := bufio.NewReader(conn).ReadBytes(t.delimiter)
123+
if len(data) > 0 {
124+
output.Data = string(data)
125+
t.triggerFlow(conn, output)
126+
}
126127
if err != nil {
127128
errString := err.Error()
128-
if !strings.Contains(errString, "use of closed network connection") {
129+
if !strings.Contains(errString, "use of closed network connection") && err != io.EOF {
129130
t.logger.Error("Error reading data from connection: ", err.Error())
130131
} else {
131132
t.logger.Info("Connection is closed.")
@@ -134,16 +135,16 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
134135
// Return if not timeout error
135136
return
136137
}
137-
138-
} else {
139-
output.Data = string(data[:len(data)-1])
140138
}
141139
} else {
142-
var buf bytes.Buffer
143-
_, err := io.Copy(&buf, conn)
140+
data, err := bufio.NewReader(conn).ReadBytes('\n')
141+
if len(data) > 0 {
142+
output.Data = string(data)
143+
t.triggerFlow(conn, output)
144+
}
144145
if err != nil {
145146
errString := err.Error()
146-
if !strings.Contains(errString, "use of closed network connection") {
147+
if !strings.Contains(errString, "use of closed network connection") && err != io.EOF {
147148
t.logger.Error("Error reading data from connection: ", err.Error())
148149
} else {
149150
t.logger.Info("Connection is closed.")
@@ -152,38 +153,38 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
152153
// Return if not timeout error
153154
return
154155
}
155-
} else {
156-
output.Data = string(buf.Bytes())
157156
}
158157
}
158+
}
159+
}
159160

160-
if output.Data != "" {
161-
var replyData []string
162-
for i := 0; i < len(t.handlers); i++ {
163-
results, err := t.handlers[i].Handle(context.Background(), output)
164-
if err != nil {
165-
t.logger.Error("Error invoking action : ", err.Error())
166-
continue
167-
}
161+
func (t *Trigger) triggerFlow(conn net.Conn, output *Output) {
162+
if output.Data != "" {
163+
var replyData []string
164+
for i := 0; i < len(t.handlers); i++ {
165+
results, err := t.handlers[i].Handle(context.Background(), output)
166+
if err != nil {
167+
t.logger.Error("Error invoking action : ", err.Error())
168+
continue
169+
}
168170

169-
reply := &Reply{}
170-
err = reply.FromMap(results)
171-
if err != nil {
172-
t.logger.Error("Failed to convert flow output : ", err.Error())
173-
continue
174-
}
175-
if reply.Reply != "" {
176-
replyData = append(replyData, reply.Reply)
177-
}
171+
reply := &Reply{}
172+
err = reply.FromMap(results)
173+
if err != nil {
174+
t.logger.Error("Failed to convert flow output : ", err.Error())
175+
continue
178176
}
177+
if reply.Reply != "" {
178+
replyData = append(replyData, reply.Reply)
179+
}
180+
}
179181

180-
if len(replyData) > 0 {
181-
replyToSend := strings.Join(replyData, string(t.delimiter))
182-
// Send a response back to client contacting us.
183-
_, err := conn.Write([]byte(replyToSend + "\n"))
184-
if err != nil {
185-
t.logger.Error("Failed to write to connection : ", err.Error())
186-
}
182+
if len(replyData) > 0 {
183+
replyToSend := strings.Join(replyData, string(t.delimiter))
184+
// Send a response back to client contacting us.
185+
_, err := conn.Write([]byte(replyToSend + "\n"))
186+
if err != nil {
187+
t.logger.Error("Failed to write to connection : ", err.Error())
187188
}
188189
}
189190
}

0 commit comments

Comments
 (0)