Test Setup Failed
Push — master ( 93326b...47e83c )
by Tolga
01:45
created

circuitbreaker.NewBundleReader   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package circuitbreaker
2
3
import (
4
	"context"
5
6
	"github.com/sony/gobreaker"
7
8
	"github.com/Permify/permify/internal/storage"
9
	base "github.com/Permify/permify/pkg/pb/base/v1"
10
)
11
12
// BundleReader - Add circuit breaker behaviour to bundle reader
13
type BundleReader struct {
14
	delegate storage.BundleReader
15
	cb       *gobreaker.CircuitBreaker
16
}
17
18
// NewBundleReader - Add circuit breaker behaviour to new bundle reader
19
func NewBundleReader(delegate storage.BundleReader, cb *gobreaker.CircuitBreaker) *BundleReader {
20
	return &BundleReader{delegate: delegate, cb: cb}
21
}
22
23
// Read - Reads bundles from the repository
24
func (r *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle *base.DataBundle, err error) {
25
	response, err := r.cb.Execute(func() (interface{}, error) {
26
		return r.delegate.Read(ctx, tenantID, name)
27
	})
28
	if err != nil {
29
		return nil, err
30
	}
31
	return response.(*base.DataBundle), nil
32
}
33