Explorar o código

Add option to ignore cassandras reported address

Add IgnorePeerAddr to cluster config which when enabled will use the
supplied host address instead of the one which the driver discovers from
cassandra.
Chris Bannister %!s(int64=10) %!d(string=hai) anos
pai
achega
8c70accded
Modificáronse 2 ficheiros con 18 adicións e 0 borrados
  1. 9 0
      cluster.go
  2. 9 0
      events.go

+ 9 - 0
cluster.go

@@ -126,6 +126,15 @@ type ClusterConfig struct {
 	// via Discovery
 	HostFilter HostFilter
 
+	// If IgnorePeerAddr is true and the address in system.peers does not match
+	// the supplied host by either initial hosts or discovered via events then the
+	// host will be replaced with the supplied address.
+	//
+	// For example if an event comes in with host=10.0.0.1 but when looking up that
+	// address in system.local or system.peers returns 127.0.0.1, the peer will be
+	// set to 10.0.0.1 which is what will be used to connect to.
+	IgnorePeerAddr bool
+
 	// internal config for testing
 	disableControlConn bool
 }

+ 9 - 0
events.go

@@ -167,6 +167,11 @@ func (s *Session) handleNewNode(host net.IP, port int, waitForBinary bool) {
 		hostInfo = &HostInfo{peer: host.String(), port: port, state: NodeUp}
 	}
 
+	addr := host.String()
+	if s.cfg.IgnorePeerAddr && hostInfo.Peer() != addr {
+		hostInfo.setPeer(addr)
+	}
+
 	if s.cfg.HostFilter != nil {
 		if !s.cfg.HostFilter.Accept(hostInfo) {
 			return
@@ -216,6 +221,10 @@ func (s *Session) handleNodeUp(ip net.IP, port int, waitForBinary bool) {
 	addr := ip.String()
 	host := s.ring.getHost(addr)
 	if host != nil {
+		if s.cfg.IgnorePeerAddr && host.Peer() != addr {
+			host.setPeer(addr)
+		}
+
 		if s.cfg.HostFilter != nil {
 			if !s.cfg.HostFilter.Accept(host) {
 				return