-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathantispam.go
More file actions
94 lines (83 loc) · 3.22 KB
/
Copy pathantispam.go
File metadata and controls
94 lines (83 loc) · 3.22 KB
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
// Copyright 2024 The Tessera authors. All Rights Reserved.
//
// 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 tessera
import (
"context"
"fmt"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
)
// newInMemoryDedup wraps an Add function to prevent duplicate entries being written to the underlying
// storage by keeping an in-memory cache of recently seen entries.
// Where an existing entry has already been `Add`ed, the previous `IndexFuture` will be returned.
// When no entry is found in the cache, the delegate method will be called to store the entry, and
// the result will be registered in the cache.
//
// Internally this uses a cache with a max size configured by the size parameter.
// If the entry being `Add`ed is not found in the cache, then it calls the delegate.
//
// This object can be used in isolation, or in conjunction with a persistent dedup implementation.
// When using this with a persistent dedup, the persistent layer should be the delegate of this
// InMemoryDedup. This allows recent duplicates to be deduplicated in memory, reducing the need to
// make calls to a persistent storage.
func newInMemoryDedup(size uint) func(AddFn) AddFn {
return func(af AddFn) AddFn {
c, err := lru.New[string, func() IndexFuture](int(size))
if err != nil {
panic(fmt.Errorf("lru.New(%d): %v", size, err))
}
dedup := &inMemoryDedup{
delegate: af,
cache: c,
}
return dedup.add
}
}
type inMemoryDedup struct {
delegate func(ctx context.Context, e *Entry) IndexFuture
cache *lru.Cache[string, func() IndexFuture]
}
// Add adds the entry to the underlying delegate only if e hasn't been recently seen. In either case,
// an IndexFuture will be returned that the client can use to get the sequence number of this entry.
func (d *inMemoryDedup) add(ctx context.Context, e *Entry) IndexFuture {
ctx, span := tracer.Start(ctx, "tessera.Appender.inmemoryDedup.Add")
defer span.End()
id := string(e.Identity())
f := sync.OnceValue(func() IndexFuture {
// However many calls with the same entry come in and are deduplicated, we should only call delegate
// once for each unique entry:
df := d.delegate(ctx, e)
return func() (Index, error) {
idx, err := df()
// If things went wrong we shouldn't cache the error, but rather let the request be retried as the error
// may be transient (including ErrPushback).
if err != nil {
d.cache.Remove(id)
}
return idx, err
}
})
// if we've seen this entry before, discard our f and replace
// with the one we created last time, otherwise store f against id.
if prev, ok, _ := d.cache.PeekOrAdd(id, f); ok {
f = func() IndexFuture {
return func() (Index, error) {
i, err := prev()()
i.IsDup = true
return i, err
}
}
}
return f()
}