@@ -98,18 +98,19 @@ func (d *DBKeyCache) Version(ctx context.Context, sequence int32) (database.Cryp
98
98
99
99
func (d * DBKeyCache )Latest (ctx context.Context ) (database.CryptoKey ,error ) {
100
100
d .cacheMu .RLock ()
101
+ latest := d .latestKey
102
+ d .cacheMu .RUnlock ()
103
+
101
104
now := d .Clock .Now ().UTC ()
102
- if d .latestKey .IsActive (now ) {
103
- d .cacheMu .RUnlock ()
104
- return d .latestKey ,nil
105
+ if latest .IsActive (now ) {
106
+ return latest ,nil
105
107
}
106
- d .cacheMu .RUnlock ()
107
108
108
109
d .cacheMu .Lock ()
109
110
defer d .cacheMu .Unlock ()
110
111
111
- if d . latestKey .IsActive (now ) {
112
- return d . latestKey ,nil
112
+ if latest .IsActive (now ) {
113
+ return latest ,nil
113
114
}
114
115
115
116
cache ,latest ,err := d .newCache (ctx )
@@ -153,6 +154,7 @@ func (d *DBKeyCache) newCache(ctx context.Context) (map[int32]database.CryptoKey
153
154
}
154
155
cache := toMap (keys )
155
156
var latest database.CryptoKey
157
+ // Keys are returned in order from highest sequence to lowest.
156
158
for _ ,key := range keys {
157
159
if ! key .IsActive (now ) {
158
160
continue