-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathestimated.go
69 lines (53 loc) · 1.59 KB
/
estimated.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
package data
import (
"context"
"errors"
"github.com/bits-and-blooms/bloom/v3"
"github.com/code-payments/code-server/pkg/metrics"
)
const (
estimatedProviderMetricsName = "data.estimated_provider"
)
var (
maxEstimatedAccounts = 1000000
maxEstimatedAccountsErrRate = 0.01
)
var (
ErrInvalidAccount = errors.New("invalid account")
)
type EstimatedData interface {
// Account
// --------------------------------------------------------------------------------
TestForKnownAccount(ctx context.Context, account []byte) (bool, error)
AddKnownAccount(ctx context.Context, account []byte) error
}
type EstimatedProvider struct {
knownAccounts *bloom.BloomFilter
}
func NewEstimatedProvider() (EstimatedData, error) {
filter := bloom.NewWithEstimates(uint(maxEstimatedAccounts), maxEstimatedAccountsErrRate)
return &EstimatedProvider{
knownAccounts: filter,
}, nil
}
// Account
// --------------------------------------------------------------------------------
func (p *EstimatedProvider) TestForKnownAccount(ctx context.Context, account []byte) (bool, error) {
tracer := metrics.TraceMethodCall(ctx, estimatedProviderMetricsName, "TestForKnownAccount")
defer tracer.End()
if len(account) > 0 {
} else {
return false, ErrInvalidAccount
}
return p.knownAccounts.Test(account), nil
}
func (p *EstimatedProvider) AddKnownAccount(ctx context.Context, account []byte) error {
tracer := metrics.TraceMethodCall(ctx, estimatedProviderMetricsName, "AddKnownAccount")
defer tracer.End()
if len(account) > 0 {
p.knownAccounts.Add(account)
} else {
return ErrInvalidAccount
}
return nil
}