11package proxy
22
33import (
4- "context"
54 "crypto/ecdsa"
65 "fmt"
6+ "sync"
77
8+ "github.com/gin-gonic/gin"
89 "github.com/scroll-tech/go-ethereum/common"
910 "github.com/scroll-tech/go-ethereum/crypto"
1011
@@ -13,13 +14,18 @@ import (
1314)
1415
1516type Client interface {
16- Client (context .Context ) * upClient
17+ Client (* gin .Context ) * upClient
1718}
1819
1920type ClientManager struct {
2021 cliCfg * config.ProxyClient
2122 cfg * config.UpStream
2223 privKey * ecdsa.PrivateKey
24+
25+ cachedCli struct {
26+ sync.RWMutex
27+ cli * upClient
28+ }
2329}
2430
2531// transformToValidPrivateKey safely transforms arbitrary bytes into valid private key bytes
@@ -55,8 +61,26 @@ func NewClientManager(cliCfg *config.ProxyClient, cfg *config.UpStream) (*Client
5561 }, nil
5662}
5763
58- func (cliMgr * ClientManager ) Client (ctx context.Context ) * upClient {
59- return newUpClient (cliMgr .cfg , cliMgr )
64+ func (cliMgr * ClientManager ) doLogin () * upClient {
65+ loginCli := newUpClient (cliMgr .cfg , cliMgr )
66+
67+ return loginCli
68+ }
69+
70+ func (cliMgr * ClientManager ) Client (ctx * gin.Context ) * upClient {
71+ cliMgr .cachedCli .RLock ()
72+ if cliMgr .cachedCli .cli != nil {
73+ defer cliMgr .cachedCli .RUnlock ()
74+ return cliMgr .cachedCli .cli
75+ }
76+ cliMgr .cachedCli .RUnlock ()
77+ cliMgr .cachedCli .Lock ()
78+ defer cliMgr .cachedCli .Unlock ()
79+ if cliMgr .cachedCli .cli != nil {
80+ return cliMgr .cachedCli .cli
81+ }
82+
83+ return nil
6084}
6185
6286func (cliMgr * ClientManager ) OnError (isUnauth bool ) {
0 commit comments