Skip to content

Commit 5d9ffcc

Browse files
committed
Squashed 'src/github.com/uniqush/uniqush-push/' changes from fe9fdc1..aa8d278
aa8d278 min. wait time is 1 sec 0d7b936 randomly wait to prevent message storm 21f4735 version 0ad2647 To work with CL 67010043 a4c9fc4 version ff309e0 wrong waiting time in apns. 0320967 Try to fix #45 525f18a fix #44 3944e04 try to recover on temporary error. git-subtree-dir: src/github.com/uniqush/uniqush-push git-subtree-split: aa8d278e0fad38bbb00cb84111f5586e2e3e7d6a
1 parent ad285c1 commit 5d9ffcc

File tree

7 files changed

+87
-40
lines changed

7 files changed

+87
-40
lines changed

db/pushredisdb.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ package db
2020
import (
2121
"errors"
2222
"fmt"
23-
redis "github.com/monnand/goredis"
24-
. "github.com/uniqush/uniqush-push/push"
2523
"strconv"
2624
"strings"
25+
26+
redis "github.com/monnand/goredis"
27+
. "github.com/uniqush/uniqush-push/push"
2728
)
2829

2930
type PushRedisDB struct {
@@ -75,22 +76,22 @@ func newPushRedisDB(c *DatabaseConfig) (*PushRedisDB, error) {
7576
return ret, nil
7677
}
7778

78-
func (r *PushRedisDB) keyValueToDeliveryPoint(name string, value []byte) *DeliveryPoint {
79+
func (r *PushRedisDB) keyValueToDeliveryPoint(name string, value []byte) (dp *DeliveryPoint, err error) {
7980
psm := r.psm
80-
dp, err := psm.BuildDeliveryPointFromBytes(value)
81+
dp, err = psm.BuildDeliveryPointFromBytes(value)
8182
if err != nil {
82-
return nil
83+
dp = nil
8384
}
84-
return dp
85+
return
8586
}
8687

87-
func (r *PushRedisDB) keyValueToPushServiceProvider(name string, value []byte) *PushServiceProvider {
88+
func (r *PushRedisDB) keyValueToPushServiceProvider(name string, value []byte) (psp *PushServiceProvider, err error) {
8889
psm := r.psm
89-
psp, err := psm.BuildPushServiceProviderFromBytes(value)
90+
psp, err = psm.BuildPushServiceProviderFromBytes(value)
9091
if err != nil {
91-
return nil
92+
psp = nil
9293
}
93-
return psp
94+
return
9495
}
9596

9697
func deliveryPointToValue(dp *DeliveryPoint) []byte {
@@ -109,7 +110,7 @@ func (r *PushRedisDB) GetDeliveryPoint(name string) (*DeliveryPoint, error) {
109110
if b == nil {
110111
return nil, nil
111112
}
112-
return r.keyValueToDeliveryPoint(name, b), nil
113+
return r.keyValueToDeliveryPoint(name, b)
113114
}
114115

115116
func (r *PushRedisDB) SetDeliveryPoint(dp *DeliveryPoint) error {
@@ -125,7 +126,7 @@ func (r *PushRedisDB) GetPushServiceProvider(name string) (*PushServiceProvider,
125126
if b == nil {
126127
return nil, nil
127128
}
128-
return r.keyValueToPushServiceProvider(name, b), nil
129+
return r.keyValueToPushServiceProvider(name, b)
129130
}
130131

131132
func (r *PushRedisDB) SetPushServiceProvider(psp *PushServiceProvider) error {

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ package main
2020
import (
2121
"flag"
2222
"fmt"
23-
. "github.com/uniqush/uniqush-push/srv"
2423
"os"
2524
"runtime"
25+
. "github.com/uniqush/uniqush-push/srv"
2626
)
2727

2828
var uniqushPushConfFlags = flag.String("config", "/etc/uniqush/uniqush-push.conf", "Config file path")
2929
var uniqushPushShowVersionFlag = flag.Bool("version", false, "Version info")
3030

31-
var uniqushPushVersion = "uniqush-push 1.5.1"
31+
var uniqushPushVersion = "uniqush-push 1.5.2"
3232

3333
func installPushSrvices() {
3434
InstallGCM()

push/errors.go

+14
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,26 @@ type RetryError struct {
4747
Provider *PushServiceProvider
4848
Destination *DeliveryPoint
4949
Content *Notification
50+
Reason error
5051
}
5152

5253
func (e *RetryError) Error() string {
54+
if e.Reason != nil {
55+
return fmt.Sprintf("Retry (%v)", e.Reason)
56+
}
5357
return fmt.Sprintf("Retry")
5458
}
5559

60+
func NewRetryErrorWithReason(psp *PushServiceProvider, dp *DeliveryPoint, notif *Notification, after time.Duration, reason error) error {
61+
return &RetryError{
62+
After: after,
63+
Provider: psp,
64+
Destination: dp,
65+
Content: notif,
66+
Reason: reason,
67+
}
68+
}
69+
5670
func NewRetryError(psp *PushServiceProvider, dp *DeliveryPoint, notif *Notification, after time.Duration) error {
5771
return &RetryError{
5872
After: after,

pushbackend.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package main
1919

2020
import (
21+
"sync"
22+
"time"
2123
. "github.com/uniqush/log"
2224
. "github.com/uniqush/uniqush-push/db"
2325
. "github.com/uniqush/uniqush-push/push"
24-
"sync"
25-
"time"
2626
)
2727

2828
type PushBackEnd struct {
@@ -111,7 +111,7 @@ func (self *PushBackEnd) fixError(reqId string, event error, logger Logger, afte
111111
if sub, ok = err.Destination.FixedData["subscriber"]; !ok {
112112
return nil
113113
}
114-
if after <= 0*time.Second {
114+
if after <= 1*time.Second {
115115
after = 5 * time.Second
116116
}
117117
if after > 1*time.Minute {

restapi.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ package main
2020
import (
2121
"crypto/rand"
2222
"encoding/base64"
23-
"time"
24-
2523
"fmt"
26-
27-
"github.com/uniqush/log"
28-
. "github.com/uniqush/uniqush-push/push"
2924
"io"
3025
"net/http"
3126
"regexp"
3227
"strconv"
3328
"strings"
3429
"sync"
30+
"time"
31+
32+
"github.com/uniqush/log"
33+
. "github.com/uniqush/uniqush-push/push"
3534
)
3635

3736
type RestAPI struct {
@@ -115,7 +114,13 @@ func getSubscribersFromMap(kv map[string]string, validate bool) (subs []string,
115114
return
116115
}
117116
}
118-
subs = strings.Split(v, ",")
117+
s := strings.Split(v, ",")
118+
subs = make([]string, 0, len(s))
119+
for _, sub := range s {
120+
if len(sub) > 0 {
121+
subs = append(subs, sub)
122+
}
123+
}
119124
if validate {
120125
err = validateSubscribers(subs)
121126
if err != nil {
@@ -247,7 +252,7 @@ func (self *RestAPI) pushNotification(reqId string, kv map[string]string, perdp
247252
return
248253
}
249254

250-
logger.Infof("RequestId=%v From=%v Service=%v Subscribers=\"%v\"", reqId, remoteAddr, service, subs)
255+
logger.Infof("RequestId=%v From=%v Service=%v NrSubscribers=%v Subscribers=\"%+v\"", reqId, remoteAddr, service, len(subs), subs)
251256

252257
self.backend.Push(reqId, service, subs, notif, perdp, logger)
253258
return

srv/apns.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ import (
2525
"encoding/json"
2626
"errors"
2727
"fmt"
28-
"github.com/uniqush/cache"
29-
"github.com/uniqush/connpool"
30-
. "github.com/uniqush/uniqush-push/push"
3128
"io"
3229
"math/rand"
3330
"net"
@@ -36,6 +33,10 @@ import (
3633
"sync"
3734
"sync/atomic"
3835
"time"
36+
37+
"github.com/uniqush/cache"
38+
"github.com/uniqush/connpool"
39+
. "github.com/uniqush/uniqush-push/push"
3940
)
4041

4142
const (
@@ -161,6 +162,10 @@ func (p *apnsPushService) BuildDeliveryPointFromMap(kv map[string]string, dp *De
161162
return errors.New("NoSubscriber")
162163
}
163164
if devtoken, ok := kv["devtoken"]; ok && len(devtoken) > 0 {
165+
_, err := hex.DecodeString(devtoken)
166+
if err != nil {
167+
return fmt.Errorf("Invalid delivery point: bad device token. %v", err)
168+
}
164169
dp.FixedData["devtoken"] = devtoken
165170
} else {
166171
return errors.New("NoDevToken")
@@ -405,12 +410,12 @@ func newAPNSConnManager(psp *PushServiceProvider, resultChan chan *apnsResult) *
405410
InsecureSkipVerify: false,
406411
}
407412

413+
manager.addr = psp.VolatileData["addr"]
408414
if skip, ok := psp.VolatileData["skipverify"]; ok {
409415
if skip == "true" {
410416
manager.conf.InsecureSkipVerify = true
411417
}
412418
}
413-
manager.addr = psp.VolatileData["addr"]
414419
manager.resultChan = resultChan
415420
return manager
416421
}
@@ -420,16 +425,23 @@ func (self *apnsConnManager) NewConn() (net.Conn, error) {
420425
return nil, self.err
421426
}
422427

423-
conn, err := net.DialTimeout("tcp", self.addr, time.Duration(maxWaitTime)*time.Second)
424-
if err != nil {
425-
return nil, err
426-
}
428+
/*
429+
conn, err := net.DialTimeout("tcp", self.addr, time.Duration(maxWaitTime)*time.Second)
430+
if err != nil {
431+
return nil, err
432+
}
427433
428-
if c, ok := conn.(*net.TCPConn); ok {
429-
c.SetKeepAlive(true)
430-
}
431-
tlsconn := tls.Client(conn, self.conf)
432-
err = tlsconn.Handshake()
434+
if c, ok := conn.(*net.TCPConn); ok {
435+
c.SetKeepAlive(true)
436+
}
437+
tlsconn := tls.Client(conn, self.conf)
438+
err = tlsconn.Handshake()
439+
if err != nil {
440+
return nil, err
441+
}
442+
*/
443+
444+
tlsconn, err := tls.Dial("tcp", self.addr, self.conf)
433445
if err != nil {
434446
return nil, err
435447
}
@@ -491,7 +503,8 @@ func (self *apnsPushService) singlePush(payload, token []byte, expiry uint32, mi
491503
conn.Close()
492504

493505
time.Sleep(sleepTime)
494-
sleepTime *= sleepTime
506+
// randomly wait more time
507+
sleepTime += time.Duration(rand.Int63n(int64(sleepTime)))
495508
// Let's try another connection to see if we can recover this error
496509
conn, err = pool.Get()
497510

srv/gcm.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"encoding/json"
2424
"errors"
2525
"fmt"
26-
. "github.com/uniqush/uniqush-push/push"
2726
"io/ioutil"
27+
"net"
2828
"net/http"
2929
"strconv"
3030
"time"
31+
. "github.com/uniqush/uniqush-push/push"
3132
)
3233

3334
const (
@@ -211,8 +212,21 @@ func (self *gcmPushService) multicast(psp *PushServiceProvider, dpList []*Delive
211212
res.Provider = psp
212213
res.Content = notif
213214

214-
res.Err = e2
215215
res.Destination = dp
216+
if err, ok := e2.(net.Error); ok {
217+
// Temporary error. Try to recover
218+
if err.Temporary() {
219+
after := 3 * time.Second
220+
res.Err = NewRetryErrorWithReason(psp, dp, notif, after, err)
221+
}
222+
} else if err, ok := e2.(*net.DNSError); ok {
223+
// DNS error, try to recover it by retry
224+
after := 3 * time.Second
225+
res.Err = NewRetryErrorWithReason(psp, dp, notif, after, err)
226+
227+
} else {
228+
res.Err = e2
229+
}
216230
resQueue <- res
217231
}
218232
return

0 commit comments

Comments
 (0)