5 Star 29 Fork 12

楠木 / etherscan-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
setup_e2e_test.go 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
楠木 提交于 2020-11-12 12:36 . CI: increase cooling period again
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"fmt"
"os"
"testing"
"time"
)
const apiKeyEnvName = "ETHERSCAN_API_KEY"
var (
// api test client for many test cases
api *Client
// bucket default rate limiter
bucket *Bucket
// apiKey etherscan API key
apiKey string
)
func init() {
apiKey = os.Getenv(apiKeyEnvName)
if apiKey == "" {
panic(fmt.Sprintf("API key is empty, set env variable %q with a valid API key to proceed.", apiKeyEnvName))
}
bucket = NewBucket(500 * time.Millisecond)
api = New(Mainnet, apiKey)
api.Verbose = true
api.BeforeRequest = func(module string, action string, param map[string]interface{}) error {
bucket.Take()
return nil
}
}
// Bucket is a simple and easy rate limiter
// Use NewBucket() to construct one.
type Bucket struct {
bucket chan bool
refillTime time.Duration
}
// NewBucket factory of Bucket
func NewBucket(refillTime time.Duration) (b *Bucket) {
b = &Bucket{
bucket: make(chan bool),
refillTime: refillTime,
}
go b.fillRoutine()
return
}
// Take a action token from bucket,
// blocks if there is currently no token left.
func (b *Bucket) Take() {
<-b.bucket
}
// fill a action token into bucket,
// no-op if the bucket is currently full
func (b *Bucket) fill() {
b.bucket <- true
}
func (b *Bucket) fillRoutine() {
ticker := time.NewTicker(b.refillTime)
for range ticker.C {
b.fill()
}
}
// noError checks for testing error
func noError(t *testing.T, err error, msg string) {
if err != nil {
t.Fatalf("%s: %v", msg, err)
}
}
Go
1
https://gitee.com/nanmu42/etherscan-api.git
git@gitee.com:nanmu42/etherscan-api.git
nanmu42
etherscan-api
etherscan-api
master

搜索帮助