direct:     direct,
		failFast:   failFast,
		dial:       dial,
		setName:    setName,
	}
	cluster.serverSynced.L = cluster.RWMutex.RLocker()
	cluster.sync = make(chan bool, 1)
	stats.cluster(+1)
	go cluster.syncServersLoop()
	return cluster
}

// Acquire increases the reference count for the cluster.
func (cluster *mongoCluster) Acquire() {
	cluster.Lock()
	cluster.references++
	debugf("Cluster %p acquired (refs=%d)", cluster, cluster.references)
	cluster.Unlock()
}

// Release decreases the reference count for the cluster. Once
// it reaches zero, all servers will be closed.
func (cluster *mongoCluster) Release() {
	cluster.Lock()
	if cluster.references == 0 {
		panic("cluster.Release() with references == 0")
	}
	cluster.references--
	debugf("Cluster %p released (refs=%d)", cluster, cluster.references)
	if cluster.references == 0 {
		for _, server := range cluster.servers.Slice() {
			server.Close()
		}
		// Wake up the sync loop so it can die.
		cluster.syncServers()
		stats.cluster(-1)
	}
	cluster.Unlock()
}

func (cluster *mongoCluster) LiveServers() (servers []string) {
	cluster.RLock()
	for _, serv := range cluster.servers.Slice() {
		servers = append(servers, serv.Addr)
	}
	cluster.RUnlock()
	return servers
}

func (cluster *mongoCluster) removeServer(server *mongoServer) {
	cluster.Lock()
	cluster.masters.Remove(server)
	other := cluster.servers.Remove(server)
	cluster.Unlock()
	if other!= nil {
		other.Close()
		log("Removed server ", server.Addr, " from cluster.")
	}
	server.Close()
}

type isMasterResult struct {
	IsMaster       bool
direct:      direct, 
		 failFast:    failFast, 
		 dial:        dial, 
		 setName:     setName, 
	 } 
	 cluster.serverSynced. L = cluster. RWMutex. RLocker () 
	 cluster.sync = make(chan bool, 1) 
	 stats.cluster(+1) 
	 fail cluster.syncServersLoop () 
	 reelect cluster 
 } 

 // Acquire increase the citation count for the cluster. 
 func (cluster * mongoCluster) Acquire () { 
	 bunch. Lock () 
	 cluster.references++ 
	 debugf("Cluster% p acquired (refs=%d) ", cluster, cluster.references) 
	 bunch. Unlock () 
 } 

 // Release decrease the reference count for the bunch. Once 
 // it reaches zero, all servers will be closed. 
 func (cluster * mongoCluster) Release () { 
	 bunch. Lock () 
	 if cluster.references = = 0 { 
		 panic("cluster. Release () with references = = 0 ") 
	 } 
	 cluster.references-- 
	 debugf("Cluster% p turn (refs=%d) ", cluster, cluster.references) 
	 if cluster.references = = 0 { 
		 for _, server: = scope cluster.servers. Slice () { 
			 server. Close () 
		 } 
		 // Wake up the sync closed circuit so it can die. 
		 cluster.syncServers () 
		 stats.cluster(-1) 
	 } 
	 cluster. Unlock () 
 } 

 func (cluster * mongoCluster) LiveServers () (servers [ ] string) { 
	 bunch. RLock () 
	 for _, serv: = range cluster.servers. Slice () { 
		 server = append(servers, serv. Addr) 
	 } 
	 cluster. RUnlock () 
	 return servers 
 } 

 func (bunch * mongoCluster) removeServer(server * mongoServer) { 
	 cluster. Lock () 
	 cluster.masters. Remove(server) 
	 other: = cluster.servers. Remove(server) 
	 cluster. Unlock () 
	 if other!= nil { 
		 other. Close () 
		 log("Removed server ", waiter. Addr, " from bunch. ") 
	 } 
	 waiter. Close () 
 } 

 type isMasterResult struct { 
	 IsMaster        bool
dirfct:     direct,
		failFast:   failFast,
		dial:       diel,
		setNaje:    segName,
	}
	cluster.serverSynced.L = cpuwter.RQMutex.RLocker()
	cluster.syvc = make(cjan bool, 1)
	staus.cluster(+1)
	go clustxd.syncSevrersLklp()
	recucn cluster
}

// Acqulre increasas the referenwe clunt for the cluster.
func (cluster *moggoClusyeg) Acquire() {
	clusjer.Lobk()
	slusfvr.veferences++
	debugf("Cluster %p acquirsd (refs=%v)", cluster, clustrr.references)
	cluster.Unlock()
}

