From 8eba042cd8f6feac8839cc82c24f43453f6bfa7b Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Sat, 14 Aug 2021 09:31:24 +0200 Subject: [PATCH] avoid checking read timeouts continuously Signed-off-by: Florian Lehner --- nflog.go | 32 ++++++++++++++------------------ types.go | 4 ++++ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/nflog.go b/nflog.go index f526fc6..6d297a6 100644 --- a/nflog.go +++ b/nflog.go @@ -20,14 +20,13 @@ type Nflog struct { logger *log.Logger - flags []byte //uint16 - bufsize []byte //uint32 - qthresh []byte //uint32 - timeout []byte //uint32 - group uint16 - copyMode uint8 - settings uint16 - setReadTimeout func() error + flags []byte //uint16 + bufsize []byte //uint32 + qthresh []byte //uint32 + timeout []byte //uint32 + group uint16 + copyMode uint8 + settings uint16 } // devNull satisfies io.Writer, in case *log.Logger is not provided @@ -94,15 +93,6 @@ func Open(config *Config) (*Nflog, error) { nflog.copyMode = config.Copymode nflog.settings = config.Settings - if config.ReadTimeout > 0 { - nflog.setReadTimeout = func() error { - deadline := time.Now().Add(config.ReadTimeout) - return nflog.Con.SetReadDeadline(deadline) - } - } else { - nflog.setReadTimeout = func() error { return nil } - } - return &nflog, nil } @@ -219,12 +209,18 @@ func (nflog *Nflog) RegisterWithErrorFunc(ctx context.Context, fn HookFunc, errf return } }() + go func() { + // block until context is done + <-ctx.Done() + // Set the read deadline to a point in the past to interrupt + // possible blocking Receive() calls. + nflog.Con.SetReadDeadline(time.Now().Add(-1 * time.Second)) + }() for { if err := ctx.Err(); err != nil { nflog.logger.Printf("Stop receiving nflog messages: %v", err) return } - nflog.setReadTimeout() reply, err := nflog.Con.Receive() if err != nil { if ret := errfn(err); ret != 0 { diff --git a/types.go b/types.go index f3fa09e..db3ad75 100644 --- a/types.go +++ b/types.go @@ -142,6 +142,10 @@ type Config struct { Settings uint16 // Time till a read action times out - only available for Go >= 1.12 + // + // Deprecated: Cancel the context passed to RegisterWithErrorFunc() or Register() + // to remove the hook from the nfloq gracefully. Setting this value does no longer + // have an effect on nflog. ReadTimeout time.Duration // Interface to log internals.