Ejemplo n.º 1
0
func (l *Listener) send(serviceName string) error {
	service, err := l.backend.GetServiceDetails(serviceName)
	if err != nil {
		return err
	}
	update := model.ServiceUpdate{
		ServiceKey:  model.MakeServiceKey("tcp", net.ParseIP(service.Address), service.Port),
		ServiceInfo: &model.ServiceInfo{Protocol: service.Protocol},
	}
	l.backend.ForeachInstance(serviceName, func(name string, instance data.Instance) {
		update.ServiceInfo.Instances = append(update.ServiceInfo.Instances, model.MakeInstance(net.ParseIP(instance.Address), instance.Port))
	})
	log.Printf("Sending update for %s: %+v\n", update.ServiceKey.String(), update.ServiceInfo)
	l.updates <- update
	return nil
}
Ejemplo n.º 2
0
func (srv *Server) doRequest(conn *net.UnixConn) error {
	var buf bytes.Buffer
	_, err := io.Copy(&buf, conn)
	if err != nil {
		return err
	}

	// XXX support service deletion commands

	parts := strings.Split(strings.TrimSpace(buf.String()), " ")
	if len(parts) <= 0 {
		return fmt.Errorf("service specification should begin with port:ip-address")
	}

	addr, err := net.ResolveTCPAddr("tcp", parts[0])
	if err != nil {
		return err
	}

	var insts []model.Instance
	for _, inst := range parts[2:] {
		addr, err := net.ResolveTCPAddr("tcp", inst)
		if err != nil {
			return err
		}

		insts = append(insts, model.MakeInstance(addr.IP, addr.Port))
	}

	var update model.ServiceUpdate
	update.ServiceKey = model.MakeServiceKey("tcp", addr.IP, addr.Port)
	update.ServiceInfo = &model.ServiceInfo{
		Protocol:  parts[1],
		Instances: insts,
	}

	select {
	case srv.updates <- update:
	case <-srv.closed:
	}

	return nil
}