Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timeout for network connection #65

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ func newClientWithConn(gw string, conn Conn) Client {
return c
}

func NewClientWithCert(gw string, cert tls.Certificate) Client {
conn := NewConnWithCert(gw, cert)

func NewClietWithCertTimeout(gw string, cert tls.Certificate, timeout int) Client {
conn := NewConnWithCertTimeout(gw, cert, timeout)
return newClientWithConn(gw, conn)
}

func NewClientWithCert(gw string, cert tls.Certificate) Client {
return NewClietWithCertTimeout(gw, cert, 0)
}

func NewClient(gw string, cert string, key string) (Client, error) {
conn, err := NewConn(gw, cert, key)
if err != nil {
Expand All @@ -63,15 +66,19 @@ func NewClient(gw string, cert string, key string) (Client, error) {
return newClientWithConn(gw, conn), nil
}

func NewClientWithFiles(gw string, certFile string, keyFile string) (Client, error) {
conn, err := NewConnWithFiles(gw, certFile, keyFile)
func NewClientWithFilesTimeout(gw string, certFile string, keyFile string, timeout int) (Client, error) {
conn, err := NewConnWithFilesTimeout(gw, certFile, keyFile, timeout)
if err != nil {
return Client{}, err
}

return newClientWithConn(gw, conn), nil
}

func NewClientWithFiles(gw string, certFile string, keyFile string) (Client, error) {
return NewClientWithFilesTimeout(gw, certFile, keyFile, 0)
}

func (c *Client) Send(n Notification) error {
c.notifs <- n
return nil
Expand Down Expand Up @@ -132,6 +139,7 @@ func (c *Client) runLoop() {
if err != nil {
// TODO Probably want to exponentially backoff...
time.Sleep(1 * time.Second)
log.Println("err connecting to apns ", err.Error())
continue
}

Expand Down Expand Up @@ -205,9 +213,11 @@ func readErrs(c *Conn) chan error {

go func() {
p := make([]byte, 6, 6)
c.NetConn.SetReadDeadline(time.Time{})
_, err := c.Read(p)
if err != nil {
errs <- err
log.Println("read err", err.Error())
return
}

Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package apns_test
import (
"bytes"
"encoding/binary"
"io/ioutil"
"os"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/timehop/apns"
"io/ioutil"
"os"
"time"
)

var _ = Describe("Client", func() {
Expand Down
33 changes: 28 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"net"
"strings"
"time"
)

const (
Expand All @@ -18,19 +19,24 @@ const (
type Conn struct {
NetConn net.Conn
Conf *tls.Config
timeout time.Duration

gateway string
connected bool
}

func NewConnWithCert(gw string, cert tls.Certificate) Conn {
func NewConnWithCertTimeout(gw string, cert tls.Certificate, timeout int) Conn {
gatewayParts := strings.Split(gw, ":")
conf := tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: gatewayParts[0],
}

return Conn{gateway: gw, Conf: &conf}
return Conn{gateway: gw, Conf: &conf, timeout: time.Duration(timeout) * time.Second}
}

func NewConnWithCert(gw string, cert tls.Certificate) Conn {
return NewConnWithCertTimeout(gw, cert, 0)
}

// NewConnWithFiles creates a new Conn from certificate and key in the specified files
Expand All @@ -44,13 +50,18 @@ func NewConn(gw string, crt string, key string) (Conn, error) {
}

// NewConnWithFiles creates a new Conn from certificate and key in the specified files
func NewConnWithFiles(gw string, certFile string, keyFile string) (Conn, error) {
func NewConnWithFilesTimeout(gw string, certFile string, keyFile string, timeout int) (Conn, error) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return Conn{}, err
}

return NewConnWithCert(gw, cert), nil
return NewConnWithCertTimeout(gw, cert, timeout), nil
}

// NewConnWithFiles creates a new Conn from certificate and key in the specified files
func NewConnWithFiles(gw string, certFile string, keyFile string) (Conn, error) {
return NewConnWithFilesTimeout(gw, certFile, keyFile, 0)
}

// Connect actually creates the TLS connection
Expand All @@ -60,12 +71,21 @@ func (c *Conn) Connect() error {
c.NetConn.Close()
}

conn, err := net.Dial("tcp", c.gateway)
var conn net.Conn
var err error
if c.timeout > 0 {
conn, err = net.DialTimeout("tcp", c.gateway, c.timeout)
} else {
conn, err = net.Dial("tcp", c.gateway)
}
if err != nil {
return err
}

tlsConn := tls.Client(conn, c.Conf)
if c.timeout > 0 {
tlsConn.SetDeadline(time.Now().Add(c.timeout * 3))
}
err = tlsConn.Handshake()
if err != nil {
return err
Expand All @@ -91,5 +111,8 @@ func (c *Conn) Read(p []byte) (int, error) {

// Write writes data from the connection
func (c *Conn) Write(p []byte) (int, error) {
if c.timeout > 0 {
c.NetConn.SetWriteDeadline(time.Now().Add(c.timeout))
}
return c.NetConn.Write(p)
}
6 changes: 3 additions & 3 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"bytes"
"crypto/tls"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/timehop/apns"
"io"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/timehop/apns"
)

var DummyCert = `-----BEGIN CERTIFICATE-----
Expand Down
6 changes: 3 additions & 3 deletions feedback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"io/ioutil"
"os"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/timehop/apns"
"io/ioutil"
"os"
"time"
)

var _ = Describe("Feedback", func() {
Expand Down