Skip to content

Commit 2a93d0e

Browse files
committed
avoid checking read timeouts continuously
Signed-off-by: Florian Lehner <[email protected]>
1 parent 59e800a commit 2a93d0e

File tree

6 files changed

+14
-24
lines changed

6 files changed

+14
-24
lines changed

conntrack.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66
"fmt"
77
"log"
8+
"time"
89
"unsafe"
910

1011
"github.com/florianl/go-conntrack/internal/unix"
@@ -335,6 +336,13 @@ func (nfct *Nfct) register(ctx context.Context, t Table, groups NetlinkGroup, fi
335336
}
336337

337338
go func() {
339+
go func() {
340+
// block until context is done
341+
<-ctx.Done()
342+
// Set the read deadline to a point in the past to interrupt
343+
// possible blocking Receive() calls.
344+
nfct.Con.SetReadDeadline(time.Now().Add(-1 * time.Second))
345+
}()
338346
defer func() {
339347
if err := nfct.removeFilter(); err != nil {
340348
nfct.logger.Printf("could not remove filter: %v", err)
@@ -351,19 +359,17 @@ func (nfct *Nfct) register(ctx context.Context, t Table, groups NetlinkGroup, fi
351359
return
352360
default:
353361
}
354-
if err := nfct.setReadTimeout(); err != nil {
355-
nfct.logger.Printf("could not set read timeout: %v", err)
356-
}
357362
reply, err := nfct.Con.Receive()
358363
if err != nil {
359364
if opError, ok := err.(*netlink.OpError); ok {
360365
if opError.Timeout() || opError.Temporary() {
361366
continue
362367
}
363368
}
364-
nfct.logger.Printf("receiving error: %v", err)
365369
if nfct.errChan != nil {
366370
nfct.errChan <- err
371+
} else {
372+
nfct.logger.Printf("receiving error: %v", err)
367373
}
368374
return
369375
}
@@ -487,9 +493,6 @@ func (nfct *Nfct) send(req netlink.Message) error {
487493
return err
488494
}
489495

490-
if err := nfct.setReadTimeout(); err != nil {
491-
nfct.logger.Printf("could not set read timeout: %v", err)
492-
}
493496
return nil
494497
}
495498

conntrack_gteq_1.12.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ func Open(config *Config) (*Nfct, error) {
2626
} else {
2727
nfct.logger = config.Logger
2828
}
29-
if config.ReadTimeout > 0 {
30-
nfct.setReadTimeout = func() error {
31-
deadline := time.Now().Add(config.ReadTimeout)
32-
return nfct.Con.SetReadDeadline(deadline)
33-
}
34-
} else {
35-
nfct.setReadTimeout = func() error { return nil }
36-
}
3729

3830
if config.WriteTimeout > 0 {
3931
nfct.setWriteTimeout = func() error {

conntrack_lt_1.12.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func Open(config *Config) (*Nfct, error) {
2525
} else {
2626
nfct.logger = config.Logger
2727
}
28-
nfct.setReadTimeout = func() error { return nil }
2928
nfct.setWriteTimeout = func() error { return nil }
3029

3130
return &nfct, nil

conntrack_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ func TestCreate(t *testing.T) {
160160
for _, tc := range tests {
161161
t.Run(tc.name, func(t *testing.T) {
162162
nfct := &Nfct{}
163-
AdjustReadTimeout(nfct, func() error { return nil })
164163
AdjustWriteTimeout(nfct, func() error { return nil })
165164
nfct.Con = nltest.Dial(func(reqs []netlink.Message) ([]netlink.Message, error) {
166165
if len(reqs) == 0 {

export_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package conntrack
22

33
// Export for testing
4-
var AdjustReadTimeout = adjustReadTimeout
54
var AdjustWriteTimeout = adjustWriteTimeout

types.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ type Config struct {
3030
NetNS int
3131

3232
// Time till a read action times out - only available for Go >= 1.12
33+
//
34+
// Deprecated: Cancel the context passed to RegisterFiltered() or Register()
35+
// to remove the conntrack gracefully. Setting this value does no longer
36+
// have an effect on conntrack.
3337
ReadTimeout time.Duration
3438

3539
// Time till a write action times out - only available for Go >= 1.12
@@ -66,15 +70,9 @@ type Nfct struct {
6670

6771
errChan chan error
6872

69-
setReadTimeout func() error
7073
setWriteTimeout func() error
7174
}
7275

73-
// adjust the ReadTimeout (mostly for testing)
74-
func adjustReadTimeout(nfct *Nfct, fn func() error) {
75-
nfct.setReadTimeout = fn
76-
}
77-
7876
// adjust the WriteTimeout (mostly for testing)
7977
func adjustWriteTimeout(nfct *Nfct, fn func() error) {
8078
nfct.setWriteTimeout = fn

0 commit comments

Comments
 (0)