-
Notifications
You must be signed in to change notification settings - Fork 3
/
accessgrant.go
55 lines (42 loc) · 1.02 KB
/
accessgrant.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
package pubnub
import (
"errors"
"sync"
"github.com/pubnub/go/messaging"
)
var ErrInvalidType = errors.New("invalid type")
type AccessGrant struct {
pool sync.Pool
}
type AccessGrantOptions struct {
ResumeOnReconnect bool
SubscribeTimeout int
Origin string
}
func NewAccessGrantOptions() *AccessGrantOptions {
return &AccessGrantOptions{
ResumeOnReconnect: true,
SubscribeTimeout: 20,
Origin: "pubsub.pubnub.com",
}
}
func NewAccessGrant(ao *AccessGrantOptions, cs *ClientSettings) *AccessGrant {
messaging.SetResumeOnReconnect(ao.ResumeOnReconnect)
messaging.SetSubscribeTimeout(uint16(ao.SubscribeTimeout))
messaging.SetOrigin(ao.Origin)
p := sync.Pool{
New: func() interface{} {
return NewPubNubClient(cs)
},
}
return &AccessGrant{pool: p}
}
func (ag *AccessGrant) Grant(as *AuthSettings) error {
client, ok := ag.pool.Get().(*PubNubClient)
if !ok {
panic(ErrInvalidType)
}
defer ag.pool.Put(client)
client.pub.SetAuthenticationKey(as.Token)
return client.Grant(as)
}