forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
126 lines (108 loc) · 3.46 KB
/
repo.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
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2014 - Max Ekman <max@looplab.se>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package version
import (
"context"
"time"
"github.com/jpillora/backoff"
eh "github.com/looplab/eventhorizon"
)
// Repo is a middleware that adds version checking to a read repository.
type Repo struct {
eh.ReadWriteRepo
}
// NewRepo creates a new Repo.
func NewRepo(repo eh.ReadWriteRepo) *Repo {
return &Repo{
ReadWriteRepo: repo,
}
}
// Parent implements the Parent method of the eventhorizon.ReadRepo interface.
func (r *Repo) Parent() eh.ReadRepo {
return r.ReadWriteRepo
}
// Find implements the Find method of the eventhorizon.ReadModel interface.
// If the context contains a min version set by WithMinVersion it will only
// return an item if its version is at least min version. If a timeout or
// deadline is set on the context it will repetedly try to get the item until
// either the version matches or the deadline is reached.
func (r *Repo) Find(ctx context.Context, id eh.UUID) (interface{}, error) {
// If there is no min version set just return the item as normally.
minVersion, ok := eh.MinVersionFromContext(ctx)
if !ok || minVersion < 1 {
return r.ReadWriteRepo.Find(ctx, id)
}
// Try to get the correct version, retry with exponentially longer intervals
// until the deadline expires. If there is no deadline just try once.
delay := &backoff.Backoff{}
_, hasDeadline := ctx.Deadline()
for {
model, err := r.findMinVersion(ctx, id, minVersion)
if rrErr, ok := err.(eh.RepoError); ok &&
(rrErr.Err == eh.ErrIncorrectModelVersion || rrErr.Err == eh.ErrModelNotFound) {
// Try again for incorrect version or if the model was not found.
} else if err != nil {
// Return any real error.
return nil, err
} else {
// Return the model.
return model, nil
}
// If there is no deadline, return whatever we have at this point.
if !hasDeadline {
return model, err
}
select {
case <-time.After(delay.Duration()):
case <-ctx.Done():
return nil, ctx.Err()
}
}
// Should never get here.
return nil, eh.RepoError{
Err: eh.ErrModelNotFound,
Namespace: eh.NamespaceFromContext(ctx),
}
}
// findMinVersion finds an item if it has a version and it is at least minVersion.
func (r *Repo) findMinVersion(ctx context.Context, id eh.UUID, minVersion int) (interface{}, error) {
model, err := r.ReadWriteRepo.Find(ctx, id)
if err != nil {
return nil, err
}
versionable, ok := model.(eh.Versionable)
if !ok {
return nil, eh.RepoError{
Err: eh.ErrModelHasNoVersion,
Namespace: eh.NamespaceFromContext(ctx),
}
}
if versionable.AggregateVersion() < minVersion {
return nil, eh.RepoError{
Err: eh.ErrIncorrectModelVersion,
Namespace: eh.NamespaceFromContext(ctx),
}
}
return model, nil
}
// Repository returns a parent ReadRepo if there is one.
func Repository(repo eh.ReadRepo) *Repo {
if repo == nil {
return nil
}
if r, ok := repo.(*Repo); ok {
return r
}
return Repository(repo.Parent())
}