|
1
|
|
|
package decorators |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"errors" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/afex/hystrix-go/hystrix" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/Permify/permify/internal/storage" |
|
10
|
|
|
base "github.com/Permify/permify/pkg/pb/base/v1" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
// BundleWriterWithCircuitBreaker - Add circuit breaker behaviour to bundle writer |
|
14
|
|
|
type BundleWriterWithCircuitBreaker struct { |
|
15
|
|
|
delegate storage.BundleWriter |
|
16
|
|
|
timeout int |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// NewBundleWriterWithCircuitBreaker - Add circuit breaker behaviour to new bundle writer |
|
20
|
|
|
func NewBundleWriterWithCircuitBreaker(delegate storage.BundleWriter, timeout int) *BundleWriterWithCircuitBreaker { |
|
21
|
|
|
return &BundleWriterWithCircuitBreaker{delegate: delegate, timeout: timeout} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// Write - Write bundles from the repository |
|
25
|
|
|
func (r *BundleWriterWithCircuitBreaker) Write(ctx context.Context, tenantID string, bundles []*base.DataBundle) (names []string, err error) { |
|
26
|
|
|
type circuitBreakerResponse struct { |
|
27
|
|
|
Names []string |
|
28
|
|
|
Error error |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
output := make(chan circuitBreakerResponse, 1) |
|
32
|
|
|
hystrix.ConfigureCommand("bundleWriter.write", hystrix.CommandConfig{Timeout: r.timeout}) |
|
33
|
|
|
bErrors := hystrix.Go("bundleWriter.write", func() error { |
|
34
|
|
|
names, err := r.delegate.Write(ctx, tenantID, bundles) |
|
35
|
|
|
output <- circuitBreakerResponse{Names: names, Error: err} |
|
36
|
|
|
return nil |
|
37
|
|
|
}, func(err error) error { |
|
38
|
|
|
return nil |
|
39
|
|
|
}) |
|
40
|
|
|
|
|
41
|
|
|
select { |
|
42
|
|
|
case out := <-output: |
|
43
|
|
|
return out.Names, out.Error |
|
44
|
|
|
case <-bErrors: |
|
45
|
|
|
return nil, errors.New(base.ErrorCode_ERROR_CODE_CIRCUIT_BREAKER.String()) |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Delete - Delete bundles from the repository |
|
50
|
|
|
func (r *BundleWriterWithCircuitBreaker) Delete(ctx context.Context, tenantID, name string) (err error) { |
|
51
|
|
|
type circuitBreakerResponse struct { |
|
52
|
|
|
Error error |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
output := make(chan circuitBreakerResponse, 1) |
|
56
|
|
|
hystrix.ConfigureCommand("bundleWriter.write", hystrix.CommandConfig{Timeout: r.timeout}) |
|
57
|
|
|
bErrors := hystrix.Go("bundleWriter.write", func() error { |
|
58
|
|
|
err := r.delegate.Delete(ctx, tenantID, name) |
|
59
|
|
|
output <- circuitBreakerResponse{Error: err} |
|
60
|
|
|
return nil |
|
61
|
|
|
}, func(err error) error { |
|
62
|
|
|
return nil |
|
63
|
|
|
}) |
|
64
|
|
|
|
|
65
|
|
|
select { |
|
66
|
|
|
case out := <-output: |
|
67
|
|
|
return out.Error |
|
68
|
|
|
case <-bErrors: |
|
69
|
|
|
return errors.New(base.ErrorCode_ERROR_CODE_CIRCUIT_BREAKER.String()) |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|