-
Notifications
You must be signed in to change notification settings - Fork 1
/
amqp.go
58 lines (48 loc) · 1.41 KB
/
amqp.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
package gaeamqp
import (
"crypto/tls"
"net"
"time"
"github.com/streadway/amqp"
"golang.org/x/net/context"
"google.golang.org/appengine/socket"
)
const (
defaultHeartbeat = 10 * time.Second
defaultConnectionTimeout = 30 * time.Second
)
func appEngineDial(c context.Context) func(network, addr string) (net.Conn, error) {
return func(network, addr string) (net.Conn, error) {
conn, err := socket.DialTimeout(c, network, addr, defaultConnectionTimeout)
if err != nil {
return nil, err
}
if err := conn.SetReadDeadline(time.Now().Add(defaultConnectionTimeout)); err != nil {
return nil, err
}
return conn, nil
}
}
func Dialer(c context.Context) func(url string) (*amqp.Connection, error) {
return func(url string) (*amqp.Connection, error) {
return amqp.DialConfig(url, amqp.Config{
Heartbeat: defaultHeartbeat,
Dial: appEngineDial(c),
})
}
}
func DialerTLS(c context.Context) func(url string, amqps *tls.Config) (*amqp.Connection, error) {
return func(url string, amqps *tls.Config) (*amqp.Connection, error) {
return amqp.DialConfig(url, amqp.Config{
Heartbeat: defaultHeartbeat,
Dial: appEngineDial(c),
TLSClientConfig: amqps,
})
}
}
func Dial(c context.Context, url string) (*amqp.Connection, error) {
return Dialer(c)(url)
}
func DialTLS(c context.Context, url string, amqps *tls.Config) (*amqp.Connection, error) {
return DialerTLS(c)(url, amqps)
}