@@ -2,6 +2,12 @@ package sarama
2
2
3
3
import "sync"
4
4
5
+ var expectationsPool = sync.Pool {
6
+ New : func () interface {} {
7
+ return make (chan * ProducerError , 1 )
8
+ },
9
+ }
10
+
5
11
// SyncProducer publishes Kafka messages, blocking until they have been acknowledged. It routes messages to the correct
6
12
// broker, refreshing metadata as appropriate, and parses responses for errors. You must call Close() on a producer
7
13
// to avoid leaks, it may not be garbage-collected automatically when it passes out of scope.
@@ -110,32 +116,38 @@ func verifyProducerConfig(config *Config) error {
110
116
}
111
117
112
118
func (sp * syncProducer ) SendMessage (msg * ProducerMessage ) (partition int32 , offset int64 , err error ) {
113
- expectation := make ( chan * ProducerError , 1 )
119
+ expectation := expectationsPool . Get ().( chan * ProducerError )
114
120
msg .expectation = expectation
115
121
sp .producer .Input () <- msg
116
-
117
- if pErr := <- expectation ; pErr != nil {
122
+ pErr := <- expectation
123
+ msg .expectation = nil
124
+ expectationsPool .Put (expectation )
125
+ if pErr != nil {
118
126
return - 1 , - 1 , pErr .Err
119
127
}
120
128
121
129
return msg .Partition , msg .Offset , nil
122
130
}
123
131
124
132
func (sp * syncProducer ) SendMessages (msgs []* ProducerMessage ) error {
125
- expectations := make (chan chan * ProducerError , len (msgs ))
133
+ indices := make (chan int , len (msgs ))
126
134
go func () {
127
- for _ , msg := range msgs {
128
- expectation := make ( chan * ProducerError , 1 )
135
+ for i , msg := range msgs {
136
+ expectation := expectationsPool . Get ().( chan * ProducerError )
129
137
msg .expectation = expectation
130
138
sp .producer .Input () <- msg
131
- expectations <- expectation
139
+ indices <- i
132
140
}
133
- close (expectations )
141
+ close (indices )
134
142
}()
135
143
136
144
var errors ProducerErrors
137
- for expectation := range expectations {
138
- if pErr := <- expectation ; pErr != nil {
145
+ for i := range indices {
146
+ expectation := msgs [i ].expectation
147
+ pErr := <- expectation
148
+ msgs [i ].expectation = nil
149
+ expectationsPool .Put (expectation )
150
+ if pErr != nil {
139
151
errors = append (errors , pErr )
140
152
}
141
153
}
0 commit comments