Пример #1
0
func NewTopicPartitionConsumer(host string, topic string, partition int, offset Offset, continuous bool) (*TopicPartitionConsumer, error) {
	connection, err := grpc.Dial(host, grpc.WithInsecure())
	if err != nil {
		return nil, err
	}

	client := api.NewEdgyClient(connection)

	if _, err := client.Ping(context.Background(), &api.PingRequest{}); err != nil {
		connection.Close()
		return nil, err
	}

	consumer := &TopicPartitionConsumer{
		host:       host,
		continuous: continuous,
		offset:     offset,
		logger:     tidy.GetLogger(),
		client:     client,
		topic:      topic,
		partition:  partition,
		messages:   make(chan IncomingBatch),
	}

	go consumer.doReading()
	//go consumer.doDispatching()

	return consumer, nil
}
Пример #2
0
func (this ClusterBuilder) Build() (Cluster, error) {
	cluster := Cluster{
		nodes: make([]Node, len(this.nodes)),
	}

	if len(this.nodes) == 0 {
		return cluster, errors.New("no nodes in cluster")
	}

	nodes := make(map[int32]Node, len(this.nodes))

	for _, node := range this.nodes {
		if duplicate, ok := nodes[node.Partition]; ok {
			return cluster, fmt.Errorf("duplicate partition %v, node %v and %v", node.Partition, node.Name, duplicate.Name)
		}

		nodes[node.Partition] = node
	}

	for p := int32(0); p < int32(len(this.nodes)); p++ {
		if node, ok := nodes[p]; !ok {
			return cluster, fmt.Errorf("missing partition %v", p)
		} else {
			logger.With("host", node.IP).Debug("adding node to cluster")

			connection, err := grpc.Dial(node.IP, grpc.WithInsecure())
			if err != nil {
				// TODO: close connections
				return cluster, err
			}

			client := api.NewEdgyClient(connection)

			node.client = client
			cluster.nodes[p] = node
		}
	}

	return cluster, nil
}