Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DialV2: add support for custom timeouts #64

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions bmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,55 @@ var (
namespace = "bmc"
)

type Config struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a more descriptive name? E.g., V2SessionlessTransportConfig or DialConfig.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree Config is a little too generic. I would use DialConfig and DialConfigOption. I'm not sure what the conventions are around exporting DialConfig - can we get away with dialConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get away with dialConfig?

I believe we can: https://pkg.go.dev/golang.org/x/net/idna#Option

Timeout time.Duration
}

type OptionFunc func(c *Config)

func WithTimeout(timeout time.Duration) OptionFunc {
return func(c *Config) {
c.Timeout = timeout
}
}

// Dial is currently an alias for DialV2. When IPMI v1.5 is implemented, this
// will query the BMC for IPMI v2.0 capability. If it supports IPMI v2.0, a
// V2SessionlessTransport will be returned, otherwise a V1SessionlessTransport
// will be returned. If you know the BMC's capabilities, or need a specific
// feature (e.g. DCMI), use the DialV*() functions instead, which expose
// additional information and functionality.
func Dial(_ context.Context, addr string) (SessionlessTransport, error) {
return DialV2(addr)
func Dial(_ context.Context, addr string, options ...OptionFunc) (SessionlessTransport, error) {
return DialV2(addr, options...)
}

// DialV2 establishes a new IPMI v2.0 connection with the supplied BMC. The
// address is of the form IP[:port] (IPv6 must be enclosed in square brackets).
// Use this if you know the BMC supports IPMI v2.0 and/or require DCMI
// functionality. Note v4 is preferred to v6 if a hostname is passed returning
// both A and AAAA records.
func DialV2(addr string) (*V2SessionlessTransport, error) {
func DialV2(addr string, options ...OptionFunc) (*V2SessionlessTransport, error) {
v2ConnectionOpenAttempts.Inc()
t, err := newTransport(addr)
if err != nil {
v2ConnectionOpenFailures.Inc()
return nil, err
}
v2ConnectionsOpen.Inc()
return newV2SessionlessTransport(t), nil

c := &Config{
Timeout: 1 * time.Second,
}
for _, option := range options {
option(c)
}
return newV2SessionlessTransport(t, c), nil
}

func newV2SessionlessTransport(t transport.Transport) *V2SessionlessTransport {
func newV2SessionlessTransport(t transport.Transport, c *Config) *V2SessionlessTransport {
return &V2SessionlessTransport{
Transport: t,
V2Sessionless: newV2Sessionless(t, time.Second),
V2Sessionless: newV2Sessionless(t, c.Timeout),
}
}

Expand Down
Loading