-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManager.go
86 lines (78 loc) · 1.93 KB
/
ConnectionManager.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
package main
import (
"log"
"time"
)
type Retrier struct {
Status string // Retrier Status
HoldTime time.Duration // Time duration after which Operation will retry. If Operation fails hold time multiplies until 360 seconds
MaxHoldTime time.Duration
Operation func() (<-chan interface{}, error)
}
func GetRetrierOverloadInstance(Func func() (<-chan interface{}, error)) Retrier {
r := Retrier{}
r.HoldTime = 10 * time.Second
r.MaxHoldTime = 360 * time.Second
r.Status = "Closed"
r.Operation = Func
return r
}
func (r *Retrier) Open() {
r.Status = "Open"
}
func (r *Retrier) Close() {
r.HoldTime = 10 * time.Second
r.Status = "Closed"
}
func (r *Retrier) Multiply() {
if r.HoldTime <= r.MaxHoldTime {
r.HoldTime = r.HoldTime * 2
}
}
func (r *Retrier) isClosed() bool {
return r.Status == "Closed"
}
func (r *Retrier) Do() (<-chan interface{}, error) {
result, err := r.Operation()
commonReceiver := make(chan interface{})
if err != nil {
r.Open()
log.Println(err)
}
if r.isClosed() {
// if status is success starting routine to receive messages
go func() {
for message := range result {
commonReceiver <- message
}
// Ending routine when error happened and setting status to fail
r.Open()
}()
}
// Starting forever loop in routine to check if status is fail
go func() {
for {
time.Sleep(1 * time.Second)
if !r.isClosed() {
// if status is fail waiting for defined hold time and trying again
time.Sleep(r.HoldTime)
retryResult, err := r.Operation()
if err != nil {
log.Println(err)
r.Multiply()
} else {
// if retry is success setting status to success and starting routine to receive messages
r.Close()
go func() {
for message := range retryResult {
commonReceiver <- message
}
// Ending routine when error happened and setting status to fail
r.Open()
}()
}
}
}
}()
return commonReceiver, nil
}