diff options
author | jloup <jeanloup.jamet@gmail.com> | 2018-02-22 11:31:59 +0100 |
---|---|---|
committer | jloup <jeanloup.jamet@gmail.com> | 2018-02-22 11:31:59 +0100 |
commit | 2f91f20a8645339385ada602684f4957f20f4da4 (patch) | |
tree | a084cf293af15cc6ea6a1417edad51fcf0674947 /api/external_services.go | |
parent | 3602fbf8412d69900d793a963c8e774f487f5e45 (diff) | |
download | Front-2f91f20a8645339385ada602684f4957f20f4da4.tar.gz Front-2f91f20a8645339385ada602684f4957f20f4da4.tar.zst Front-2f91f20a8645339385ada602684f4957f20f4da4.zip |
Poloniex connection.
Diffstat (limited to 'api/external_services.go')
-rw-r--r-- | api/external_services.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/api/external_services.go b/api/external_services.go new file mode 100644 index 0000000..c467171 --- /dev/null +++ b/api/external_services.go | |||
@@ -0,0 +1,40 @@ | |||
1 | package api | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "fmt" | ||
6 | "time" | ||
7 | ) | ||
8 | |||
9 | // Use this to call external services. It will handle timeout and request cancellation gracefully. | ||
10 | func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error { | ||
11 | routineDone := make(chan *Error) | ||
12 | |||
13 | go func() { | ||
14 | routineDone <- routine() | ||
15 | }() | ||
16 | |||
17 | ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
18 | defer cancel() | ||
19 | |||
20 | select { | ||
21 | case err := <-routineDone: | ||
22 | return err | ||
23 | case <-ctx.Done(): | ||
24 | return &Error{ExternalServiceTimeout, "external service timeout", fmt.Errorf("'%v' routine timeouted", tag)} | ||
25 | } | ||
26 | } | ||
27 | |||
28 | var ErrorChan chan error | ||
29 | |||
30 | func ErrorMonitoring() { | ||
31 | for { | ||
32 | err := <-ErrorChan | ||
33 | log.Errorf("error: %v", err) | ||
34 | } | ||
35 | } | ||
36 | |||
37 | func init() { | ||
38 | ErrorChan = make(chan error) | ||
39 | go ErrorMonitoring() | ||
40 | } | ||