Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3). Masyunis customizations are re-applied in the following commits.
This commit is contained in:
1 parent
eb3e46d8d0
commit
e07de898cd
573 files changed
+35846
-6807
No files matched your search
@@ -50,6 +50,33 @@ func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
})
|
||||
}
|
||||
|
||||
// deleteIfExpired removes key only if it still carries the exact expiry that an
|
||||
// expired Get observed and that expiry is still in the past.
|
||||
//
|
||||
// Get runs in a read-only transaction, so it can only schedule cleanup
|
||||
// asynchronously. Between observing the expiry and this delete running, another
|
||||
// request may Set a fresh value for the same key. Re-reading and matching the
|
||||
// observed expiry inside the write transaction makes the timestamp act as a
|
||||
// generation token: a refreshed value carries a different, future expiry and is
|
||||
// therefore left untouched (see AWOO-015).
|
||||
func (s *Store) deleteIfExpired(ctx context.Context, key string, observed time.Time) error {
|
||||
return s.bdb.Update(func(tx *bbolt.Tx) error {
|
||||
valueBkt := tx.Bucket([]byte(key))
|
||||
if valueBkt == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
expiry, err := time.Parse(time.RFC3339Nano, string(valueBkt.Get([]byte("expiry"))))
|
||||
if err != nil || !expiry.Equal(observed) || !time.Now().After(expiry) {
|
||||
// Unparseable, refreshed to a different generation, or no longer
|
||||
// expired: leave it for cleanup or a later Get to handle.
|
||||
return nil
|
||||
}
|
||||
|
||||
return tx.DeleteBucket([]byte(key))
|
||||
})
|
||||
}
|
||||
|
||||
// Get a value from the datastore.
|
||||
//
|
||||
// Because each value is stored in its own bucket with data and expiry keys,
|
||||
@@ -77,7 +104,7 @@ func (s *Store) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
}
|
||||
|
||||
if time.Now().After(expiry) {
|
||||
go s.Delete(context.Background(), key)
|
||||
go s.deleteIfExpired(context.Background(), key, expiry) //nolint:errcheck
|
||||
return fmt.Errorf("%w: %q", store.ErrNotFound, key)
|
||||
}
|
||||
|
||||
@@ -131,7 +158,7 @@ func (s *Store) cleanup(ctx context.Context) error {
|
||||
|
||||
expiryStr := valueBkt.Get([]byte("expiry"))
|
||||
if expiryStr == nil {
|
||||
slog.Warn("while running cleanup, expiry is not set somehow, file a bug?", "key", string(key))
|
||||
slog.WarnContext(ctx, "while running cleanup, expiry is not set somehow, file a bug?", "key", string(key))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -163,7 +190,7 @@ func (s *Store) cleanupThread(ctx context.Context) {
|
||||
return
|
||||
case <-t.C:
|
||||
if err := s.cleanup(ctx); err != nil {
|
||||
slog.Error("error during bbolt cleanup", "err", err)
|
||||
slog.ErrorContext(ctx, "error during bbolt cleanup", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user