@@ -35,12 +35,10 @@ const (
35
35
defaultMaxRetryWait = 60000
36
36
defaultMaxRetry = 13
37
37
defaultReconnectWaitIncreRate = 1.5
38
- // Default sub-second precision value to false since it is only compatible
39
- // with fluentd versions v0.14 and above.
40
- defaultSubSecondPrecision = false
41
38
42
39
// Default value whether to skip checking insecure certs on TLS connections.
43
40
defaultTlsInsecureSkipVerify = false
41
+ defaultReadTimeout = time .Duration (0 ) // Read() will not time out
44
42
)
45
43
46
44
// randomGenerator is used by getUniqueId to generate ack hashes. Its value is replaced
@@ -82,6 +80,9 @@ type Config struct {
82
80
83
81
// Flag to skip verifying insecure certs on TLS connections
84
82
TlsInsecureSkipVerify bool `json:"tls_insecure_skip_verify"`
83
+
84
+ // ReadTimeout specifies the timeout on reads. Currently only acks are read.
85
+ ReadTimeout time.Duration `json:"read_timeout"`
85
86
}
86
87
87
88
type ErrUnknownNetwork struct {
@@ -153,6 +154,9 @@ func newWithDialer(config Config, d dialer) (f *Fluent, err error) {
153
154
if config .WriteTimeout == 0 {
154
155
config .WriteTimeout = defaultWriteTimeout
155
156
}
157
+ if config .ReadTimeout == 0 {
158
+ config .ReadTimeout = defaultReadTimeout
159
+ }
156
160
if config .BufferLimit == 0 {
157
161
config .BufferLimit = defaultBufferLimit
158
162
}
@@ -629,11 +633,25 @@ func (f *Fluent) syncReadAck(ctx context.Context) (*AckResp, error) {
629
633
resp := & AckResp {}
630
634
var err error
631
635
636
+ if f .conn == nil {
637
+ return resp , fmt .Errorf ("fluent#read: connection has been closed before reading from it" )
638
+ }
639
+
632
640
// Check if context is cancelled. If it is, we can return early here.
633
641
if err := ctx .Err (); err != nil {
634
642
return resp , errIsClosing
635
643
}
636
644
645
+ t := f .Config .ReadTimeout
646
+ if time .Duration (0 ) < t {
647
+ err = f .conn .SetReadDeadline (time .Now ().Add (t ))
648
+ } else {
649
+ err = f .conn .SetReadDeadline (time.Time {})
650
+ }
651
+ if err != nil {
652
+ return resp , fmt .Errorf ("fluent#read: failed to set read deadline: %w" , err )
653
+ }
654
+
637
655
if f .Config .MarshalAsJSON {
638
656
dec := json .NewDecoder (f .conn )
639
657
err = dec .Decode (resp )
0 commit comments