Skip to content

Commit

Permalink
Merge 086b26f into 74f789c
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovic committed Nov 28, 2018
2 parents 74f789c + 086b26f commit a3f5dc5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
6 changes: 6 additions & 0 deletions server/configs/gwa.conf
@@ -0,0 +1,6 @@
listen: "127.0.0.1:-1"
gateway {
name: "A"
listen: "127.0.0.1:5227"
include 'gws.conf'
}
6 changes: 6 additions & 0 deletions server/configs/gwb.conf
@@ -0,0 +1,6 @@
listen: "127.0.0.1:-1"
gateway {
name: "B"
listen: "127.0.0.1:5228"
include 'gws.conf'
}
10 changes: 10 additions & 0 deletions server/configs/gws.conf
@@ -0,0 +1,10 @@
gateways [
{
name: "A"
url: "nats://127.0.0.1:5227"
}
{
name: "B"
url: "nats://127.0.0.1:5228"
}
]
8 changes: 8 additions & 0 deletions server/gateway.go
Expand Up @@ -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)),
Expand Down Expand Up @@ -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 {
Expand Down
53 changes: 53 additions & 0 deletions server/gateway_test.go
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions server/opts.go
Expand Up @@ -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
Expand Down

0 comments on commit a3f5dc5

Please sign in to comment.