func (nc *NetworkClient) send(topic string, partition int32, batch []*ProducerRecord) { leader, err := nc.connector.GetLeader(topic, partition) if err != nil { for _, record := range batch { record.metadataChan <- &RecordMetadata{Record: record, Error: err} } } request := new(siesta.ProduceRequest) request.RequiredAcks = int16(nc.requiredAcks) request.AckTimeoutMs = nc.ackTimeoutMs for _, record := range batch { request.AddMessage(record.Topic, record.Partition, &siesta.Message{Key: record.encodedKey, Value: record.encodedValue}) } responseChan := nc.selector.Send(leader, request) if nc.requiredAcks > 0 { go listenForResponse(topic, partition, batch, responseChan) } else { // acks = 0 case, just complete all requests for _, record := range batch { record.metadataChan <- &RecordMetadata{ Record: record, Offset: -1, Topic: topic, Partition: partition, Error: siesta.ErrNoError, } } } }
func TestSelector(t *testing.T) { selectorConfig := DefaultSelectorConfig() selector := NewSelector(selectorConfig) link := siesta.NewBrokerLink(&siesta.Broker{ID: 1, Host: "localhost", Port: 9092}, true, 1*time.Minute, 5) request1 := new(siesta.ProduceRequest) request1.RequiredAcks = 1 request1.AckTimeoutMs = 2000 request1.AddMessage("siesta", 0, &siesta.Message{MagicByte: 0, Value: []byte("hello world")}) request2 := new(siesta.ProduceRequest) request2.RequiredAcks = 1 request2.AckTimeoutMs = 2000 request2.AddMessage("siesta", 0, &siesta.Message{MagicByte: 0, Value: []byte("hello world again")}) responseChan1 := selector.Send(link, request1) responseChan2 := selector.Send(link, request2) //make sure we can read in other order than was sent response2 := <-responseChan2 response1 := <-responseChan1 assert(t, response1.err, nil) assert(t, response2.err, nil) }
func (nc *NetworkClient) buildProduceRequest(batch []*ProducerRecord) *siesta.ProduceRequest { request := new(siesta.ProduceRequest) request.RequiredAcks = int16(nc.RequiredAcks) request.AckTimeoutMs = nc.AckTimeoutMs for _, record := range batch { request.AddMessage(record.Topic, record.Partition, &siesta.Message{Key: record.encodedKey, Value: record.encodedValue}) } return request }