From 560b53a7274e6c1bd1ce210e6d60eebd4679153a Mon Sep 17 00:00:00 2001 From: Peter Saxton Date: Fri, 7 Jan 2022 13:30:12 +0100 Subject: [PATCH 1/2] add context to subscription methods --- client.go | 2 +- client_sub.go | 9 ++++++--- subscription.go | 32 ++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/client.go b/client.go index 58013e1c..37c7eb36 100644 --- a/client.go +++ b/client.go @@ -423,7 +423,7 @@ func (c *Client) monitor(ctx context.Context) { // try to transfer all subscriptions to the new session and // recreate them all if that fails. - res, err := c.transferSubscriptions(subIDs) + res, err := c.transferSubscriptions(ctx, subIDs) switch { case err != nil: dlog.Printf("transfer subscriptions failed. Recreating all subscriptions: %v", err) diff --git a/client_sub.go b/client_sub.go index 1e747ea6..5baf4fa3 100644 --- a/client_sub.go +++ b/client_sub.go @@ -17,6 +17,9 @@ import ( // Parameters that have not been set are set to their default values. // See opcua.DefaultSubscription* constants func (c *Client) Subscribe(params *SubscriptionParameters, notifyCh chan<- *PublishNotificationData) (*Subscription, error) { + return c.SubscribeWithContext(context.Background(), params, notifyCh) +} +func (c *Client) SubscribeWithContext(ctx context.Context, params *SubscriptionParameters, notifyCh chan<- *PublishNotificationData) (*Subscription, error) { stats.Client().Add("Subscribe", 1) if params == nil { @@ -34,7 +37,7 @@ func (c *Client) Subscribe(params *SubscriptionParameters, notifyCh chan<- *Publ } var res *ua.CreateSubscriptionResponse - err := c.Send(req, func(v interface{}) error { + err := c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) if err != nil { @@ -99,14 +102,14 @@ func (c *Client) recreateSubscription(ctx context.Context, id uint32) error { // transferSubscriptions ask the server to transfer the given subscriptions // of the previous session to the current one. -func (c *Client) transferSubscriptions(ids []uint32) (*ua.TransferSubscriptionsResponse, error) { +func (c *Client) transferSubscriptions(ctx context.Context, ids []uint32) (*ua.TransferSubscriptionsResponse, error) { req := &ua.TransferSubscriptionsRequest{ SubscriptionIDs: ids, SendInitialValues: false, } var res *ua.TransferSubscriptionsResponse - err := c.Send(req, func(v interface{}) error { + err := c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) return res, err diff --git a/subscription.go b/subscription.go index 3b9ca088..9dc4762d 100644 --- a/subscription.go +++ b/subscription.go @@ -84,17 +84,17 @@ type PublishNotificationData struct { func (s *Subscription) Cancel(ctx context.Context) error { stats.Subscription().Add("Cancel", 1) s.c.forgetSubscription(ctx, s.SubscriptionID) - return s.delete() + return s.delete(ctx) } // delete removes the subscription from the server. -func (s *Subscription) delete() error { +func (s *Subscription) delete(ctx context.Context) error { req := &ua.DeleteSubscriptionsRequest{ SubscriptionIDs: []uint32{s.SubscriptionID}, } var res *ua.DeleteSubscriptionsResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) @@ -112,6 +112,9 @@ func (s *Subscription) delete() error { } func (s *Subscription) Monitor(ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) { + return s.MonitorWithContext(context.Background(), ts, items...) +} +func (s *Subscription) MonitorWithContext(ctx context.Context, ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) { stats.Subscription().Add("Monitor", 1) stats.Subscription().Add("MonitoredItems", int64(len(items))) @@ -123,7 +126,7 @@ func (s *Subscription) Monitor(ts ua.TimestampsToReturn, items ...*ua.MonitoredI } var res *ua.CreateMonitoredItemsResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) @@ -147,6 +150,9 @@ func (s *Subscription) Monitor(ts ua.TimestampsToReturn, items ...*ua.MonitoredI } func (s *Subscription) Unmonitor(monitoredItemIDs ...uint32) (*ua.DeleteMonitoredItemsResponse, error) { + return s.UnmonitorWithContext(context.Background(), monitoredItemIDs...) +} +func (s *Subscription) UnmonitorWithContext(ctx context.Context, monitoredItemIDs ...uint32) (*ua.DeleteMonitoredItemsResponse, error) { stats.Subscription().Add("Unmonitor", 1) stats.Subscription().Add("UnmonitoredItems", int64(len(monitoredItemIDs))) @@ -156,7 +162,7 @@ func (s *Subscription) Unmonitor(monitoredItemIDs ...uint32) (*ua.DeleteMonitore } var res *ua.DeleteMonitoredItemsResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) @@ -173,6 +179,9 @@ func (s *Subscription) Unmonitor(monitoredItemIDs ...uint32) (*ua.DeleteMonitore } func (s *Subscription) ModifyMonitoredItems(ts ua.TimestampsToReturn, items ...*ua.MonitoredItemModifyRequest) (*ua.ModifyMonitoredItemsResponse, error) { + return s.ModifyMonitoredItemsWithContext(context.Background(), ts, items...) +} +func (s *Subscription) ModifyMonitoredItemsWithContext(ctx context.Context, ts ua.TimestampsToReturn, items ...*ua.MonitoredItemModifyRequest) (*ua.ModifyMonitoredItemsResponse, error) { stats.Subscription().Add("ModifyMonitoredItems", 1) stats.Subscription().Add("ModifiedMonitoredItems", int64(len(items))) @@ -191,7 +200,7 @@ func (s *Subscription) ModifyMonitoredItems(ts ua.TimestampsToReturn, items ...* ItemsToModify: items, } var res *ua.ModifyMonitoredItemsResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) if err != nil { @@ -223,6 +232,9 @@ func (s *Subscription) ModifyMonitoredItems(ts ua.TimestampsToReturn, items ...* // To add links from a triggering item to an item to report provide the server assigned ID(s) in the `add` argument. // To remove links from a triggering item to an item to report provide the server assigned ID(s) in the `remove` argument. func (s *Subscription) SetTriggering(triggeringItemID uint32, add, remove []uint32) (*ua.SetTriggeringResponse, error) { + return s.SetTriggeringWithContext(context.Background(), triggeringItemID, add, remove) +} +func (s *Subscription) SetTriggeringWithContext(ctx context.Context, triggeringItemID uint32, add, remove []uint32) (*ua.SetTriggeringResponse, error) { stats.Subscription().Add("SetTriggering", 1) // Part 4, 5.12.5.2 SetTriggering Service Parameters @@ -234,7 +246,7 @@ func (s *Subscription) SetTriggering(triggeringItemID uint32, add, remove []uint } var res *ua.SetTriggeringResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) return res, err @@ -319,7 +331,7 @@ func (s *Subscription) recreate(ctx context.Context) error { SubscriptionIDs: []uint32{s.SubscriptionID}, } var res *ua.DeleteSubscriptionsResponse - _ = s.c.Send(req, func(v interface{}) error { + _ = s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) dlog.Print("subscription deleted") @@ -336,7 +348,7 @@ func (s *Subscription) recreate(ctx context.Context) error { Priority: params.Priority, } var res *ua.CreateSubscriptionResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) if err != nil { @@ -379,7 +391,7 @@ func (s *Subscription) recreate(ctx context.Context) error { } var res *ua.CreateMonitoredItemsResponse - err := s.c.Send(req, func(v interface{}) error { + err := s.c.SendWithContext(ctx, req, func(v interface{}) error { return safeAssign(v, &res) }) if err != nil { From 9f41dc1004ddc9d550015e3c7fb0072fe4041b33 Mon Sep 17 00:00:00 2001 From: Peter Saxton Date: Thu, 20 Jan 2022 16:17:15 +0100 Subject: [PATCH 2/2] add deprecation notices to subscription methods --- client_sub.go | 5 +++++ subscription.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/client_sub.go b/client_sub.go index 5baf4fa3..460b87d9 100644 --- a/client_sub.go +++ b/client_sub.go @@ -16,9 +16,14 @@ import ( // Subscribe creates a Subscription with given parameters. // Parameters that have not been set are set to their default values. // See opcua.DefaultSubscription* constants +// +// Note: Starting with v0.5 this method will require a context +// and the corresponding XXXWithContext(ctx) method will be removed. func (c *Client) Subscribe(params *SubscriptionParameters, notifyCh chan<- *PublishNotificationData) (*Subscription, error) { return c.SubscribeWithContext(context.Background(), params, notifyCh) } + +// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method. func (c *Client) SubscribeWithContext(ctx context.Context, params *SubscriptionParameters, notifyCh chan<- *PublishNotificationData) (*Subscription, error) { stats.Client().Add("Subscribe", 1) diff --git a/subscription.go b/subscription.go index 9dc4762d..73062e60 100644 --- a/subscription.go +++ b/subscription.go @@ -111,9 +111,13 @@ func (s *Subscription) delete(ctx context.Context) error { } } +// Note: Starting with v0.5 this method will require a context +// and the corresponding XXXWithContext(ctx) method will be removed. func (s *Subscription) Monitor(ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) { return s.MonitorWithContext(context.Background(), ts, items...) } + +// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method. func (s *Subscription) MonitorWithContext(ctx context.Context, ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) { stats.Subscription().Add("Monitor", 1) stats.Subscription().Add("MonitoredItems", int64(len(items))) @@ -149,9 +153,13 @@ func (s *Subscription) MonitorWithContext(ctx context.Context, ts ua.TimestampsT return res, err } +// Note: Starting with v0.5 this method will require a context +// and the corresponding XXXWithContext(ctx) method will be removed. func (s *Subscription) Unmonitor(monitoredItemIDs ...uint32) (*ua.DeleteMonitoredItemsResponse, error) { return s.UnmonitorWithContext(context.Background(), monitoredItemIDs...) } + +// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method. func (s *Subscription) UnmonitorWithContext(ctx context.Context, monitoredItemIDs ...uint32) (*ua.DeleteMonitoredItemsResponse, error) { stats.Subscription().Add("Unmonitor", 1) stats.Subscription().Add("UnmonitoredItems", int64(len(monitoredItemIDs))) @@ -178,9 +186,13 @@ func (s *Subscription) UnmonitorWithContext(ctx context.Context, monitoredItemID return res, err } +// Note: Starting with v0.5 this method will require a context +// and the corresponding XXXWithContext(ctx) method will be removed. func (s *Subscription) ModifyMonitoredItems(ts ua.TimestampsToReturn, items ...*ua.MonitoredItemModifyRequest) (*ua.ModifyMonitoredItemsResponse, error) { return s.ModifyMonitoredItemsWithContext(context.Background(), ts, items...) } + +// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method. func (s *Subscription) ModifyMonitoredItemsWithContext(ctx context.Context, ts ua.TimestampsToReturn, items ...*ua.MonitoredItemModifyRequest) (*ua.ModifyMonitoredItemsResponse, error) { stats.Subscription().Add("ModifyMonitoredItems", 1) stats.Subscription().Add("ModifiedMonitoredItems", int64(len(items))) @@ -231,9 +243,14 @@ func (s *Subscription) ModifyMonitoredItemsWithContext(ctx context.Context, ts u // SetTriggering sends a request to the server to add and/or remove triggering links from a triggering item. // To add links from a triggering item to an item to report provide the server assigned ID(s) in the `add` argument. // To remove links from a triggering item to an item to report provide the server assigned ID(s) in the `remove` argument. +// +// Note: Starting with v0.5 this method will require a context +// and the corresponding XXXWithContext(ctx) method will be removed. func (s *Subscription) SetTriggering(triggeringItemID uint32, add, remove []uint32) (*ua.SetTriggeringResponse, error) { return s.SetTriggeringWithContext(context.Background(), triggeringItemID, add, remove) } + +// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method. func (s *Subscription) SetTriggeringWithContext(ctx context.Context, triggeringItemID uint32, add, remove []uint32) (*ua.SetTriggeringResponse, error) { stats.Subscription().Add("SetTriggering", 1)