/
request_handler.go
116 lines (102 loc) · 4.14 KB
/
request_handler.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package user
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/config"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/user/aggregate"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/logger"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"time"
)
type UserRequestHandler interface {
Handle(ctx context.Context, tenant, objectId string, request any, params ...map[string]any) (any, error)
HandleWithRetry(ctx context.Context, tenant, objectId string, aggregateRequired bool, request any, params ...map[string]any) (any, error)
}
type userRequestHandler struct {
log logger.Logger
es eventstore.AggregateStore
cfg config.Utils
}
func NewUserRequestHandler(log logger.Logger, es eventstore.AggregateStore, cfg config.Utils) UserRequestHandler {
return &userRequestHandler{log: log, es: es, cfg: cfg}
}
func (h *userRequestHandler) Handle(ctx context.Context, tenant, objectId string, request any, params ...map[string]any) (any, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UserRequestHandler.Handle")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, tenant)
tracing.LogObjectAsJson(span, "request", request)
if params != nil && len(params) > 0 {
span.LogFields(log.Object("params", params))
}
userAggregate, err := aggregate.LoadUserAggregate(ctx, h.es, tenant, objectId)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
var requestParams map[string]any
if params != nil && len(params) > 0 {
requestParams = params[0]
}
result, err := userAggregate.HandleRequest(ctx, request, requestParams)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
return result, h.es.Save(ctx, userAggregate)
}
func (h *userRequestHandler) HandleWithRetry(ctx context.Context, tenant, objectId string, aggregateRequired bool, request any, params ...map[string]any) (any, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UserRequestHandler.HandleWithRetry")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, tenant)
tracing.LogObjectAsJson(span, "request", request)
span.LogFields(log.Bool("aggregateRequired", aggregateRequired))
if params != nil && len(params) > 0 {
span.LogFields(log.Object("params", params))
}
for attempt := 0; attempt == 0 || attempt < h.cfg.RetriesOnOptimisticLockException; attempt++ {
userAggregate, err := aggregate.LoadUserAggregate(ctx, h.es, tenant, objectId)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
if aggregateRequired && eventstore.IsAggregateNotFound(userAggregate) {
tracing.TraceErr(span, eventstore.ErrAggregateNotFound)
return nil, eventstore.ErrAggregateNotFound
}
var requestParams map[string]any
if params != nil && len(params) > 0 {
requestParams = params[0]
}
result, err := userAggregate.HandleRequest(ctx, request, requestParams)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
err = h.es.Save(ctx, userAggregate)
if err == nil {
return result, nil // Save successful
}
if eventstore.IsEventStoreErrorCodeWrongExpectedVersion(err) {
// Handle concurrency error
if attempt == h.cfg.RetriesOnOptimisticLockException-1 {
// If we have reached the maximum number of retries, return an error
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Int("retryAttempt", attempt+1))
time.Sleep(utils.BackOffExponentialDelay(attempt)) // backoffDelay is a function that increases the delay with each attempt
continue // Retry
} else {
// Some other error occurred
tracing.TraceErr(span, err)
return nil, err
}
}
err := errors.New("reached maximum number of retries")
tracing.TraceErr(span, err)
return nil, err
}