This example shows how to use the producer while simultaneously reading the Errors channel to know about any failures.
()
| 2838 | // This example shows how to use the producer while simultaneously |
| 2839 | // reading the Errors channel to know about any failures. |
| 2840 | func ExampleAsyncProducer_select() { |
| 2841 | producer, err := NewAsyncProducer([]string{"localhost:9092"}, nil) |
| 2842 | if err != nil { |
| 2843 | panic(err) |
| 2844 | } |
| 2845 | |
| 2846 | defer func() { |
| 2847 | if err := producer.Close(); err != nil { |
| 2848 | log.Fatalln(err) |
| 2849 | } |
| 2850 | }() |
| 2851 | |
| 2852 | // Trap SIGINT to trigger a shutdown. |
| 2853 | signals := make(chan os.Signal, 1) |
| 2854 | signal.Notify(signals, os.Interrupt) |
| 2855 | |
| 2856 | var enqueued, producerErrors int |
| 2857 | ProducerLoop: |
| 2858 | for { |
| 2859 | select { |
| 2860 | case producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder("testing 123")}: |
| 2861 | enqueued++ |
| 2862 | case err := <-producer.Errors(): |
| 2863 | log.Println("Failed to produce message", err) |
| 2864 | producerErrors++ |
| 2865 | case <-signals: |
| 2866 | break ProducerLoop |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | log.Printf("Enqueued: %d; errors: %d\n", enqueued, producerErrors) |
| 2871 | } |
| 2872 | |
| 2873 | // This example shows how to use the producer with separate goroutines |
| 2874 | // reading from the Successes and Errors channels. Note that in order |
nothing calls this directly
no test coverage detected