-
Notifications
You must be signed in to change notification settings - Fork 202
/
outportFactory.go
118 lines (95 loc) · 2.89 KB
/
outportFactory.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
package factory
import (
"time"
wsDriverFactory "github.com/multiversx/mx-chain-core-go/websocketOutportDriver/factory"
indexerFactory "github.com/multiversx/mx-chain-es-indexer-go/process/factory"
"github.com/multiversx/mx-chain-go/outport"
)
// WrappedOutportDriverWebSocketSenderFactoryArgs extends the wsDriverFactory.OutportDriverWebSocketSenderFactoryArgs structure with the Enabled field
type WrappedOutportDriverWebSocketSenderFactoryArgs struct {
Enabled bool
wsDriverFactory.OutportDriverWebSocketSenderFactoryArgs
}
// OutportFactoryArgs holds the factory arguments of different outport drivers
type OutportFactoryArgs struct {
RetrialInterval time.Duration
ElasticIndexerFactoryArgs indexerFactory.ArgsIndexerFactory
EventNotifierFactoryArgs *EventNotifierFactoryArgs
WebSocketSenderDriverFactoryArgs WrappedOutportDriverWebSocketSenderFactoryArgs
}
// CreateOutport will create a new instance of OutportHandler
func CreateOutport(args *OutportFactoryArgs) (outport.OutportHandler, error) {
err := checkArguments(args)
if err != nil {
return nil, err
}
outportHandler, err := outport.NewOutport(args.RetrialInterval)
if err != nil {
return nil, err
}
err = createAndSubscribeDrivers(outportHandler, args)
if err != nil {
return nil, err
}
return outportHandler, nil
}
func createAndSubscribeDrivers(outport outport.OutportHandler, args *OutportFactoryArgs) error {
err := createAndSubscribeElasticDriverIfNeeded(outport, args.ElasticIndexerFactoryArgs)
if err != nil {
return err
}
err = createAndSubscribeEventNotifierIfNeeded(outport, args.EventNotifierFactoryArgs)
if err != nil {
return err
}
return createAndSubscribeWebSocketDriver(outport, args.WebSocketSenderDriverFactoryArgs)
}
func createAndSubscribeElasticDriverIfNeeded(
outport outport.OutportHandler,
args indexerFactory.ArgsIndexerFactory,
) error {
if !args.Enabled {
return nil
}
elasticDriver, err := indexerFactory.NewIndexer(args)
if err != nil {
return err
}
return outport.SubscribeDriver(elasticDriver)
}
func createAndSubscribeEventNotifierIfNeeded(
outport outport.OutportHandler,
args *EventNotifierFactoryArgs,
) error {
if !args.Enabled {
return nil
}
eventNotifier, err := CreateEventNotifier(args)
if err != nil {
return err
}
return outport.SubscribeDriver(eventNotifier)
}
func checkArguments(args *OutportFactoryArgs) error {
if args == nil {
return outport.ErrNilArgsOutportFactory
}
return nil
}
func createAndSubscribeWebSocketDriver(
outport outport.OutportHandler,
args WrappedOutportDriverWebSocketSenderFactoryArgs,
) error {
if !args.Enabled {
return nil
}
wsFactory, err := wsDriverFactory.NewOutportDriverWebSocketSenderFactory(args.OutportDriverWebSocketSenderFactoryArgs)
if err != nil {
return err
}
wsDriver, err := wsFactory.Create()
if err != nil {
return err
}
return outport.SubscribeDriver(wsDriver)
}