Skip to content

Commit a8ec3e6

Browse files
committed
improve apikey mgmt
1 parent 9d101a8 commit a8ec3e6

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

ethstore.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const RECEIPTS_MODE_BATCH = 0
4040
const RECEIPTS_MODE_SINGLE = 1
4141

4242
var debugLevel = uint64(0)
43-
var beaconchainApiClient = NewBeaconchainApiClient("")
43+
var beaconchainApiClient = NewBeaconchainApiClient()
4444
var execTimeout = time.Second * 120
4545
var execTimeoutMu = sync.Mutex{}
4646
var consTimeout = time.Second * 120
@@ -791,33 +791,37 @@ type TxReceipt struct {
791791
}
792792

793793
type BeaconchainApiClient struct {
794-
headers atomic.Value
794+
apikey string
795+
apikeyMu sync.Mutex
795796
ratelimiter *Ratelimiter
796797
}
797798

798-
func NewBeaconchainApiClient(apiKey string) *BeaconchainApiClient {
799+
func NewBeaconchainApiClient() *BeaconchainApiClient {
799800
c := &BeaconchainApiClient{
800-
headers: atomic.Value{},
801+
apikey: "",
801802
ratelimiter: NewRatelimiter(1),
802803
}
803-
c.headers.Store(map[string]string{"apikey": apiKey})
804804
return c
805805
}
806806

807807
func (c *BeaconchainApiClient) SetApiKey(apiKey string) {
808-
c.headers.Store(map[string]string{"apikey": apiKey})
808+
c.apikeyMu.Lock()
809+
defer c.apikeyMu.Unlock()
810+
c.apikey = apiKey
811+
}
812+
813+
func (c *BeaconchainApiClient) GetApiKey() string {
814+
c.apikeyMu.Lock()
815+
defer c.apikeyMu.Unlock()
816+
return c.apikey
809817
}
810818

811819
func (c *BeaconchainApiClient) HttpReq(ctx context.Context, method, url string, headers map[string]string, params, result interface{}) error {
812820
c.ratelimiter.Wait()
813-
cHeaders := c.headers.Load().(map[string]string)
814821
if headers == nil {
815-
headers = cHeaders
816-
} else {
817-
for k, v := range cHeaders {
818-
headers[k] = v
819-
}
822+
headers = make(map[string]string)
820823
}
824+
headers["apikey"] = c.GetApiKey()
821825

822826
var err error
823827
var req *http.Request

0 commit comments

Comments
 (0)