Gateways: Ignore reference to self

Allows the use of a global include for all gateways and each
gateway will ignore its own reference.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
Ivan Kozlovic
2018-11-28 14:23:15 -07:00
parent 74f789c985
commit 086b26f14a
6 changed files with 84 additions and 3 deletions

6
server/configs/gwa.conf Normal file
View File

@@ -0,0 +1,6 @@
listen: "127.0.0.1:-1"
gateway {
name: "A"
listen: "127.0.0.1:5227"
include 'gws.conf'
}

6
server/configs/gwb.conf Normal file
View File

@@ -0,0 +1,6 @@
listen: "127.0.0.1:-1"
gateway {
name: "B"
listen: "127.0.0.1:5228"
include 'gws.conf'
}

10
server/configs/gws.conf Normal file
View File

@@ -0,0 +1,10 @@
gateways [
{
name: "A"
url: "nats://127.0.0.1:5227"
}
{
name: "B"
url: "nats://127.0.0.1:5228"
}
]

View File

@@ -160,6 +160,10 @@ func newGateway(opts *Options) (*srvGateway, error) {
// Create remote gateways
for _, rgo := range opts.Gateway.Gateways {
// Ignore if there is a remote gateway with our name.
if rgo.Name == gateway.name {
continue
}
cfg := &gatewayCfg{
RemoteGatewayOpts: rgo.clone(),
urls: make(map[string]*url.URL, len(rgo.URLs)),
@@ -993,6 +997,10 @@ func (s *Server) processImplicitGateway(info *Info) {
defer s.gateway.Unlock()
// Name of the gateway to connect to is the Info.Gateway field.
gwName := info.Gateway
// If this is our name, bail.
if gwName == s.gateway.name {
return
}
// Check if we already have this config, and if so, we are done
cfg := s.gateway.remotes[gwName]
if cfg != nil {

View File

@@ -321,6 +321,59 @@ func TestGatewayBasic(t *testing.T) {
})
}
func TestGatewayIgnoreSelfReference(t *testing.T) {
o := testDefaultOptionsForGateway("A")
// To create a reference to itself before running the server
// it means that we have to assign an explicit port
o.Gateway.Port = 5222
o.gatewaysSolicitDelay = 0
u, _ := url.Parse(fmt.Sprintf("nats://%s:%d", o.Gateway.Host, o.Gateway.Port))
cfg := &RemoteGatewayOpts{
Name: "A",
URLs: []*url.URL{u},
}
o.Gateway.Gateways = append(o.Gateway.Gateways, cfg)
s := runGatewayServer(o)
defer s.Shutdown()
// Wait a bit to make sure that there is no attempt to connect.
time.Sleep(20 * time.Millisecond)
// No outbound connection expected, and no attempt to connect.
if s.getRemoteGateway("A") != nil {
t.Fatalf("Should not have a remote gateway config for A")
}
if s.getOutboundGatewayConnection("A") != nil {
t.Fatalf("Should not have a gateway connection to A")
}
s.Shutdown()
// Now try with config files and include
s1, _ := RunServerWithConfig("configs/gwa.conf")
defer s1.Shutdown()
s2, _ := RunServerWithConfig("configs/gwb.conf")
defer s2.Shutdown()
waitForOutboundGateways(t, s1, 1, 2*time.Second)
waitForOutboundGateways(t, s2, 1, 2*time.Second)
waitForInboundGateways(t, s1, 1, 2*time.Second)
waitForInboundGateways(t, s2, 1, 2*time.Second)
if s1.getRemoteGateway("A") != nil {
t.Fatalf("Should not have a remote gateway config for A")
}
if s1.getOutboundGatewayConnection("A") != nil {
t.Fatalf("Should not have a gateway connection to A")
}
if s2.getRemoteGateway("B") != nil {
t.Fatalf("Should not have a remote gateway config for B")
}
if s2.getOutboundGatewayConnection("B") != nil {
t.Fatalf("Should not have a gateway connection to B")
}
}
func TestGatewaySolicitDelay(t *testing.T) {
o2 := testDefaultOptionsForGateway("B")
s2 := runGatewayServer(o2)

View File

@@ -830,9 +830,7 @@ func parseGateways(v interface{}, errors *[]error, warnings *[]error) ([]*Remote
case "urls":
urls, errs := parseURLs(v.([]interface{}), "gateway")
if errs != nil {
for _, e := range errs {
*errors = append(*errors, e)
}
*errors = append(*errors, errs...)
continue
}
gateway.URLs = urls