generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmemcached.go
77 lines (62 loc) · 2.66 KB
/
memcached.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
package datastore
import (
"encoding/json"
"fmt"
"time"
builderApi "github.com/attestantio/go-builder-client/api"
"github.com/bradfitz/gomemcache/memcache"
"github.com/flashbots/go-utils/cli"
)
var (
defaultMemcachedExpirySeconds = int32(cli.GetEnvInt("MEMCACHED_EXPIRY_SECONDS", 45))
defaultMemcachedTimeoutMs = cli.GetEnvInt("MEMCACHED_CLIENT_TIMEOUT_MS", 250)
defaultMemcachedMaxIdleConns = cli.GetEnvInt("MEMCACHED_MAX_IDLE_CONNS", 10)
)
type Memcached struct {
client *memcache.Client
keyPrefix string
}
// SaveExecutionPayload attempts to insert execution engine payload to memcached using composite key of slot,
// proposer public key, block hash, and cache prefix if specified. Note that writes to the same key value
// (i.e. same slot, proposer public key, and block hash) will overwrite the existing entry.
func (m *Memcached) SaveExecutionPayload(slot uint64, proposerPubKey, blockHash string, payload *builderApi.VersionedSubmitBlindedBlockResponse) error {
// TODO: standardize key format with redis cache and re-use the same function(s)
key := fmt.Sprintf("boost-relay/%s:cache-getpayload-response:%d_%s_%s", m.keyPrefix, slot, proposerPubKey, blockHash)
bytes, err := json.Marshal(payload)
if err != nil {
return err
}
//nolint:exhaustruct // "Flags" variable unused and opaque server-side
return m.client.Set(&memcache.Item{Key: key, Value: bytes, Expiration: defaultMemcachedExpirySeconds})
}
// GetExecutionPayload attempts to fetch execution engine payload from memcached using composite key of slot,
// proposer public key, block hash, and cache prefix if specified.
func (m *Memcached) GetExecutionPayload(slot uint64, proposerPubKey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
// TODO: standardize key format with redis cache and re-use the same function(s)
key := fmt.Sprintf("boost-relay/%s:cache-getpayload-response:%d_%s_%s", m.keyPrefix, slot, proposerPubKey, blockHash)
item, err := m.client.Get(key)
if err != nil {
return nil, err
}
result := new(builderApi.VersionedSubmitBlindedBlockResponse)
if err = result.UnmarshalJSON(item.Value); err != nil {
return nil, err
}
return result, nil
}
func NewMemcached(prefix string, servers ...string) (*Memcached, error) {
if len(servers) == 0 {
return nil, nil
}
sl := new(memcache.ServerList)
if err := sl.SetServers(servers...); err != nil {
return nil, err
}
client := memcache.NewFromSelector(sl)
if err := client.Ping(); err != nil {
return nil, err
}
client.MaxIdleConns = defaultMemcachedMaxIdleConns
client.Timeout = time.Duration(defaultMemcachedTimeoutMs) * time.Millisecond
return &Memcached{client: client, keyPrefix: prefix}, nil
}