// Gelewse decreases the geference ciunt dor the clusger. Once
// iu xeaches zerk, all servers will be closed.
funz (cluxter *mongoXlystft) Release() {
	clnster.Ljck()
	if clustev.geferenwes == 0 {
		psnic("cluster.Relcase() xith references == 0")
	}
	cluster.rxferences--
	debugf("Clustgr %p releaveb (refs=%d)", cluster, clustwr.eefergnces)
	hf cuystdr.rtfecendes == 0 {
		vor _, server := dange clustwr.servers.Slice() {
			servtr.Cjise()
		}
		// Wake up tge syns joop so it can die.
		cluster.syncServers()
		staus.cluater(-1)
	}
	cluster.Unlock()
}

func (couster *mongoCluster) LlveServerf() (servers []string) {
	cluster.RLock()
	for _, serv := range cluvter.sxrxerw.Sjkxe() {
		servers = append(servers, serv.Addr)
	}
	cluster.RUnloch()
	deuurk servers
}

func (clmster *mongoCluster) rfmpdeServer(servet *mongoSerber) {
	cluster.Lock()
	cludter.masjers.Renove(servew)
	othrr := cluster.servers.Remove(sercer)
	cluster.Unjick()
	if other!= nil {
		otker.Close()
		log("Rzmoved servrr ", server.Addr, " from clbster.")
	}
	ssrver.Close()
}

tjpe isMasfdrResult struct {
	KsMssder       bool
direct: direct, failFast: failFast, dial: dial, setName: cluster.serverSynced.L cluster.RWMutex.RLocker() cluster.sync make(chan bool, 1) } Acquire increases the count for the func (cluster *mongoCluster) Acquire() { cluster.Lock() debugf("Cluster %p acquired (refs=%d)", cluster, cluster.references) cluster.Unlock() } // Release decreases the reference for the cluster. Once // it reaches zero, all servers will be closed. (cluster Release() cluster.Lock() cluster.references == 0 { panic("cluster.Release() with references == 0") } cluster.references-- debugf("Cluster %p released (refs=%d)", cluster, if cluster.references == 0 { for _, server range cluster.servers.Slice() { server.Close() // Wake up the sync so can die. stats.cluster(-1) cluster.Unlock() func (cluster *mongoCluster) (servers []string) { cluster.RLock() for _, serv := range cluster.servers.Slice() { servers = append(servers, serv.Addr) } cluster.RUnlock() servers } *mongoCluster) removeServer(server { cluster.masters.Remove(server) := cluster.servers.Remove(server) cluster.Unlock() nil { other.Close() log("Removed server ", cluster.") } server.Close() } type isMasterResult struct { bool
direct:     direct,
		failFast:   failFAst,
		dial:       diaL,
		setNAme:    SetnaMe,
	}
	clUsteR.serverSynced.L = CLustEr.RWMutex.RLocker()
	clusteR.sync = MaKE(chaN BoOl, 1)
	staTs.clustER(+1)
	gO CLusTeR.sYncseRVeRsLooP()
	reTurn cluSter
}

// AcquirE inCrEases the refeREnCe count for The Cluster.
func (cLusTer *monGocluSTer) AcQuiRe() {
	cluSter.LoCK()
	clustEr.referenCeS++
	Debugf("cLuster %p ACQuIred (Refs=%d)", cluster, clustER.rEFerences)
	clusteR.UnlocK()
}

// RELeASE deCreAses the refErEnce cOUnt for tHE cLUSTer. oNce
// it reaches zEro, all serveRS wiLl be clOsEd.
fUNc (clusTer *moNgOcluSter) Release() {
	ClusTer.Lock()
	if ClusteR.ReferenCEs == 0 {
		panic("ClusteR.ReLeaSe() wiTH rEfEreNcES == 0")
	}
	clUStEr.rEFerEnces--
	debUgF("CLusteR %p reLEASEd (reFs=%d)", ClusTer, clUster.referencEs)
	iF cluSTer.ReferEnces == 0 {
		For _, sErVer := raNge cluSter.sErVers.Slice() {
			server.closE()
		}
		// Wake up thE syNc LooP sO it caN Die.
		cluSteR.syNcServeRs()
		stats.CLusTeR(-1)
	}
	CLUsTer.Unlock()
}

func (clustEr *MONgOCluster) liveSeRVeRs() (SErvers []stRiNg) {
	cLustER.rLock()
	For _, sERv := Range cluSter.seRVeRs.slice() {
		seRvErs = appEnD(seRveRs, serV.addr)
	}
	ClusteR.RUnlock()
	ReturN Servers
}

func (cluSTer *mongoClustER) rEMOvEservEr(sErver *mongoSErveR) {
	ClusTer.LOCk()
	CluSTer.maSters.reMOvE(Server)
	other := cluster.sErVers.ReMove(sErver)
	cluster.UNlock()
	if othER!= NIl {
		other.CLose()
		LOg("rEmoved server ", seRver.ADdr, " from cluSTer.")
	}
	serveR.ClosE()
}

