加入 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)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化