From 0b953e5d69564d1239f482ef5095e84bd9e9c09a Mon Sep 17 00:00:00 2001 From: Scott Knight <4534275+knightsc@users.noreply.github.com> Date: Fri, 22 Jun 2018 17:40:12 -0400 Subject: [PATCH] Create a custom http.Client for apns communication (#446) In order to set custom transport options on the http.Client used by the push service we need to create our own instance. This allows us to configure the IdleConnTimeout and prevent connections from being killed and pushes not being sent. 90 seconds is used here since it's the same as the DefaultTransport provided by Go. --- platform/apns/service.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/platform/apns/service.go b/platform/apns/service.go index afa95feb..51347913 100644 --- a/platform/apns/service.go +++ b/platform/apns/service.go @@ -5,10 +5,13 @@ import ( "crypto/tls" "fmt" "log" + "net/http" "sync" + "time" "github.com/RobotsAndPencils/buford/push" "github.com/pkg/errors" + "golang.org/x/net/http2" "github.com/micromdm/micromdm/platform/config" "github.com/micromdm/micromdm/platform/pubsub" @@ -121,13 +124,33 @@ func updateClient(svc *PushService, sub pubsub.Subscriber) error { return nil } +func newClient(cert tls.Certificate) (*http.Client, error) { + config := &tls.Config{ + Certificates: []tls.Certificate{cert}, + } + config.BuildNameToCertificate() + transport := &http.Transport{ + TLSClientConfig: config, + IdleConnTimeout: 90 * time.Second, + } + + if err := http2.ConfigureTransport(transport); err != nil { + return nil, err + } + + return &http.Client{ + Transport: transport, + Timeout: 20 * time.Second, + }, nil +} + func NewPushService(provider PushCertificateProvider) (*push.Service, error) { cert, err := provider.PushCertificate() if err != nil { return nil, errors.Wrap(err, "get push certificate from store") } - client, err := push.NewClient(*cert) + client, err := newClient(*cert) if err != nil { return nil, errors.Wrap(err, "create push service client") }