/
processor.go
122 lines (102 loc) · 3.13 KB
/
processor.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
package antifraud
import (
"context"
"github.com/sonm-io/core/connor/types"
"go.uber.org/zap"
"google.golang.org/grpc"
)
const (
LogFormatCommon = "common"
PoolFormatDwarf = "dwarf"
PoolFormatUley = "uley"
ProcessorFormatDisabled = "disabled"
)
// Processor is part of AntiFraud module that continuously
// processing results of container execution (like logs, pool reports, etc)
// and calculate task quality value.
type Processor interface {
Run(ctx context.Context) error
TaskID() string
TaskQuality() (accurate bool, quality float64)
}
type disabledProcessor struct {
taskID string
}
func (p *disabledProcessor) TaskID() string { return p.taskID }
func (p *disabledProcessor) TaskQuality() (accurate bool, quality float64) { return true, 1.0 }
func (p *disabledProcessor) Run(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}
// ProcessorFactory builds particular processors for the anti-fraud
type ProcessorFactory interface {
LogProcessor(deal *types.Deal, taskID string, opts ...Option) Processor
PoolProcessor(deal *types.Deal, taskID string, opts ...Option) Processor
}
func NewProcessorFactory(cfg *Config) ProcessorFactory {
var pool, log builderFunc
switch cfg.PoolProcessorConfig.Format {
case PoolFormatDwarf:
pool = func(deal *types.Deal, taskID string, opts ...Option) Processor {
o := makeOpts(opts...)
return newDwarfPoolProcessor(&cfg.PoolProcessorConfig, o.logger, deal, taskID)
}
case PoolFormatUley:
pool = func(deal *types.Deal, taskID string, opts ...Option) Processor {
o := makeOpts(opts...)
return newUleyPoolProcessor(&cfg.PoolProcessorConfig, o.logger, deal, taskID)
}
case ProcessorFormatDisabled:
pool = func(deal *types.Deal, taskID string, opts ...Option) Processor {
return &disabledProcessor{}
}
}
switch cfg.LogProcessorConfig.Format {
case LogFormatCommon:
log = func(deal *types.Deal, taskID string, opts ...Option) Processor {
o := makeOpts(opts...)
return newLogProcessor(&cfg.LogProcessorConfig, o.logger, o.cc, deal, taskID)
}
case ProcessorFormatDisabled:
log = func(deal *types.Deal, taskID string, opts ...Option) Processor {
return &disabledProcessor{taskID: taskID}
}
}
return &processorFactory{
pool: pool,
log: log,
}
}
func makeOpts(opts ...Option) *processorOpts {
o := &processorOpts{}
for _, opt := range opts {
opt(o)
}
return o
}
type processorOpts struct {
logger *zap.Logger
cc *grpc.ClientConn
}
type Option func(options *processorOpts)
func WithLogger(log *zap.Logger) Option {
return func(o *processorOpts) {
o.logger = log
}
}
func WithClientConn(cc *grpc.ClientConn) Option {
return func(o *processorOpts) {
o.cc = cc
}
}
type builderFunc func(deal *types.Deal, taskID string, opts ...Option) Processor
type processorFactory struct {
pool builderFunc
log builderFunc
}
func (p *processorFactory) LogProcessor(deal *types.Deal, taskID string, opts ...Option) Processor {
return p.log(deal, taskID, opts...)
}
func (p *processorFactory) PoolProcessor(deal *types.Deal, taskID string, opts ...Option) Processor {
return p.pool(deal, taskID, opts...)
}