Skip to content

Commit

Permalink
Add function SetReadTimeoutNoErr
Browse files Browse the repository at this point in the history
Same functionality as SetReadTimeout except that it does not return any
errors. This makes it more easily used in combination with defer. The
potential errors from the underlying serial library are handled
(invalidTimeoutValue), but a panic is added to notify about potential
API changes.

Deprecation notice added for SetReadTimeout
  • Loading branch information
dehanj authored and mchack-work committed Oct 24, 2024
1 parent 3d1a960 commit e16b32b
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions tkeyclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ func (tk TillitisKey) Close() error {
return nil
}

// SetReadTimeout sets the timeout of the underlying serial connection
// to the TKey. Pass 0 seconds to not have any timeout. Note that the
// timeout implemented in the serial lib only works for simple Read().
// E.g. io.ReadFull() will Read() until the buffer is full.
// SetReadTimeout sets the timeout of the underlying serial connection to the
// TKey. Pass 0 seconds to not have any timeout. Note that the timeout
// implemented in the serial lib only works for simple Read(). E.g.
// io.ReadFull() will Read() until the buffer is full.
//
// Deprecated: use SetReadTimeoutNoErr, which can more easily be used with
// defer.
func (tk TillitisKey) SetReadTimeout(seconds int) error {
var t time.Duration = -1
if seconds > 0 {
Expand All @@ -123,6 +126,26 @@ func (tk TillitisKey) SetReadTimeout(seconds int) error {
return nil
}

// SetReadTimeoutNoErr sets the timeout, in seconds, of the underlying
// serial connection to the TKey. Pass 0 seconds to not have any
// timeout.
//
// Note that the timeout only works for simple Read(). E.g.
// io.ReadFull() will still read until the buffer is full.
func (tk TillitisKey) SetReadTimeoutNoErr(seconds int) {
var t time.Duration = -1 // disables timeout
if seconds > 0 {
t = time.Duration(seconds) * time.Second
}
if err := tk.conn.SetReadTimeout(t); err != nil {
// err != nil exclusively on invalid values of t,
// which is handled before the call. Panic only
// possible for API change in go.bug.st/serial
panic(fmt.Sprintf("SetReadTimeout: %v", err))
}
return
}

type NameVersion struct {
Name0 string
Name1 string
Expand Down

0 comments on commit e16b32b

Please sign in to comment.