|
@@ -6,17 +6,20 @@ import (
|
|
|
"net"
|
|
|
"strconv"
|
|
|
"sync"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
|
|
|
type BrokerConfig struct {
|
|
|
- MaxOpenRequests int
|
|
|
+ MaxOpenRequests int
|
|
|
+ ReadTimeout time.Duration
|
|
|
}
|
|
|
|
|
|
|
|
|
func NewBrokerConfig() *BrokerConfig {
|
|
|
return &BrokerConfig{
|
|
|
- MaxOpenRequests: 1,
|
|
|
+ MaxOpenRequests: 4,
|
|
|
+ ReadTimeout: 1 * time.Minute,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -27,6 +30,10 @@ func (config *BrokerConfig) Validate() error {
|
|
|
return ConfigurationError("Invalid MaxOpenRequests")
|
|
|
}
|
|
|
|
|
|
+ if config.ReadTimeout <= 0 {
|
|
|
+ return ConfigurationError("Invalid ReadTimeout")
|
|
|
+ }
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -339,6 +346,8 @@ func (b *Broker) encode(pe packetEncoder) (err error) {
|
|
|
func (b *Broker) responseReceiver() {
|
|
|
header := make([]byte, 8)
|
|
|
for response := range b.responses {
|
|
|
+ b.conn.SetReadDeadline(time.Now().Add(b.conf.ReadTimeout))
|
|
|
+
|
|
|
_, err := io.ReadFull(b.conn, header)
|
|
|
if err != nil {
|
|
|
response.errors <- err
|
|
@@ -352,6 +361,8 @@ func (b *Broker) responseReceiver() {
|
|
|
continue
|
|
|
}
|
|
|
if decodedHeader.correlationID != response.correlationID {
|
|
|
+
|
|
|
+
|
|
|
response.errors <- DecodingError{Info: "CorrelationID didn't match"}
|
|
|
continue
|
|
|
}
|
|
@@ -359,6 +370,10 @@ func (b *Broker) responseReceiver() {
|
|
|
buf := make([]byte, decodedHeader.length-4)
|
|
|
_, err = io.ReadFull(b.conn, buf)
|
|
|
if err != nil {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
response.errors <- err
|
|
|
continue
|
|
|
}
|