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

Add ConnOption WithDefaultSendTimeout #380

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 22 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"
"sync"
"time"
)

var (
Expand All @@ -30,8 +31,9 @@ var ErrClosed = errors.New("dbus: connection closed by user")
type Conn struct {
transport

ctx context.Context
cancelCtx context.CancelFunc
ctx context.Context
cancelCtx context.CancelFunc
defaultTimeout time.Duration

closeOnce sync.Once
closeErr error
Expand Down Expand Up @@ -258,6 +260,14 @@ func WithContext(ctx context.Context) ConnOption {
}
}

// WithDefaultSendTimeout supplies the connection with a default timeout for outgoing messages.
func WithDefaultSendTimeout(timeout time.Duration) ConnOption {
return func(conn *Conn) error {
conn.defaultTimeout = timeout
return nil
}
}

// NewConn creates a new private *Conn from an already established connection.
func NewConn(conn io.ReadWriteCloser, opts ...ConnOption) (*Conn, error) {
return newConn(genericTransport{conn}, opts...)
Expand Down Expand Up @@ -540,7 +550,16 @@ func (conn *Conn) send(ctx context.Context, msg *Message, ch chan *Call) *Call {
}

var call *Call
ctx, canceler := context.WithCancel(ctx)
var canceler context.CancelFunc
if ctx == context.TODO() {
if conn.defaultTimeout > 0 {
ctx, canceler = context.WithTimeout(context.Background(), conn.defaultTimeout)
} else {
ctx, canceler = context.WithCancel(context.Background())
}
} else {
ctx, canceler = context.WithCancel(ctx)
}
msg.serial = conn.getSerial()
if msg.Type == TypeMethodCall && msg.Flags&FlagNoReplyExpected == 0 {
call = new(Call)
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Object struct {

// Call calls a method with (*Object).Go and waits for its reply.
func (o *Object) Call(method string, flags Flags, args ...interface{}) *Call {
return <-o.createCall(context.Background(), method, flags, make(chan *Call, 1), args...).Done
return <-o.createCall(context.TODO(), method, flags, make(chan *Call, 1), args...).Done
}

// CallWithContext acts like Call but takes a context
Expand Down Expand Up @@ -90,7 +90,7 @@ func (o *Object) RemoveMatchSignal(iface, member string, options ...MatchOption)
// If the method parameter contains a dot ('.'), the part before the last dot
// specifies the interface on which the method is called.
func (o *Object) Go(method string, flags Flags, ch chan *Call, args ...interface{}) *Call {
return o.createCall(context.Background(), method, flags, ch, args...)
return o.createCall(context.TODO(), method, flags, ch, args...)
}

// GoWithContext acts like Go but takes a context
Expand Down