forked from mercari/gaurun
/
client.go
69 lines (60 loc) · 1.5 KB
/
client.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
59
60
61
62
63
64
65
66
67
68
69
package gaurun
import (
"net"
"net/http"
"time"
"github.com/mercari/gaurun/gcm"
)
func keepAliveInterval(keepAliveTimeout int) int {
const minInterval = 30
const maxInterval = 90
if keepAliveTimeout <= minInterval {
return keepAliveTimeout
}
result := keepAliveTimeout / 3
if result < minInterval {
return minInterval
}
if result > maxInterval {
return maxInterval
}
return result
}
// InitGCMClient initializes GCMClient which is globally declared.
func InitGCMClient() error {
// By default, use GCM endpoint. If UseFCM is explicitly enabled via configuration,
// use FCM endpoint.
url := gcm.GCMSendEndpoint
if ConfGaurun.Android.UseFCM {
url = gcm.FCMSendEndpoint
}
var err error
GCMClient, err = gcm.NewClient(url, ConfGaurun.Android.ApiKey)
if err != nil {
return err
}
transport := &http.Transport{
MaxIdleConnsPerHost: ConfGaurun.Android.KeepAliveConns,
Dial: (&net.Dialer{
Timeout: time.Duration(ConfGaurun.Android.Timeout) * time.Second,
KeepAlive: time.Duration(keepAliveInterval(ConfGaurun.Android.KeepAliveTimeout)) * time.Second,
}).Dial,
IdleConnTimeout: time.Duration(ConfGaurun.Android.KeepAliveTimeout) * time.Second,
}
GCMClient.Http = &http.Client{
Transport: transport,
Timeout: time.Duration(ConfGaurun.Android.Timeout) * time.Second,
}
return nil
}
func InitAPNSClient() error {
var err error
APNSClient, err = NewApnsClientHttp2(
ConfGaurun.Ios.PemCertPath,
ConfGaurun.Ios.PemKeyPath,
)
if err != nil {
return err
}
return nil
}