/
messagewriter.go
61 lines (51 loc) · 1.67 KB
/
messagewriter.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package pubsub
import (
"time"
"github.com/juju/errors"
"github.com/juju/juju/api"
pubsubapi "github.com/juju/juju/api/controller/pubsub"
"github.com/juju/juju/rpc/params"
)
// MessageWriter defines the two methods called for message forwarding.
type MessageWriter interface {
// ForwardMessage forwards the given message to the server.
ForwardMessage(*params.PubSubMessage) error
Close()
}
var dialOpts = api.DialOpts{
DialAddressInterval: 20 * time.Millisecond,
// If for some reason we are getting rate limited, there is a standard
// five second delay before we get the login response. Ideally we need
// to wait long enough for this response to get back to us.
// Ideally the apiserver wouldn't be rate limiting connections from other
// API servers, see bug #1733256.
Timeout: 10 * time.Second,
RetryDelay: 1 * time.Second,
}
// NewMessageWriter will connect to the remote defined by the info,
// and return a MessageWriter.
func NewMessageWriter(info *api.Info) (MessageWriter, error) {
conn, err := api.Open(info, dialOpts)
if err != nil {
return nil, errors.Trace(err)
}
a := pubsubapi.NewAPI(conn)
writer, err := a.OpenMessageWriter()
if err != nil {
conn.Close()
return nil, errors.Trace(err)
}
return &remoteConnection{connection: conn, MessageWriter: writer}, nil
}
// remoteConnection represents an api connection to another
// API server for the purpose of forwarding pubsub messages.
type remoteConnection struct {
connection api.Connection
pubsubapi.MessageWriter
}
func (r *remoteConnection) Close() {
r.MessageWriter.Close()
r.connection.Close()
}