예제 #1
0
파일: handler.go 프로젝트: micro/router-srv
func (r *Router) SelectStream(ctx context.Context, req *proto.SelectRequest, stream proto.Router_SelectStreamStream) error {
	// TODO: process filters

	if len(req.Service) == 0 {
		return errors.BadRequest("go.micro.srv.router.Router.Select", "invalid service name")
	}

	t := time.NewTicker(time.Duration(router.DefaultExpiry) * time.Second)
	defer t.Stop()

	for {
		services, err := router.Select(req.Service)
		if err != nil && err == selector.ErrNotFound {
			return errors.NotFound("go.micro.srv.router.Router.SelectStream", err.Error())
		} else if err != nil {
			return errors.InternalServerError("go.micro.srv.router.Router.SelectStream", err.Error())
		}

		if err := stream.Send(&proto.SelectResponse{
			Services: services,
			Expires:  time.Now().Unix() + int64(router.DefaultExpiry),
		}); err != nil {
			return err
		}

		<-t.C
	}

	return nil
}
예제 #2
0
파일: handler.go 프로젝트: micro/router-srv
func (r *Router) Select(ctx context.Context, req *proto.SelectRequest, rsp *proto.SelectResponse) error {
	// TODO: process filters

	if len(req.Service) == 0 {
		return errors.BadRequest("go.micro.srv.router.Router.Select", "invalid service name")
	}

	services, err := router.Select(req.Service)
	if err != nil && err == selector.ErrNotFound {
		return errors.NotFound("go.micro.srv.router.Router.Select", err.Error())
	} else if err != nil {
		return errors.InternalServerError("go.micro.srv.router.Router.Select", err.Error())
	}

	rsp.Services = services
	rsp.Expires = time.Now().Unix() + int64(router.DefaultExpiry)

	return nil
}