|
@@ -24,13 +24,18 @@ var ( |
|
@@ -24,13 +24,18 @@ var ( |
24
|
authtoken = ""
|
24
|
authtoken = ""
|
25
|
expire time.Time
|
25
|
expire time.Time
|
26
|
authMux sync.RWMutex
|
26
|
authMux sync.RWMutex
|
27
|
- expireSpan = time.Second * 3600 * 6
|
27
|
+ expireSpan = time.Second * 3600
|
|
|
28
|
+)
|
|
|
29
|
+
|
|
|
30
|
+const (
|
|
|
31
|
+ error_not_auth = "not_auth"
|
28
|
)
|
32
|
)
|
29
|
|
33
|
|
30
|
//GetuiNotification 个推消息推送
|
34
|
//GetuiNotification 个推消息推送
|
31
|
type GetuiNotification struct {
|
35
|
type GetuiNotification struct {
|
32
|
Options *push.Options
|
36
|
Options *push.Options
|
33
|
Request *httplib.BeegoHTTPRequest
|
37
|
Request *httplib.BeegoHTTPRequest
|
|
|
38
|
+ retry int
|
34
|
}
|
39
|
}
|
35
|
|
40
|
|
36
|
func (notify *GetuiNotification) Init(options ...push.Option) error {
|
41
|
func (notify *GetuiNotification) Init(options ...push.Option) error {
|
|
@@ -38,19 +43,32 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { |
|
@@ -38,19 +43,32 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { |
38
|
for _, o := range options {
|
43
|
for _, o := range options {
|
39
|
o(notify.Options)
|
44
|
o(notify.Options)
|
40
|
}
|
45
|
}
|
|
|
46
|
+ notify.retry = 3
|
41
|
return nil
|
47
|
return nil
|
42
|
}
|
48
|
}
|
43
|
func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
|
49
|
func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
|
44
|
- switch notify.Options.PushType {
|
|
|
45
|
- case push.PushToSingle:
|
|
|
46
|
- err = notify.pushToSingle(option)
|
|
|
47
|
- case push.PushToList:
|
|
|
48
|
- err = notify.pushToList(option)
|
|
|
49
|
- default:
|
|
|
50
|
- err = notify.pushToSingle(option)
|
|
|
51
|
- }
|
|
|
52
|
- if err != nil {
|
|
|
53
|
- return err
|
50
|
+ retry := 1
|
|
|
51
|
+ for {
|
|
|
52
|
+ switch notify.Options.PushType {
|
|
|
53
|
+ case push.PushToSingle:
|
|
|
54
|
+ err = notify.pushToSingle(option)
|
|
|
55
|
+ case push.PushToList:
|
|
|
56
|
+ err = notify.pushToList(option)
|
|
|
57
|
+ default:
|
|
|
58
|
+ err = notify.pushToSingle(option)
|
|
|
59
|
+ }
|
|
|
60
|
+ if err == nil {
|
|
|
61
|
+ break
|
|
|
62
|
+ }
|
|
|
63
|
+ //重试
|
|
|
64
|
+ if err != nil && retry > notify.retry {
|
|
|
65
|
+ return err
|
|
|
66
|
+ }
|
|
|
67
|
+ log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err))
|
|
|
68
|
+ retry++
|
|
|
69
|
+ if retry > notify.retry {
|
|
|
70
|
+ break
|
|
|
71
|
+ }
|
54
|
}
|
72
|
}
|
55
|
return nil
|
73
|
return nil
|
56
|
}
|
74
|
}
|
|
@@ -206,6 +224,7 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { |
|
@@ -206,6 +224,7 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { |
206
|
authtoken = rsp.AuthToken
|
224
|
authtoken = rsp.AuthToken
|
207
|
token = rsp.AuthToken
|
225
|
token = rsp.AuthToken
|
208
|
expire = time.Now().Add(expireSpan)
|
226
|
expire = time.Now().Add(expireSpan)
|
|
|
227
|
+ log.Info(fmt.Sprintf("【个推】token:%v expire:%v", token, expire))
|
209
|
return
|
228
|
return
|
210
|
}
|
229
|
}
|
211
|
|
230
|
|
|
@@ -222,6 +241,11 @@ func handleResult(url string, result *Result) (err error) { |
|
@@ -222,6 +241,11 @@ func handleResult(url string, result *Result) (err error) { |
222
|
if strings.ToLower(result.Result) == "ok" {
|
241
|
if strings.ToLower(result.Result) == "ok" {
|
223
|
return
|
242
|
return
|
224
|
}
|
243
|
}
|
|
|
244
|
+ switch result.Result {
|
|
|
245
|
+ case error_not_auth:
|
|
|
246
|
+ setToken("")
|
|
|
247
|
+ break
|
|
|
248
|
+ }
|
225
|
err = fmt.Errorf("grequest fail,url:%v error:%v", url, result.Result)
|
249
|
err = fmt.Errorf("grequest fail,url:%v error:%v", url, result.Result)
|
226
|
return err
|
250
|
return err
|
227
|
}
|
251
|
}
|
|
@@ -230,3 +254,9 @@ func sign(appkey, timestamp, mastersecret string) string { |
|
@@ -230,3 +254,9 @@ func sign(appkey, timestamp, mastersecret string) string { |
230
|
sha.Write([]byte(appkey + timestamp + mastersecret))
|
254
|
sha.Write([]byte(appkey + timestamp + mastersecret))
|
231
|
return fmt.Sprintf("%x", sha.Sum(nil))
|
255
|
return fmt.Sprintf("%x", sha.Sum(nil))
|
232
|
}
|
256
|
}
|
|
|
257
|
+
|
|
|
258
|
+func setToken(token string) {
|
|
|
259
|
+ authMux.Lock()
|
|
|
260
|
+ defer authMux.Unlock()
|
|
|
261
|
+ authtoken = ""
|
|
|
262
|
+} |