// connect to all services func (p *service_pool) connect_all(directory string) { client := p.client_pool.Get().(*etcd.Client) defer func() { p.client_pool.Put(client) }() // get the keys under directory log.Info("connecting services under:", directory) resp, err := client.Get(directory, true, true) if err != nil { log.Error(err) return } // validation check if !resp.Node.Dir { log.Error("not a directory") return } for _, node := range resp.Node.Nodes { if node.Dir { // service directory for _, service := range node.Nodes { p.add_service(service.Key, service.Value) } } else { log.Warning("malformed service directory:", node.Key) } } log.Info("services add complete") }
func main() { defer utils.PrintPanicStack() go func() { log.Info(http.ListenAndServe("0.0.0.0:6060", nil)) }() log.SetPrefix(SERVICE) // server startup procedure startup() tcpAddr, err := net.ResolveTCPAddr("tcp4", _port) checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) log.Info("listening on:", listener.Addr()) // loop accepting for { conn, err := listener.AcceptTCP() if err != nil { log.Warning("accept failed:", err) continue } go handleClient(conn) // check server close signal select { case <-die: listener.Close() goto FINAL default: } } FINAL: // server closed, wait forever for { <-time.After(time.Second) } }