Skip to content

Commit

Permalink
fix -3100
Browse files Browse the repository at this point in the history
zc2638 committed Apr 14, 2022
1 parent d6e9a12 commit 4641fba
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 13 additions & 3 deletions cmd/ddshop/app/app.go
Original file line number Diff line number Diff line change
@@ -91,9 +91,18 @@ func NewRootCommand() *cobra.Command {
}()

select {
case err := <-errCh:
case resultErr := <-errCh:
cancelFunc()
return err
go func() {
if opt.BarkKey == "" {
return
}
ins := notice.NewBark(opt.BarkKey)
if err := ins.Send("抢菜异常", resultErr.Error()); err != nil {
logrus.Warningf("Bark消息通知失败: %v", err)
}
}()
return resultErr
case <-successCh:
cancelFunc()
core.LoopRun(10, func() {
@@ -122,9 +131,10 @@ func NewRootCommand() *cobra.Command {

cookieEnv := os.Getenv("DDSHOP_COOKIE")
barkKeyEnv := os.Getenv("DDSHOP_BARKKEY")
payTypeEnv := os.Getenv("DDSHOP_PAYTYPE")
cmd.Flags().StringVar(&opt.Cookie, "cookie", cookieEnv, "设置用户个人cookie")
cmd.Flags().StringVar(&opt.BarkKey, "bark-key", barkKeyEnv, "设置bark的通知key")
cmd.Flags().StringVar(&opt.PayType, "pay-type", "", "设置支付方式,支付宝、微信、alipay、wechat")
cmd.Flags().StringVar(&opt.PayType, "pay-type", payTypeEnv, "设置支付方式,支付宝、微信、alipay、wechat")
cmd.Flags().Int64Var(&opt.Interval, "interval", 500, "设置请求间隔时间(ms),默认为100")
return cmd
}
18 changes: 14 additions & 4 deletions core/session.go
Original file line number Diff line number Diff line change
@@ -95,6 +95,10 @@ func (s *Session) Clone() *Session {
}

func (s *Session) execute(ctx context.Context, request *resty.Request, method, url string) (*resty.Response, error) {
return s.executeRetry(ctx, request, method, url, 1)
}

func (s *Session) executeRetry(ctx context.Context, request *resty.Request, method, url string, frequency int) (*resty.Response, error) {
if ctx != nil {
request.SetContext(ctx)
}
@@ -113,12 +117,18 @@ func (s *Session) execute(ctx context.Context, request *resty.Request, method, u
return resp, nil
case -3000, -3001:
logrus.Warningf("当前人多拥挤(%v): %s", code, resp.String())
case -3100:
logrus.Warningf("当前页面拥挤(%v): %s", code, resp.String())
logrus.Warningf("将在 %dms 后重试", s.interval)
time.Sleep(time.Duration(s.interval) * time.Millisecond)
default:
return nil, fmt.Errorf("无法识别的状态码: %v", resp.String())
if frequency > 15 {
return nil, fmt.Errorf("无法识别的状态码: %v", resp.String())
}
logrus.Warningf("尝试次数: %d, 无法识别的状态码: %v", frequency, resp.String())
}
logrus.Warningf("将在 %dms 后重试", s.interval)
time.Sleep(time.Duration(s.interval) * time.Millisecond)
return s.execute(nil, request, method, url)
frequency++
return s.executeRetry(nil, request, method, url, frequency)
}

func (s *Session) buildHeader() http.Header {

0 comments on commit 4641fba

Please sign in to comment.