]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/external_services.go
Add column 'status' to market_configs.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / external_services.go
CommitLineData
2f91f20a 1package api
2
3import (
4 "context"
5 "fmt"
6 "time"
7)
8
9// Use this to call external services. It will handle timeout and request cancellation gracefully.
299b6b6d 10func CallExternalService(tag string, timeout time.Duration, routine func() error) error {
11 routineDone := make(chan error)
2f91f20a 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
28var ErrorChan chan error
29
30func ErrorMonitoring() {
31 for {
32 err := <-ErrorChan
33 log.Errorf("error: %v", err)
34 }
35}
36
37func init() {
38 ErrorChan = make(chan error)
39 go ErrorMonitoring()
40}