示例#1
0
// The Master is the thing which allows slaves to connect and get updates
func runMaster(httpAddr string, httpUpdateAddr string) {

	var sr *ServiceRegistry.ServiceRegistry

	// The Persistor is the thing which saves any updates to Redis
	// Also the initial ServiceRegistry is loaded from it
	p := persistor.NewPersistor()
	sru1 := p.Listen()

	sr = p.LoadServiceRegistry("FirstRegistry")

	rep := replication.NewMaster()
	sru2 := rep.StartListener()

	h := http.NewBalancer()
	finished1, requestUpdate, sru3 := h.RunHTTP(httpAddr)

	u := update.NewUpdater()
	finished2 := u.RunHTTP(httpUpdateAddr, sr)

	sr.RegisterUpdateChannel(sru1)
	sr.RegisterUpdateChannel(sru2)
	sr.RegisterUpdateChannel(sru3)

	for {
		select {
		case <-finished1:
			return
		case <-finished2:
			return
		case <-requestUpdate:
			log.Println("[Main] Someone has requested an update")
			sr.SendRegistryUpdate()
		case <-time.After(30 * time.Second):
			sru1 <- *sr
		}
	}

}