Skip to content

Commit

Permalink
fix(status): 修复status使用错误
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 11, 2024
1 parent fb233d9 commit fd09cf4
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 36 deletions.
6 changes: 2 additions & 4 deletions api/http/v1/dto/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dto

import "time"

// PushRequest 表示推送请求的结构体
type PushRequest struct {
// AppID 应用程序标识
Expand Down Expand Up @@ -31,8 +29,8 @@ type PushOption struct {
// Retry 重试次数
Retry int `json:"retry,omitempty"`

// RetryInterval 重试间隔
RetryInterval time.Duration `json:"retry_interval"`
// RetryInterval 重试间隔(以秒为单位)
RetryInterval int `json:"retry_interval,omitempty"`
}

type PushStatRequest struct {
Expand Down
4 changes: 3 additions & 1 deletion push/fcm_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (f *FCMService) send(ctx context.Context, appid string, token string, notif

resp := &Response{Code: Fail}

f.status.AddAndroidTotal(1)

notification.Token = token
res, err := client.Send(ctx, notification)
if err != nil {
Expand All @@ -150,7 +152,7 @@ func (f *FCMService) send(ctx context.Context, appid string, token string, notif
resp.Msg = res
}

return resp, nil
return resp, err
}

// checkNotification for check request message
Expand Down
6 changes: 4 additions & 2 deletions push/honor_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func (h *HonorService) send(ctx context.Context, appid string, token string, not
return nil, errors.New("invalid appid or appid push is not enabled")
}

resp := &Response{}
h.status.AddHonorTotal(1)

resp := &Response{Code: Fail}
notification.Token = []string{token}
res, err := client.SendMessage(ctx, appid, notification)
if err != nil {
Expand All @@ -124,7 +126,7 @@ func (h *HonorService) send(ctx context.Context, appid string, token string, not
resp.Data = res.Data
}

return resp, nil
return resp, err

//var es []error
//
Expand Down
8 changes: 5 additions & 3 deletions push/huawei_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,23 @@ func (h *HMSService) send(ctx context.Context, appid string, token string, notif
return nil, errors.New("invalid appid or appid push is not enabled")
}

h.status.AddHuaweiTotal(1)

resp := &Response{}
notification.Message.Token = []string{token}
res, err := client.SendMessage(ctx, notification)
if err != nil {
log.Printf("hcm send error: %s", err)
log.Printf("huawei send error: %s", err)
h.status.AddHuaweiFailed(1)
resp.Code = Fail
resp.Msg = res.Msg
} else if res != nil && res.Code != "80000000" {
log.Printf("honor send error: %s", res.Msg)
log.Printf("huawei send error: %s", res.Msg)
h.status.AddHonorFailed(1)
err = errors.New(res.Msg)
resp.Msg = res.Msg
} else {
log.Printf("hcm send success: %s", res)
log.Printf("huawei send success: %s", res)
h.status.AddHuaweiSuccess(1)
resp.Code = Success
resp.Msg = res.Msg
Expand Down
2 changes: 2 additions & 0 deletions push/meizu_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (m *MeizuService) send(appid string, token string, message string) (*Respon
return nil, errors.New("invalid appid or appid push is not enabled")
}

m.status.AddMeizuTotal(1)

var err error
resp := &Response{}
res := pushFunc(token, message)
Expand Down
2 changes: 2 additions & 0 deletions push/oppo_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (o *OppoService) send(appID string, token string, notification *op.Message)
return nil, errors.New("invalid appid or appid push is not enabled")
}

o.status.AddOppoTotal(1)

resp := &Response{Code: Fail}
notification.SetTargetValue(token)
res, err := client.Unicast(notification)
Expand Down
6 changes: 2 additions & 4 deletions push/option.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package push

import "time"

type SendOption interface {
Apply(option *SendOptions)
}
Expand All @@ -12,8 +10,8 @@ type SendOptions struct {
DryRun bool `json:"dry_run,omitempty"`
// Retry 重试次数
Retry int `json:"retry,omitempty"`
// RetryInterval 重试间隔
RetryInterval time.Duration `json:"retry_interval"`
// RetryInterval 重试间隔(以秒为单位)
RetryInterval int `json:"retry_interval"`
}

func (s *SendOptions) Apply(option *SendOptions) {
Expand Down
22 changes: 15 additions & 7 deletions push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ const (

type SendFunc func(ctx context.Context, token string) (*Response, error)

func RetrySend(ctx context.Context, send SendFunc, tokens []string, retry int, retryInterval time.Duration, maxConcurrent int) error {
func RetrySend(ctx context.Context, send SendFunc, tokens []string, retry int, retryInterval int, maxConcurrent int) error {
var wg sync.WaitGroup
if retryInterval <= 0 {
retryInterval = time.Second
retryInterval = 1
}
if maxConcurrent <= 0 {
maxConcurrent = 100
Expand All @@ -146,8 +146,11 @@ func RetrySend(ctx context.Context, send SendFunc, tokens []string, retry int, r
} else {
es = append(es, err)
}
log.Printf("send error: %s (attempt %d)", err, i+1)
time.Sleep(retryInterval)
if i == 0 {
continue
}
log.Printf("send error: %s (attempt %d)", err, i)
time.Sleep(time.Duration(retryInterval) * time.Second)
} else {
log.Printf("send success: %s", res.Msg)
break
Expand All @@ -157,12 +160,17 @@ func RetrySend(ctx context.Context, send SendFunc, tokens []string, retry int, r
}
wg.Wait()
if len(es) > 0 {
var errorStrings []string
uniqueErrors := make(map[string]struct{})
for _, err := range es {
errorStrings = append(errorStrings, err.Error())
uniqueErrors[err.Error()] = struct{}{}
}
var uniqueErrorStrings []string
for err := range uniqueErrors {
uniqueErrorStrings = append(uniqueErrorStrings, err)
}
allErrorsString := strings.Join(errorStrings, ", ")
allErrorsString := strings.Join(uniqueErrorStrings, ", ")
return errors.New(allErrorsString)
}

return nil
}
18 changes: 9 additions & 9 deletions push/vivo_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,24 @@ func (v *VivoService) send(appid string, token string, notification *vp.Message)
return nil, errors.New("invalid appid or appid push is not enabled")
}

resp := &Response{
Code: Fail,
}
v.status.AddVivoTotal(1)

resp := &Response{Code: Fail}
notification.RegId = token
res, err := client.Send(notification, token)
if err != nil {
log.Printf("oppo send error: %s", err)
v.status.AddOppoFailed(1)
log.Printf("vivo send error: %s", err)
v.status.AddVivoFailed(1)
resp.Msg = err.Error()
} else if res != nil && res.Result != 0 {
log.Printf("oppo send error: %s", res.Desc)
v.status.AddOppoFailed(1)
log.Printf("vivo send error: %s", res.Desc)
v.status.AddVivoFailed(1)
err = errors.New(res.Desc)
resp.Code = res.Result
resp.Msg = res.Desc
} else {
log.Printf("oppo send success: %s", res.Desc)
v.status.AddOppoSuccess(1)
log.Printf("vivo send success: %s", res.Desc)
v.status.AddVivoSuccess(1)
resp.Code = Success
resp.Msg = res.Desc
}
Expand Down
10 changes: 6 additions & 4 deletions push/xiaomi_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,23 @@ func (x *XiaomiPushService) send(ctx context.Context, appID string, token string
return nil, errors.New("invalid appid or appid push is not enabled")
}

resp := &Response{}
x.status.AddXiaomiTotal(1)

resp := &Response{Code: Fail}
res, err := client.Send(ctx, message, token)
if err != nil {
log.Printf("xiaomi send error: %s", err)
x.status.AddOppoFailed(1)
x.status.AddXiaomiFailed(1)
resp.Msg = err.Error()
} else if res != nil && res.Code != 0 {
log.Printf("xiaomi send error: %s", res.Reason)
x.status.AddOppoFailed(1)
x.status.AddXiaomiFailed(1)
err = errors.New(res.Reason)
resp.Code = int(res.Code)
resp.Msg = res.Reason
} else {
log.Printf("xiaomi send success: %s", res.Reason)
x.status.AddOppoSuccess(1)
x.status.AddXiaomiSuccess(1)
resp.Code = Success
resp.Msg = res.Reason
}
Expand Down
2 changes: 0 additions & 2 deletions store/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package store

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -138,7 +137,6 @@ func (fs *FileStore) saveToFile() error {
if err != nil {
return err
}
fmt.Println("periodic save data to file data => ", string(data))

dir := filepath.Dir(fs.path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
Expand Down

0 comments on commit fd09cf4

Please sign in to comment.