-
Notifications
You must be signed in to change notification settings - Fork 0
/
insight_processor_builder.go
120 lines (107 loc) · 3.35 KB
/
insight_processor_builder.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
package factories
import (
"fmt"
. "github.com/featbit/featbit-go-sdk/interfaces"
"github.com/featbit/featbit-go-sdk/internal/insight"
"net/http"
"time"
)
const (
DefaultCapacity = 10000
DefaultFlushInterval = time.Second
DefaultRetryDelay = 100 * time.Millisecond
DefaultRetryTimes = 1
)
// InsightProcessorBuilder factory to create default implementation of interfaces.InsightProcessor
type InsightProcessorBuilder struct {
capacity int
flushInterval time.Duration
retryInterval time.Duration
maxRetryTimes int
}
// NewInsightProcessorBuilder creates an instance of InsightProcessorBuilder
func NewInsightProcessorBuilder() *InsightProcessorBuilder {
return &InsightProcessorBuilder{
capacity: DefaultCapacity,
flushInterval: DefaultFlushInterval,
retryInterval: DefaultRetryDelay,
maxRetryTimes: DefaultRetryTimes,
}
}
// Capacity sets the inbox capacity
func (i *InsightProcessorBuilder) Capacity(capacity int) *InsightProcessorBuilder {
if capacity <= 0 {
i.capacity = DefaultCapacity
} else {
i.capacity = capacity
}
return i
}
// FlushInterval sets the interval of flush message
func (i *InsightProcessorBuilder) FlushInterval(flushInterval time.Duration) *InsightProcessorBuilder {
if flushInterval <= 0 {
i.flushInterval = DefaultFlushInterval
} else {
i.flushInterval = flushInterval
}
return i
}
// RetryInterval sets the time to wait for next retry if the last sending events failed
func (i *InsightProcessorBuilder) RetryInterval(retryInterval time.Duration) *InsightProcessorBuilder {
if retryInterval <= 0 {
i.retryInterval = DefaultRetryDelay
} else {
i.retryInterval = retryInterval
}
return i
}
// MaxRetryTimes sets max retry times for a failed event sending
func (i *InsightProcessorBuilder) MaxRetryTimes(maxRetryTimes int) *InsightProcessorBuilder {
if maxRetryTimes <= 0 {
i.maxRetryTimes = DefaultRetryTimes
} else {
i.maxRetryTimes = maxRetryTimes
}
return i
}
// CreateInsightProcessor creates an instance of interfaces.InsightProcessor
func (i *InsightProcessorBuilder) CreateInsightProcessor(context Context) (InsightProcessor, error) {
sender, err := i.CreateInsightEventSender(context)
if err != nil {
return nil, err
}
capacity := i.capacity
if capacity > DefaultCapacity {
capacity = DefaultCapacity
}
flushInterval := i.flushInterval
if flushInterval > 3*time.Second {
flushInterval = 3 * time.Second
}
return insight.NewEventProcessor(context, sender, capacity, flushInterval), nil
}
// CreateInsightEventSender creates an instance of interfaces.Sender
func (i *InsightProcessorBuilder) CreateInsightEventSender(context Context) (Sender, error) {
network := context.GetNetwork()
client, ok := network.GetHTTPClient().(*http.Client)
if !ok {
return nil, fmt.Errorf("non supported HTTP Client")
}
headers := network.GetHeaders(nil)
retryInterval := i.retryInterval
if retryInterval > time.Second {
retryInterval = time.Second
}
maxRetryTimes := i.maxRetryTimes
if maxRetryTimes > 3 {
maxRetryTimes = 3
}
return insight.NewEventSenderImp(client, headers, retryInterval, maxRetryTimes), nil
}
type nullInsightProcessorBuilder struct{}
func ExternalEventTrack() InsightProcessorFactory {
return &nullInsightProcessorBuilder{}
}
func (n *nullInsightProcessorBuilder) CreateInsightProcessor(Context) (InsightProcessor, error) {
return insight.NewNullEventProcessor(), nil
}