-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnection_decoder.go
56 lines (46 loc) · 1.39 KB
/
connection_decoder.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
package pluto
import (
"encoding/json"
"net"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
type ConnectionDecoder struct {
MaxDecode uint64
ReadDeadline time.Duration
ProcessableBuilder func(context Processable, new OutComingProcessable) Processable
Processor Processor
}
func (p ConnectionDecoder) Process(processable Processable) (Processable, bool) {
conn := processable.GetBody().(map[string]any)["connection"].(net.Conn)
decoder := json.NewDecoder(conn)
decoder.UseNumber()
for i := uint64(0); i < p.MaxDecode; i++ {
if err := conn.SetReadDeadline(time.Now().Add(p.ReadDeadline)); err != nil {
Log.Error("Set read deadline", zap.Error(err))
return processable, false
}
var outComingProcessable OutComingProcessable
if err := decoder.Decode(&outComingProcessable); err != nil {
Log.Debug("Decoding out-coming processable", zap.Error(err))
return processable, false
}
if result, success := p.Processor.Process(p.ProcessableBuilder(processable, outComingProcessable)); !success {
return result, false
}
}
return &InternalProcessable{
ID: uuid.New(),
Body: processable.GetBody(),
CreatedAt: time.Now(),
}, true
}
func (p ConnectionDecoder) GetDescriptor() ProcessorDescriptor {
return ProcessorDescriptor{
Name: "CONNECTION_DECODER_PROCESSOR",
Description: "",
Input: "",
Output: "",
}
}