Skip to content

Commit

Permalink
avoid checking read timeouts continuously
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Aug 14, 2021
1 parent 4fecbe2 commit 8eba042
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 14 additions & 18 deletions nflog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8eba042

Please sign in to comment.