diff --git a/api/http/v1/dto/http.go b/api/http/v1/dto/http.go index bddf955..e150565 100644 --- a/api/http/v1/dto/http.go +++ b/api/http/v1/dto/http.go @@ -1,7 +1,5 @@ package dto -import "time" - // PushRequest 表示推送请求的结构体 type PushRequest struct { // AppID 应用程序标识 @@ -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 { diff --git a/push/fcm_push.go b/push/fcm_push.go index de5c4e1..db65867 100644 --- a/push/fcm_push.go +++ b/push/fcm_push.go @@ -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 { @@ -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 diff --git a/push/honor_push.go b/push/honor_push.go index adab74f..401bd54 100644 --- a/push/honor_push.go +++ b/push/honor_push.go @@ -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 { @@ -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 // diff --git a/push/huawei_push.go b/push/huawei_push.go index 8ef3ba9..d7e2bea 100644 --- a/push/huawei_push.go +++ b/push/huawei_push.go @@ -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 diff --git a/push/meizu_push.go b/push/meizu_push.go index 105827d..08e1836 100644 --- a/push/meizu_push.go +++ b/push/meizu_push.go @@ -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) diff --git a/push/oppo_push.go b/push/oppo_push.go index 0a41c7d..a2d0f8a 100644 --- a/push/oppo_push.go +++ b/push/oppo_push.go @@ -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) diff --git a/push/option.go b/push/option.go index 0910951..0f4ecc5 100644 --- a/push/option.go +++ b/push/option.go @@ -1,7 +1,5 @@ package push -import "time" - type SendOption interface { Apply(option *SendOptions) } @@ -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) { diff --git a/push/push.go b/push/push.go index c1a4459..4db5843 100644 --- a/push/push.go +++ b/push/push.go @@ -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 @@ -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 @@ -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 } diff --git a/push/vivo_push.go b/push/vivo_push.go index 82d3f49..d156dbb 100644 --- a/push/vivo_push.go +++ b/push/vivo_push.go @@ -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 } diff --git a/push/xiaomi_push.go b/push/xiaomi_push.go index 0ba7a3e..6b6eb2a 100644 --- a/push/xiaomi_push.go +++ b/push/xiaomi_push.go @@ -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 } diff --git a/store/file.go b/store/file.go index c499983..c157907 100644 --- a/store/file.go +++ b/store/file.go @@ -2,7 +2,6 @@ package store import ( "encoding/json" - "fmt" "io/ioutil" "log" "os" @@ -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) {