Skip to content

Commit

Permalink
Allow configuration of APNS client count via startup arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 16, 2024
1 parent 1010a48 commit f271853
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions apns/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f271853

Please sign in to comment.