-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathget_company_wrapper.go
50 lines (45 loc) · 1021 Bytes
/
get_company_wrapper.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
package api
import (
"context"
"errors"
"fmt"
"time"
"github.com/avast/retry-go/v4"
"github.com/cuducos/go-cnpj"
)
const (
retries = 13
timeoutPerAttempt = 1 * time.Second
)
var errTimeout = errors.New("getCompany timed out")
// this wrapper avoids having the getCompany idle for too long, wrapping it in
// timeout and restarting it after that
func getCompany(db database, n string) (string, error) {
var c string
err := retry.Do(
func() error {
ctx, cancel := context.WithTimeout(context.Background(), timeoutPerAttempt)
defer cancel()
ch := make(chan error, 1)
go func() {
var err error
c, err = db.GetCompany(cnpj.Unmask(n))
ch <- err
}()
select {
case <-ctx.Done():
return errTimeout
case err := <-ch:
return err
}
},
retry.Attempts(retries),
retry.RetryIf(func(err error) bool {
return err != nil && errors.Is(err, errTimeout)
}),
)
if err != nil {
return "", fmt.Errorf("error retrieving %s: %w", n, err)
}
return c, nil
}