guonaihong / gout

gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

是否可以支持请求的复用

yimingsir opened this issue · comments

类似创建一个client,设置全局的header和一些callback函数,以后每次请求就不用重复创建client了。

        client = gout.New()
	client.
		Debug(true).
		SetTimeout(time.Duration(cc.timeout) * time.Second).
		SetHeader(gout.H{
			"Content-Type":  "application/json;v=1.0",
			"Accept":        "application/json",
			"Authorization": fmt.Sprintf("Bearer %s", cc.token),
		}).F().
		Retry().Attempt(3).WaitTime(time.Millisecond * 3).MaxWaitTime(time.Millisecond * 10).
		Func(func(c *gout.Context) error {
			if c.Error != nil {
				return filter.ErrRetry
			}
			// token过期,重新获取token后重试
			if c.Code == 401 {
				cc.Login()
				return filter.ErrRetry
			}
			return nil
		})

这个我考虑了下