-
Notifications
You must be signed in to change notification settings - Fork 8
/
local-dialer.go
47 lines (38 loc) · 1 KB
/
local-dialer.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
package nats
import (
"net"
nats_client "github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/pkg/errors"
)
const localNatsAddress = "local:4222"
// localNatsDialer dials the local nats server in-process.
type localNatsDialer struct {
n *Nats
kp nkeys.KeyPair
}
// newLocalNatsDialer constructs a new localNatsDialer.
func newLocalNatsDialer(n *Nats, keyPair nkeys.KeyPair) *localNatsDialer {
return &localNatsDialer{n: n, kp: keyPair}
}
// Dial dials the local server.
func (d *localNatsDialer) Dial(network, address string) (net.Conn, error) {
if address != localNatsAddress {
return nil, errors.Errorf("unexpected address for local nats dialer: %s", address)
}
var pubKey string
var err error
if d.kp != nil {
pubKey, err = d.kp.PublicKey()
if err != nil {
return nil, err
}
}
p1, p2 := net.Pipe()
go func() {
_ = d.n.natsServer.HandleClientConnection(p2, pubKey)
}()
return p1, nil
}
// _ is a type assertion
var _ nats_client.CustomDialer = ((*localNatsDialer)(nil))