Skip to content

Commit

Permalink
use ctx in calls to send request
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Jan 20, 2022
1 parent f2488ea commit 0abcf6b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,12 @@ func (c *Client) DetachSession() (*Session, error) {
// the response. If the client has an active session it injects the
// authentication token.
func (c *Client) Send(req ua.Request, h func(interface{}) error) error {
return c.SendWithContext(context.Background(), req, h)
}
func (c *Client) SendWithContext(ctx context.Context, req ua.Request, h func(interface{}) error) error {
stats.Client().Add("Send", 1)

err := c.sendWithTimeout(req, c.cfg.sechan.RequestTimeout, h)
err := c.sendWithTimeout(ctx, req, c.cfg.sechan.RequestTimeout, h)
stats.RecordError(err)

return err
Expand All @@ -855,15 +858,15 @@ func (c *Client) Send(req ua.Request, h func(interface{}) error) error {
// sendWithTimeout sends the request via the secure channel with a custom timeout and registers a handler for
// the response. If the client has an active session it injects the
// authentication token.
func (c *Client) sendWithTimeout(req ua.Request, timeout time.Duration, h func(interface{}) error) error {
func (c *Client) sendWithTimeout(ctx context.Context, req ua.Request, timeout time.Duration, h func(interface{}) error) error {
if c.SecureChannel() == nil {
return ua.StatusBadServerNotConnected
}
var authToken *ua.NodeID
if s := c.Session(); s != nil {
authToken = s.resp.AuthenticationToken
}
return c.SecureChannel().SendRequestWithTimeout(req, authToken, timeout, h)
return c.SecureChannel().SendRequestWithTimeoutWithContext(ctx, req, authToken, timeout, h)
}

// Node returns a node object which accesses its attributes
Expand Down
6 changes: 3 additions & 3 deletions client_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (c *Client) publish(ctx context.Context) error {

// send the next publish request
// note that res contains data even if an error was returned
res, err := c.sendPublishRequest()
res, err := c.sendPublishRequest(ctx)
stats.RecordError(err)
switch {
case err == io.EOF:
Expand Down Expand Up @@ -508,7 +508,7 @@ func (c *Client) handleNotification(ctx context.Context, sub *Subscription, res
})
}

func (c *Client) sendPublishRequest() (*ua.PublishResponse, error) {
func (c *Client) sendPublishRequest(ctx context.Context) (*ua.PublishResponse, error) {
dlog := debug.NewPrefixLogger("publish: ")

c.subMux.RLock()
Expand All @@ -522,7 +522,7 @@ func (c *Client) sendPublishRequest() (*ua.PublishResponse, error) {

dlog.Printf("PublishRequest: %s", debug.ToJSON(req))
var res *ua.PublishResponse
err := c.sendWithTimeout(req, c.publishTimeout(), func(v interface{}) error {
err := c.sendWithTimeout(ctx, req, c.publishTimeout(), func(v interface{}) error {
return safeAssign(v, &res)
})
stats.RecordError(err)
Expand Down
2 changes: 1 addition & 1 deletion uasc/secure_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func (s *SecureChannel) Renew(ctx context.Context) error {

// SendRequest sends the service request and calls h with the response.
func (s *SecureChannel) SendRequest(req ua.Request, authToken *ua.NodeID, h func(interface{}) error) error {
return s.SendRequestWithTimeout(req, authToken, s.cfg.RequestTimeout, h)
return s.SendRequestWithContext(context.Background(), req, authToken, h)
}

func (s *SecureChannel) SendRequestWithContext(ctx context.Context, req ua.Request, authToken *ua.NodeID, h func(interface{}) error) error {
Expand Down

0 comments on commit 0abcf6b

Please sign in to comment.