Skip to content

Commit f271853

Browse files
committed
Allow configuration of APNS client count via startup arguments
1 parent 1010a48 commit f271853

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

apns/apns.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ const (
3434
PayloadMaximum = 4096
3535
)
3636

37-
var maxClientCount = 50
38-
var clients = make(chan *apns2.Client, maxClientCount)
37+
var clients = make(chan *apns2.Client, 1)
3938

39+
// 初始化 APNS 客户端池
4040
func init() {
41+
ReCreateAPNS(1)
42+
}
43+
44+
func ReCreateAPNS(maxClientCount int) error {
45+
if maxClientCount < 1 {
46+
return fmt.Errorf("invalid number of clients")
47+
}
48+
4149
authKey, err := token.AuthKeyFromBytes([]byte(apnsPrivateKey))
4250
if err != nil {
4351
logger.Fatalf("failed to create APNS auth key: %v", err)
@@ -57,6 +65,8 @@ func init() {
5765
rootCAs.AppendCertsFromPEM([]byte(ca))
5866
}
5967

68+
clients = make(chan *apns2.Client, maxClientCount)
69+
6070
for i := 0; i < min(runtime.NumCPU(), maxClientCount); i++ {
6171
client := &apns2.Client{
6272
Token: &token.Token{
@@ -75,10 +85,12 @@ func init() {
7585
},
7686
Host: apns2.HostProduction,
7787
}
88+
logger.Infof("create apns client: %d", i)
7889
clients <- client
7990
}
8091

8192
logger.Info("init apns client success...")
93+
return nil
8294
}
8395

8496
func Push(msg *PushMessage) error {

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"syscall"
99
"time"
1010

11+
"github.com/finb/bark-server/v2/apns"
1112
"github.com/finb/bark-server/v2/database"
1213

1314
jsoniter "github.com/json-iterator/go"
@@ -110,6 +111,13 @@ func main() {
110111
EnvVars: []string{"BARK_SERVER_PROXY_HEADER"},
111112
Value: "",
112113
},
114+
&cli.IntFlag{
115+
Name: "max-apns-client-count",
116+
Usage: "Maximum number of APNs client connections",
117+
EnvVars: []string{"BARK_SERVER_MAX_APNS_CLIENT_COUNT"},
118+
Value: 1,
119+
Action: func(ctx *cli.Context, v int) error { return apns.ReCreateAPNS(v) },
120+
},
113121
&cli.IntFlag{
114122
Name: "concurrency",
115123
Usage: "Maximum number of concurrent connections",

0 commit comments

Comments
 (0)