-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
73 lines (63 loc) · 1.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"log"
"sync"
"github.com/gin-gonic/gin"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
)
// Notification 带重传的消息通知
type Notification struct {
mu sync.Mutex
apns *apns2.Notification
times int
}
var (
daemon chan *Notification
retryQueue chan *Notification
sleepInterval = flag.Int("sleepInterval", 3, "sleep interval")
maxRetries = flag.Int("maxRetries", 3, "maximum retry time")
cert = flag.String("cert", "./cert.p12", "p12 cert path")
passwd = flag.String("passwd", "", "p12 cert password")
redisURI = flag.String("redisURI", "", "redis URI")
prod = flag.Bool("prod", false, "prod or develop env?")
maxConsumer = flag.Int("maxConsumer", 5, "how many consumer")
maxQueueSize = flag.Int("maxQueueSize", 4000, "max size in notification queue")
maxIdle = flag.Int("maxIdle", 100, "redis concurrency")
maxActive = flag.Int("maxActive", 12000, "redis maximum active connections amount")
lifetime = flag.Int("lifetime", 30, "life time of reported device token in days")
step = flag.Int("step", 1000, "step in loop")
retryAfter = flag.Int("retryAfter", 3, "retry after n seconds")
)
func main() {
// parse cmd params
flag.Parse()
// initial redis
initRedisPool()
defer redisPool.Close()
// start expiration worker
go expirationWorker()
// load cert
cert, err := certificate.FromP12File(*cert, *passwd)
if err != nil {
log.Fatal("Cert Error:", err)
}
// start consumers
daemon = make(chan *Notification, *maxQueueSize)
for i := 0; i < *maxConsumer; i++ {
go startConsume(cert)
}
// start retry queue
retryQueue = make(chan *Notification, *maxQueueSize)
// start web server
r := gin.Default()
log.Printf("running gin...")
r.POST("/push", Push)
r.POST("/report", ReportInfo)
r.POST("/tag", SetTag)
r.POST("/tag/push", PushByTag)
r.DELETE("/badge", ClearBadge)
log.Printf("start server...")
r.Run(":9999")
}