Skip to content

Commit 31f5251

Browse files
committed
address comments
1 parent 5ddd599 commit 31f5251

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

discovery/main.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,10 @@ type Endpoint struct {
99
Port uint16
1010
}
1111

12-
func DiscoverEndpoints(serviceDNS string) ([]Endpoint, error) {
13-
endpoints := []Endpoint{}
12+
func DiscoverEndpoints(serviceDNS string) ([]*net.SRV, error) {
1413
_, remotes, err := net.LookupSRV("", "", serviceDNS)
15-
1614
if err != nil {
17-
return endpoints, err
18-
}
19-
20-
for _, n := range remotes {
21-
endpoints = append(endpoints, Endpoint{
22-
Name: n.Target,
23-
Port: n.Port,
24-
})
15+
return nil, err
2516
}
26-
27-
return endpoints, nil
17+
return remotes, nil
2818
}

messaging/nats.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
type NatsConfig struct {
1717
TLS *tls.Config `mapstructure:"tls_conf"`
18-
DiscoveryName string `mapstructure:"discovery_name"`
18+
DiscoveryName string `split_words:"true" mapstructure:"discovery_name"`
1919
Servers []string `mapstructure:"servers"`
2020
LogsSubject string `mapstructure:"log_subject"`
2121
}
@@ -66,14 +66,12 @@ func ConfigureNatsConnection(config *NatsConfig, log *logrus.Entry) (*nats.Conn,
6666

6767
// ConnectToNats will do a TLS connection to the nats servers specified
6868
func ConnectToNats(config *NatsConfig, errHandler nats.ErrHandler) (*nats.Conn, error) {
69-
var err error
70-
serversString := config.ServerString()
71-
7269
if config.DiscoveryName != "" {
73-
serversString, err = buildNatsServersString(config.DiscoveryName)
70+
servers, err := discoverNatsURLs(config.DiscoveryName)
7471
if err != nil {
7572
return nil, err
7673
}
74+
config.Servers = servers
7775
}
7876

7977
options := []nats.Option{}
@@ -91,7 +89,7 @@ func ConnectToNats(config *NatsConfig, errHandler nats.ErrHandler) (*nats.Conn,
9189
options = append(options, nats.ErrorHandler(errHandler))
9290
}
9391

94-
return nats.Connect(serversString, options...)
92+
return nats.Connect(config.ServerString(), options...)
9593
}
9694

9795
func ErrorHandler(log *logrus.Entry) nats.ErrHandler {
@@ -105,17 +103,17 @@ func ErrorHandler(log *logrus.Entry) nats.ErrHandler {
105103
}
106104
}
107105

108-
func buildNatsServersString(serviceName string) (string, error) {
109-
natsUrls := []string{}
106+
func discoverNatsURLs(serviceName string) ([]string, error) {
107+
natsURLs := []string{}
110108

111109
endpoints, err := discovery.DiscoverEndpoints(serviceName)
112110
if err != nil {
113-
return "", err
111+
return nil, err
114112
}
115113

116114
for _, endpoint := range endpoints {
117-
natsUrls = append(natsUrls, fmt.Sprintf("nats://%s:%d", endpoint.Name, endpoint.Port))
115+
natsURLs = append(natsURLs, fmt.Sprintf("nats://%s:%d", endpoint.Target, endpoint.Port))
118116
}
119117

120-
return strings.Join(natsUrls, ","), nil
118+
return natsURLs, nil
121119
}

messaging/rabbit.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *Consumer) Clone(queueName string, delivery *DeliveryDefinition) (*Consu
4242

4343
type RabbitConfig struct {
4444
Servers []string `mapstructure:"servers"`
45-
DiscoveryName string `mapstructure:"discovery_name"`
45+
DiscoveryName string `split_words:"true" mapstructure:"discovery_name"`
4646
TLS *tls.Config `mapstructure:"tls_conf"`
4747

4848
ExchangeDefinition ExchangeDefinition `envconfig:"exchange" mapstructure:"exchange"`
@@ -206,20 +206,19 @@ func ValidateRabbitConfigStruct(servers []string, exchange ExchangeDefinition, q
206206

207207
// ConnectToRabbit will open a TLS connection to rabbit mq
208208
func ConnectToRabbit(config *RabbitConfig, log *logrus.Entry) (*Consumer, error) {
209-
var err error
210-
if err = ValidateRabbitConfig(config); err != nil {
209+
if err := ValidateRabbitConfig(config); err != nil {
211210
return nil, err
212211
}
213212

214-
servers := config.Servers
215213
if config.DiscoveryName != "" {
216-
servers, err = discoverRabbitServers(config.DiscoveryName)
214+
servers, err := discoverRabbitServers(config.DiscoveryName)
217215
if err != nil {
218216
return nil, err
219217
}
218+
config.Servers = servers
220219
}
221220

222-
conn, err := DialToRabbit(servers, config.TLS, log)
221+
conn, err := DialToRabbit(config.Servers, config.TLS, log)
223222
if err != nil {
224223
return nil, err
225224
}
@@ -414,7 +413,7 @@ func discoverRabbitServers(serviceName string) ([]string, error) {
414413
}
415414

416415
for _, endpoint := range endpoints {
417-
rabbitUrls = append(rabbitUrls, fmt.Sprintf("%s:%d", endpoint.Name, endpoint.Port))
416+
rabbitUrls = append(rabbitUrls, fmt.Sprintf("%s:%d", endpoint.Target, endpoint.Port))
418417
}
419418

420419
return rabbitUrls, nil

0 commit comments

Comments
 (0)