diff --git a/apns/apns.go b/apns/apns.go index 1f806ecf..b900802c 100644 --- a/apns/apns.go +++ b/apns/apns.go @@ -34,10 +34,18 @@ const ( PayloadMaximum = 4096 ) -var maxClientCount = 50 -var clients = make(chan *apns2.Client, maxClientCount) +var clients = make(chan *apns2.Client, 1) +// 初始化 APNS 客户端池 func init() { + ReCreateAPNS(1) +} + +func ReCreateAPNS(maxClientCount int) error { + if maxClientCount < 1 { + return fmt.Errorf("invalid number of clients") + } + authKey, err := token.AuthKeyFromBytes([]byte(apnsPrivateKey)) if err != nil { logger.Fatalf("failed to create APNS auth key: %v", err) @@ -57,6 +65,8 @@ func init() { rootCAs.AppendCertsFromPEM([]byte(ca)) } + clients = make(chan *apns2.Client, maxClientCount) + for i := 0; i < min(runtime.NumCPU(), maxClientCount); i++ { client := &apns2.Client{ Token: &token.Token{ @@ -75,10 +85,12 @@ func init() { }, Host: apns2.HostProduction, } + logger.Infof("create apns client: %d", i) clients <- client } logger.Info("init apns client success...") + return nil } func Push(msg *PushMessage) error { diff --git a/main.go b/main.go index c8abbaf5..229efd59 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "syscall" "time" + "github.com/finb/bark-server/v2/apns" "github.com/finb/bark-server/v2/database" jsoniter "github.com/json-iterator/go" @@ -110,6 +111,13 @@ func main() { EnvVars: []string{"BARK_SERVER_PROXY_HEADER"}, Value: "", }, + &cli.IntFlag{ + Name: "max-apns-client-count", + Usage: "Maximum number of APNs client connections", + EnvVars: []string{"BARK_SERVER_MAX_APNS_CLIENT_COUNT"}, + Value: 1, + Action: func(ctx *cli.Context, v int) error { return apns.ReCreateAPNS(v) }, + }, &cli.IntFlag{ Name: "concurrency", Usage: "Maximum number of concurrent connections",