-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallerprocessor.go
151 lines (125 loc) · 4.04 KB
/
parallerprocessor.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
package gomsgprocessor
import (
"context"
"github.com/arquivei/foundationkit/errors"
"github.com/arquivei/foundationkit/trace"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
)
// ParallelProcessor is a interface that process in parallel a slice of Message.
type ParallelProcessor interface {
// MakeDocuments creates in parallel a slice of Document for given []Message
// using the map of DocumentBuilder (see NewParallelProcessor).
//
// This method returns a []Document and a (foundationkit/errors).Error.
// If not nil, this error has a (foundationkit/errors).Code associated with and
// can be a ErrCodeBuildDocuments or a ErrCodeDeduplicateDocuments.
MakeDocuments(context.Context, []Message) ([]Document, error)
}
// DocumentBuilder is a interface that transforms a Message into []Document.
type DocumentBuilder interface {
// Build transforms a Message into []Document.
Build(context.Context, Message) ([]Document, error)
}
type parallelProcessor struct {
builders map[MessageType]DocumentBuilder
deduplicateDocuments DeduplicateDocumentsFunc
}
// NewParallelProcessor returns a new ParallelProcessor with a map of
// DocumentBuilder for each MessageType.
//
// A list of Option is also available for this method. See option.go for more
// information.
func NewParallelProcessor(
builders map[MessageType]DocumentBuilder,
opts ...Option,
) ParallelProcessor {
p := ¶llelProcessor{
builders: builders,
deduplicateDocuments: defaultDeduplicateDocumentsFunc,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *parallelProcessor) MakeDocuments(ctx context.Context, msgs []Message) ([]Document, error) {
const op = errors.Op("gomsgprocessor.parallelProcessor.MakeDocuments")
ctx, span := trace.StartSpan(ctx, op.String())
defer span.End(nil)
documentsByNamespace, err := p.parallelBuildDocumentsByNamespace(ctx, msgs)
if err != nil {
return nil, errors.E(op, err, ErrCodeBuildDocuments)
}
deduplicatedDocuments, err := p.deduplicateDocumentsForEachNamespace(documentsByNamespace)
if err != nil {
return nil, errors.E(op, err, ErrCodeDeduplicateDocuments)
}
return deduplicatedDocuments, nil
}
func (p *parallelProcessor) parallelBuildDocumentsByNamespace(
ctx context.Context,
msgs []Message,
) (map[Namespace][]Document, error) {
const op = errors.Op("parallelBuildDocumentsByNamespace")
type builtDocument struct {
documents []Document
namespace Namespace
}
builtDocuments := make([]builtDocument, len(msgs))
g, ctx := errgroup.WithContext(ctx)
for i, msg := range msgs {
i, msg := i, msg
g.Go(func() error {
documentBuilder, ok := p.builders[msg.GetType()]
if !ok {
msg.UpdateLogWithData(ctx)
return errors.E(ErrMsgTypeHasNoBuilder, errors.KV("type", msg.GetType()))
}
documents, err := documentBuilder.Build(ctx, msg)
if err != nil {
msg.UpdateLogWithData(ctx)
return err
}
if documents == nil {
msg.UpdateLogWithData(ctx)
log.Ctx(ctx).Info().Msg("Message ignored...")
return nil
}
builtDocuments[i] = builtDocument{
documents: documents,
namespace: msg.GetNamespace(),
}
return nil
})
}
err := g.Wait()
if err != nil {
return nil, errors.E(op, err)
}
documentsByNamespace := make(map[Namespace][]Document, len(builtDocuments))
for _, builtDoc := range builtDocuments {
if builtDoc.documents == nil || builtDoc.namespace == "" {
continue
}
documentsByNamespace[builtDoc.namespace] = append(
documentsByNamespace[builtDoc.namespace],
builtDoc.documents...,
)
}
return documentsByNamespace, nil
}
func (p *parallelProcessor) deduplicateDocumentsForEachNamespace(
documentsByNamespace map[Namespace][]Document,
) ([]Document, error) {
const op = errors.Op("deduplicateDocumentsForEachNamespace")
documents := make([]Document, 0, len(documentsByNamespace))
for _, docs := range documentsByNamespace {
deduplicatedDocuments, err := p.deduplicateDocuments(docs)
if err != nil {
return nil, errors.E(op, err)
}
documents = append(documents, deduplicatedDocuments...)
}
return documents, nil
}