-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_token_manager.go
58 lines (48 loc) · 1.63 KB
/
client_token_manager.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
package apns2
import (
"errors"
"github.com/riftbit/apns2/token"
)
// Possible errors when parsing a .p8 file.
var (
ErrTokenNotFound = errors.New("tokenManager: token not found in TokenManager storage")
)
// ClientTokenManager is a way to manage multiple tokens with one connection to the APNs.
type ClientTokenManager struct {
Client *Client
TokenManager *token.Manager
}
// NewClientTokenManager returns a new ClientManager for prolonged, concurrent usage
// of multiple APNs clients. ClientManager is flexible enough to work best for
// your use case. When a client is not found in the manager, Get will return
// the result of calling Factory, which can be a Client or nil.
//
// Having multiple clients per certificate in the manager is not allowed.
//
// By default, MaxSize is 64, MaxAge is 10 minutes, and Factory always returns
// a Client with default options.
func NewClientTokenManager() *ClientTokenManager {
manager := &ClientTokenManager{
Client: NewTokenClient(nil),
TokenManager: token.NewTokenManager(),
}
return manager
}
// Push ...
func (ctm *ClientTokenManager) Push(tokenKey interface{}, n *Notification) (*Response, error) {
var token *token.Token
token, ok := ctm.TokenManager.Get(tokenKey)
if !ok {
return nil, ErrTokenNotFound
}
return ctm.Client.PushWithContextAndToken(nil, token, n)
}
// PushWithContext ...
func (ctm *ClientTokenManager) PushWithContext(ctx Context, tokenKey interface{}, n *Notification) (*Response, error) {
var token *token.Token
token, ok := ctm.TokenManager.Get(tokenKey)
if !ok {
return nil, ErrTokenNotFound
}
return ctm.Client.PushWithContextAndToken(ctx, token, n)
}