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

circuitbreaker.NewSchemaReader   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
	"github.com/Permify/permify/pkg/database"
10
	base "github.com/Permify/permify/pkg/pb/base/v1"
11
)
12
13
// SchemaReader - Add circuit breaker behaviour to schema reader
14
type SchemaReader struct {
15
	delegate storage.SchemaReader
16
	cb       *gobreaker.CircuitBreaker
17
}
18
19
// NewSchemaReader - Add circuit breaker behaviour to new schema reader
20
func NewSchemaReader(delegate storage.SchemaReader, cb *gobreaker.CircuitBreaker) *SchemaReader {
21
	return &SchemaReader{delegate: delegate, cb: cb}
22
}
23
24
// ReadSchema returns the schema definition for a specific tenant and version as a structured object.
25
func (r *SchemaReader) ReadSchema(ctx context.Context, tenantID, version string) (*base.SchemaDefinition, error) {
26
	response, err := r.cb.Execute(func() (interface{}, error) {
27
		return r.delegate.ReadSchema(ctx, tenantID, version)
28
	})
29
	if err != nil {
30
		return nil, err
31
	}
32
	return response.(*base.SchemaDefinition), nil
33
}
34
35
// ReadSchemaString returns the schema definition for a specific tenant and version as a string.
36
func (r *SchemaReader) ReadSchemaString(ctx context.Context, tenantID, version string) (definitions []string, err error) {
37
	response, err := r.cb.Execute(func() (interface{}, error) {
38
		return r.delegate.ReadSchemaString(ctx, tenantID, version)
39
	})
40
	if err != nil {
41
		return nil, err
42
	}
43
	return response.([]string), nil
44
}
45
46
// ReadEntityDefinition - Read entity definition from repository
47
func (r *SchemaReader) ReadEntityDefinition(ctx context.Context, tenantID, entityName, version string) (*base.EntityDefinition, string, error) {
48
	type circuitBreakerResponse struct {
49
		Definition *base.EntityDefinition
50
		Version    string
51
	}
52
53
	response, err := r.cb.Execute(func() (interface{}, error) {
54
		var err error
55
		var resp circuitBreakerResponse
56
		resp.Definition, resp.Version, err = r.delegate.ReadEntityDefinition(ctx, tenantID, entityName, version)
57
		return resp, err
58
	})
59
	if err != nil {
60
		return nil, "", err
61
	}
62
63
	resp := response.(circuitBreakerResponse)
64
	return resp.Definition, resp.Version, nil
65
}
66
67
// ReadRuleDefinition - Read rule definition from repository
68
func (r *SchemaReader) ReadRuleDefinition(ctx context.Context, tenantID, ruleName, version string) (*base.RuleDefinition, string, error) {
69
	type circuitBreakerResponse struct {
70
		Definition *base.RuleDefinition
71
		Version    string
72
	}
73
74
	response, err := r.cb.Execute(func() (interface{}, error) {
75
		var err error
76
		var resp circuitBreakerResponse
77
		resp.Definition, resp.Version, err = r.delegate.ReadRuleDefinition(ctx, tenantID, ruleName, version)
78
		return resp, err
79
	})
80
	if err != nil {
81
		return nil, "", err
82
	}
83
84
	resp := response.(circuitBreakerResponse)
85
	return resp.Definition, resp.Version, nil
86
}
87
88
// HeadVersion - Finds the latest version of the schema.
89
func (r *SchemaReader) HeadVersion(ctx context.Context, tenantID string) (version string, err error) {
90
	response, err := r.cb.Execute(func() (interface{}, error) {
91
		return r.delegate.HeadVersion(ctx, tenantID)
92
	})
93
	if err != nil {
94
		return "", err
95
	}
96
	return response.(string), nil
97
}
98
99
// ListSchemas - List all Schemas
100
func (r *SchemaReader) ListSchemas(ctx context.Context, tenantID string, pagination database.Pagination) (schemas []*base.SchemaList, ct database.EncodedContinuousToken, err error) {
101
	type circuitBreakerResponse struct {
102
		Schemas []*base.SchemaList
103
		Ct      database.EncodedContinuousToken
104
	}
105
106
	response, err := r.cb.Execute(func() (interface{}, error) {
107
		var err error
108
		var resp circuitBreakerResponse
109
		resp.Schemas, resp.Ct, err = r.delegate.ListSchemas(ctx, tenantID, pagination)
110
		return resp, err
111
	})
112
	if err != nil {
113
		return nil, nil, err
114
	}
115
116
	resp := response.(circuitBreakerResponse)
117
	return resp.Schemas, resp.Ct, nil
118
}
119