Skip to content

Commit

Permalink
Merge pull request #273 from ably-forks/feature/auto-assigned-port
Browse files Browse the repository at this point in the history
Allow using an automatically assigned connection port
  • Loading branch information
tylertreat authored Oct 9, 2020
2 parents 6d2e262 + 2d5d2a8 commit 1478dcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (m *metadataAPI) brokerCache(serverIDs map[string]struct{}) ([]*client.Brok
// argument is the expected number of peers to get a response from.
func (m *metadataAPI) fetchBrokerInfo(ctx context.Context, numPeers int) ([]*client.Broker, *status.Status) {
// Add ourselves.
connectionAddress := m.config.GetConnectionAddress()
connectionAddress := m.getConnectionAddress()
brokers := []*client.Broker{{
Id: m.config.Clustering.ServerID,
Host: connectionAddress.Host,
Expand Down
19 changes: 15 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,23 @@ func (s *Server) IsRunning() bool {
func (s *Server) GetListenPort() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.port
}

// getConnectionAddress returns the connection address that should be used by
// the server. It uses the port the server is currently listening to if the
// connection port is 0, so that an OS-assigned port can be used as a connection
// port.
func (s *Server) getConnectionAddress() HostPort {
s.mu.RLock()
defer s.mu.RUnlock()

if s.listener == nil {
return 0
address := s.config.GetConnectionAddress()
if address.Port == 0 {
address.Port = s.port
}

return s.listener.Addr().(*net.TCPAddr).Port
return address
}

// recoverAndPersistState recovers any existing server metadata state from disk
Expand Down Expand Up @@ -819,7 +830,7 @@ func (s *Server) handleServerInfoRequest(m *nats.Msg) {
return
}

connectionAddress := s.config.GetConnectionAddress()
connectionAddress := s.getConnectionAddress()
data, err := proto.MarshalServerInfoResponse(&proto.ServerInfoResponse{
Id: s.config.Clustering.ServerID,
Host: connectionAddress.Host,
Expand Down

0 comments on commit 1478dcb

Please sign in to comment.