cache address table

This commit is contained in:
thloyi
2026-01-05 14:38:02 +08:00
parent e6922e4561
commit 8c98ec7875
4 changed files with 33 additions and 71 deletions

View File

@@ -7,13 +7,14 @@ import (
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/panjf2000/ants/v2"
)
type AddressTables struct {
rpcClient *rpc.Client
mux sync.RWMutex
tables map[solana.PublicKey][]solana.PublicKey
tables *lru.Cache[solana.PublicKey, []solana.PublicKey]
loading map[solana.PublicKey]struct{}
pool *ants.Pool
@@ -21,9 +22,10 @@ type AddressTables struct {
func NewAddressTables(rpcClient *rpc.Client) *AddressTables {
pool, _ := ants.NewPool(10, ants.WithPreAlloc(true), ants.WithNonblocking(true))
cache, _ := lru.New[solana.PublicKey, []solana.PublicKey](10000)
return &AddressTables{
rpcClient: rpcClient,
tables: make(map[solana.PublicKey][]solana.PublicKey),
tables: cache,
loading: make(map[solana.PublicKey]struct{}),
pool: pool,
}
@@ -53,9 +55,8 @@ func (at *AddressTables) loadAddressTable(tablePubkey solana.PublicKey) ([]solan
}
func (at *AddressTables) GetAddressTable(tablePubkey solana.PublicKey, idx []uint8) []solana.PublicKey {
at.mux.RLock()
addresses, ok := at.tables[tablePubkey]
addresses, ok := at.tables.Get(tablePubkey)
if !ok {
at.mux.RUnlock()
_ = at.pool.Submit(func() {
@@ -79,8 +80,8 @@ func (at *AddressTables) GetAddressTable(tablePubkey solana.PublicKey, idx []uin
return
}
at.mux.Lock()
at.tables[tablePubkey] = table
total := len(at.tables)
at.tables.Add(tablePubkey, table)
total := at.tables.Len()
delete(at.loading, tablePubkey)
at.mux.Unlock()
logger.Info("loadAddressTable", "table", tablePubkey.String(), "table count:", total)