Skip to content

Commit

Permalink
Add cache for prover
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Sep 26, 2024
1 parent 8305174 commit 0eeca13
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/tonkeeper/claim-api-go/pkg/utils"
"github.com/tonkeeper/tongo/tvm"

"github.com/avast/retry-go"
Expand All @@ -34,6 +35,9 @@ type Handler struct {
cli *liteapi.Client
config string

proofsCache utils.Cache[ton.AccountID, prover.WalletAirdrop]
keyNotFoundCache utils.Cache[ton.AccountID, struct{}]

mu sync.RWMutex
jettonMasterStateCache map[ton.AccountID][2]string
}
Expand Down Expand Up @@ -81,6 +85,8 @@ func NewHandler(logger *zap.Logger, config Config) (*Handler, error) {
jettonMaster: config.JettonMaster,
jettonMasterStateCache: map[ton.AccountID][2]string{},
config: blockchainConfig,
proofsCache: utils.NewLRUCache[ton.AccountID, prover.WalletAirdrop](700_000, "proofs"),
keyNotFoundCache: utils.NewLRUCache[ton.AccountID, struct{}](700_000, "keyNotFound"),
}, nil
}

Expand Down Expand Up @@ -135,6 +141,18 @@ func (h *Handler) GetWalletInfo(ctx context.Context, params oas.GetWalletInfoPar
if err != nil {
return nil, BadRequest("failed to parse account id")
}

if proof, ok := h.proofsCache.Get(accountID); ok {
info, err := h.convertToWalletInfo(ctx, proof)
if err != nil {
return nil, InternalError(err)
}
return info, nil
}
if _, ok := h.keyNotFoundCache.Get(accountID); ok {
return nil, NotFound("account not found")
}

responseCh := make(chan prover.ProofResponse, 1)
h.prover.Queue() <- prover.ProofRequest{
AccountID: accountID,
Expand All @@ -145,11 +163,13 @@ func (h *Handler) GetWalletInfo(ctx context.Context, params oas.GetWalletInfoPar
return nil, BadRequest("timeout")
case resp := <-responseCh:
if resp.Err != nil && strings.Contains(resp.Err.Error(), "key is not found") {
return nil, NotFound("account not not found")
h.keyNotFoundCache.Set(accountID, struct{}{})
return nil, NotFound("account not found")
}
if resp.Err != nil {
return nil, InternalError(resp.Err)
}
h.proofsCache.Set(accountID, resp.WalletAirdrop, utils.WithExpiration(7*time.Minute))
info, err := h.convertToWalletInfo(ctx, resp.WalletAirdrop)
if err != nil {
return nil, InternalError(err)
Expand Down

0 comments on commit 0eeca13

Please sign in to comment.