-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (91 loc) · 2.58 KB
/
main.go
File metadata and controls
110 lines (91 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/segmentio/kafka-go"
)
// ProduceMessages simulates high-throughput message production to a Kafka topic.
// It incrementally increases the number of messages sent to Kafka.
func ProduceMessages(writer *kafka.Writer, wg *sync.WaitGroup, done chan bool) {
defer wg.Done()
messageCount := 1000 // Number of messages to simulate high throughput
for i := 0; i < messageCount; i++ {
select {
case <-done:
return
default:
}
message := kafka.Message{
Key: []byte(fmt.Sprintf("Key-%d", i)),
Value: []byte(fmt.Sprintf("This is message %d", i)),
}
// Context with timeout for each message
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Try to write message to Kafka
if err := writer.WriteMessages(ctx, message); err != nil {
log.Printf("Could not write message %d to Kafka: %v", i, err)
}
}
}
// ConsumeMessages reads messages from a Kafka topic to simulate high-throughput consumption.
func ConsumeMessages(reader *kafka.Reader, wg *sync.WaitGroup, done chan bool) {
defer wg.Done()
for {
select {
case <-done:
return
default:
}
// Read the next message
msg, err := reader.ReadMessage(context.Background())
if err != nil {
log.Printf("Could not read message: %v", err)
continue
}
log.Printf("Consumed message: %s = %s\n", string(msg.Key), string(msg.Value))
}
}
func main() {
// Configure Kafka broker addresses
brokerAddress := "localhost:9092"
// Kafka topic to produce and consume messages
topic := "high-throughput-topic"
// Initialize Kafka writer (producer) using the latest API
writer := kafka.Writer{
Addr: kafka.TCP(brokerAddress),
Topic: topic,
Balancer: &kafka.LeastBytes{}, // Load balancing
}
defer writer.Close()
// Initialize Kafka reader (consumer)
reader := kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{brokerAddress},
Topic: topic,
Partition: 0,
})
defer reader.Close()
// Wait group for goroutine synchronization
wg := sync.WaitGroup{}
// Channel for stopping the goroutines
done := make(chan bool)
// Start producer and consumer as goroutines
wg.Add(2)
go ProduceMessages(&writer, &wg, done)
go ConsumeMessages(reader, &wg, done)
// Capture OS signals for clean shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
<-signalChan
log.Println("Stopping producer and consumer...")
close(done)
// Wait for all goroutines to finish
wg.Wait()
log.Println("Simulation finished.")
}