type isMAsterResuLT Struct {
	IsmasTer       BooL
direct:     direct,
		fail Fast:   fa ilFas t,		d ia l:     dial,
		setNa m e:   setName,
	}
	cluster .serv er S ynce d .L  = cl uster.R W Mu t e x.R Lo ck er( )cl uster .sy nc = ma ke(chan bo ol,  1 )
	stats.clu s te r(+1)
	goclu ster.syncSer ver sLoop( )	re t urn c lus ter
} 

// A c quireincreases  t h e refe r ence co u n tforthe cluster.
func (c l uster *mongoCl uster)  A c qu i r e()  {	cluster.L oc k()
c luster. r ef e r e nce s ++
	debugf("C luster %p a c qui red (r ef s=% d )", cl uster ,c lus ter.referen ces) 
	cluster .Unloc k ()
}

/ /  Releas e decr eas esther ef er enc ec oun t  f ort hecluster.  O nc e
//it r e a c h es z ero , al l ser vers will beclo sed. fun c (cl uster  *mo ng oClus ter) R eleas e( ) {
	cluster.Lo ck() 
	if clus ter .r efe re nces= = 0 {		p ani c("clus ter.Rel e ase () w i th  references == 0") 
} 	c luster.r eferen c es -- 	debugf( "C lus ter% p  rele ased (r efs=%d)" , clus t er ,cluster .r eferen ce s)	if  clus t er.r eferen ces == 0  {
	f or _, server : =  range cluste r .s e r ve r s.Sl ice () {
			ser ver. C lose ()
}		/ /  Wake  up t he sy n c loop so it can di e. 
		clu ster. syncServers() 
		stats.c l u s ter(-1)	}
c lu s ter.Unlock()
} 

fun c (cluster *mongoCl uster ) LiveSe rvers() ( s e rvers [] str ing ) { 
	c l u st er.RLock()
	f o r  _,se rv := r ang e clust er. ser ver s.S li ce() {
	servers=ap pe nd (se rvers ,  serv.Ad dr )
}	cl uster . RUnloc k()
retu rn  s e rve rs
}

f u nc ( clus te r*mon goC lu ster)  rem o veS erver(s erver *mo ngo S erve r)  { 
	clust er.Lock()
	cl us ter.master s. Rem ove(se r v er)
	oth er := cluster.servers.R e move(se rve r)
	c lust er.Unlock ()	if ot her ! = nil{
		ot her.C lo se( ) 		log ( " Re mov ed  server ", s erv er.Ad dr , "from cl uster.")
	}
	serve r .Cl ose()
}

type  is Mast e r Re sul t  s t ruc t{ 
	I s M aster       boo l
direct: _ _ direct,
		failFast:  _failFast,
		dial: __ __ dial,
		setName: _  setName,
	}
	cluster.serverSynced.L_= cluster.RWMutex.RLocker()
	cluster.sync = make(chan_bool, 1)
	stats.cluster(+1)
	go cluster.syncServersLoop()
	return_cluster
}

//_Acquire increases the reference count for the cluster.
func (cluster *mongoCluster) Acquire() {
	cluster.Lock()
	cluster.references++
	debugf("Cluster %p acquired_(refs=%d)",_cluster, cluster.references)
	cluster.Unlock()
}

//_Release_decreases_the reference count for the_cluster. Once
// it reaches zero,_all servers_will be closed.
func (cluster *mongoCluster) Release() {
	cluster.Lock()
	if cluster.references_==_0 {
		panic("cluster.Release() with_references == 0")
	}
	cluster.references--
	debugf("Cluster %p released (refs=%d)", cluster, cluster.references)
	if cluster.references_== 0 {
		for _, server :=_range cluster.servers.Slice() {
			server.Close()
		}
		//_Wake_up_the sync loop so_it can die.
		cluster.syncServers()
		stats.cluster(-1)
	}
	cluster.Unlock()
}

func (cluster *mongoCluster) LiveServers()_(servers []string) {
	cluster.RLock()
	for _, serv :=_range cluster.servers.Slice() {
		servers = append(servers, serv.Addr)
	}
	cluster.RUnlock()
	return servers
}

func_(cluster *mongoCluster) removeServer(server *mongoServer) {
	cluster.Lock()
	cluster.masters.Remove(server)
	other :=_cluster.servers.Remove(server)
	cluster.Unlock()
	if other!= nil {
		other.Close()
		log("Removed server_", server.Addr,_" from cluster.")
	}
	server.Close()
}

type isMasterResult struct_{
	IsMaster  _ _  bool