Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from mercari/keepalive-handling
Browse files Browse the repository at this point in the history
APNs, GCM: introduced http.Transport.IdleConnTimeout from Go1.7.
  • Loading branch information
kazegusuri authored Oct 26, 2016
2 parents ef6960b + 489b9e7 commit 8808ac2
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 36 deletions.
4 changes: 2 additions & 2 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The configuration for Gaurun has some sections. The example is [here](conf/gauru
|sandbox |bool |On/Off for sandbox environment |true | |
|retry_max |int |maximum retry count for push notication to APNs |1 | |
|timeout |int |timeout for push notification to APNs |5 | |
|keepalive_timeout |int |time for continuing keep-alive connection to APNs |30 | |
|keepalive_timeout |int |time for continuing keep-alive connection to APNs |90 | |
|keepalive_conns |int |number of keep-alive connection to APNs |runtime.NumCPU()| |
|topic |string|the assigned value of `apns-topic` for Request headers| | |

Expand All @@ -42,7 +42,7 @@ The configuration for Gaurun has some sections. The example is [here](conf/gauru
|enabled |bool |On/Off for push notication to GCM |true | |
|apikey |string|API key string for GCM | | |
|timeout |int |timeout for push notication to GCM |5(sec) | |
|keepalive_timeout|int |time for continuing keep-alive connection to GCM|30 | |
|keepalive_timeout|int |time for continuing keep-alive connection to GCM|90 | |
|keepalive_conns |int |number of keep-alive connection to GCM |runtime.NumCPU()||
|retry_max |int |maximum retry count for push notication to GCM |1 | |

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gaurun is production ready.

## Requirements

Gaurun requires Go1.6.3 or later.
Gaurun requires Go1.7.3 or later.

## Supported Platforms

Expand Down
3 changes: 2 additions & 1 deletion gaurun/apns_http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func NewTransportHttp2(cert tls.Certificate) (*http.Transport, error) {
MaxIdleConnsPerHost: ConfGaurun.Ios.KeepAliveConns,
Dial: (&net.Dialer{
Timeout: time.Duration(ConfGaurun.Ios.Timeout) * time.Second,
KeepAlive: time.Duration(ConfGaurun.Ios.KeepAliveTimeout) * time.Second,
KeepAlive: time.Duration(keepAliveInterval(ConfGaurun.Ios.KeepAliveTimeout)) * time.Second,
}).Dial,
IdleConnTimeout: time.Duration(ConfGaurun.Ios.KeepAliveTimeout) * time.Second,
}

if err := http2.ConfigureTransport(transport); err != nil {
Expand Down
53 changes: 53 additions & 0 deletions gaurun/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gaurun

import (
"net"
"net/http"
"time"

"github.com/mercari/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
}

func InitHttpClient() error {
TransportGCM := &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 = &gcm.Sender{
ApiKey: ConfGaurun.Android.ApiKey,
Http: &http.Client{
Transport: TransportGCM,
Timeout: time.Duration(ConfGaurun.Android.Timeout) * time.Second,
},
}

var err error
APNSClient, err = NewApnsClientHttp2(
ConfGaurun.Ios.PemCertPath,
ConfGaurun.Ios.PemKeyPath,
)
if err != nil {
return err
}
return nil
}
15 changes: 15 additions & 0 deletions gaurun/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gaurun

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestKeepAliveInterval(t *testing.T) {
assert.Equal(t, 30, keepAliveInterval(90))
assert.Equal(t, 30, keepAliveInterval(30))
assert.Equal(t, 25, keepAliveInterval(25))
assert.Equal(t, 30, keepAliveInterval(50))
assert.Equal(t, 90, keepAliveInterval(300))
assert.Equal(t, 90, keepAliveInterval(600))
}
4 changes: 2 additions & 2 deletions gaurun/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func BuildDefaultConf() ConfToml {
conf.Android.ApiKey = ""
conf.Android.Enabled = true
conf.Android.Timeout = 5
conf.Android.KeepAliveTimeout = 30
conf.Android.KeepAliveTimeout = 90
conf.Android.KeepAliveConns = numCPU
conf.Android.RetryMax = 1
// iOS
Expand All @@ -78,7 +78,7 @@ func BuildDefaultConf() ConfToml {
conf.Ios.Sandbox = true
conf.Ios.RetryMax = 1
conf.Ios.Timeout = 5
conf.Ios.KeepAliveTimeout = 30
conf.Ios.KeepAliveTimeout = 90
conf.Ios.KeepAliveConns = numCPU
conf.Ios.Topic = ""
// log
Expand Down
4 changes: 2 additions & 2 deletions gaurun/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.Enabled, true)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.ApiKey, "")
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.Timeout, 5)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.KeepAliveTimeout, 30)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.KeepAliveTimeout, 90)
assert.Equal(suite.T(), int64(suite.ConfGaurunDefault.Android.KeepAliveConns), suite.ConfGaurunDefault.Core.WorkerNum)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Android.RetryMax, 1)
// Ios
Expand All @@ -50,7 +50,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.Sandbox, true)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.RetryMax, 1)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.Timeout, 5)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.KeepAliveTimeout, 30)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.KeepAliveTimeout, 90)
assert.Equal(suite.T(), int64(suite.ConfGaurunDefault.Ios.KeepAliveConns), suite.ConfGaurunDefault.Core.WorkerNum)
assert.Equal(suite.T(), suite.ConfGaurunDefault.Ios.Topic, "")
// Log
Expand Down
28 changes: 0 additions & 28 deletions gaurun/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"sync/atomic"
"time"
Expand Down Expand Up @@ -52,33 +51,6 @@ type CertificatePem struct {
Key []byte
}

func InitHttpClient() error {
TransportGCM := &http.Transport{
MaxIdleConnsPerHost: ConfGaurun.Android.KeepAliveConns,
Dial: (&net.Dialer{
Timeout: time.Duration(ConfGaurun.Android.Timeout) * time.Second,
KeepAlive: time.Duration(ConfGaurun.Android.KeepAliveTimeout) * time.Second,
}).Dial,
}
GCMClient = &gcm.Sender{
ApiKey: ConfGaurun.Android.ApiKey,
Http: &http.Client{
Transport: TransportGCM,
Timeout: time.Duration(ConfGaurun.Android.Timeout) * time.Second,
},
}

var err error
APNSClient, err = NewApnsClientHttp2(
ConfGaurun.Ios.PemCertPath,
ConfGaurun.Ios.PemKeyPath,
)
if err != nil {
return err
}
return nil
}

func enqueueNotifications(notifications []RequestGaurunNotification) {
for _, notification := range notifications {
err := validateNotification(&notification)
Expand Down

0 comments on commit 8808ac2

Please sign in to comment.