@@ -39,19 +39,18 @@ type DataMessage struct {
3939
4040type MessageFunc func (* DataMessage ) error
4141
42- func NewTopicManager (projectID string ) (* topicManager , error ) {
42+ func NewTopicManager (ctx context. Context , projectID string ) (* topicManager , error ) {
4343 tpm := & topicManager {
4444 projectID : projectID ,
4545 }
46- err := tpm .connect ()
46+ err := tpm .connect (ctx )
4747 if err != nil {
4848 return nil , err
4949 }
5050 return tpm , nil
5151}
5252
53- func (tm * topicManager ) connect () error {
54- ctx := t .Context ()
53+ func (tm * topicManager ) connect (ctx context.Context ) error {
5554 client , err := pubsub .NewClient (ctx , tm .projectID )
5655 if err != nil {
5756 return fmt .Errorf ("GCP pubsub.NewClient failed to connect: %v" , err )
@@ -65,9 +64,8 @@ func (tm *topicManager) disconnect() error {
6564 return tm .gcpClient .Close ()
6665}
6766
68- func (tp * topicManager ) GetMessages (topicID string , msgTimeout time.Duration , subID string , fn MessageFunc ) (int , error ) {
69- ctx := t .Context ()
70-
67+ func (tp * topicManager ) GetMessages (ctx context.Context , topicID string , msgTimeout time.Duration , subID string ,
68+ fn MessageFunc ) (int , error ) {
7169 topic := tp .gcpClient .Topic (topicID )
7270 cfg := & pubsub.SubscriptionConfig {
7371 Topic : topic ,
@@ -133,24 +131,23 @@ func getOrCreateSub(ctx context.Context, client *pubsub.Client, subID string, cf
133131 return sub , nil
134132}
135133
136- func deleteSubscriptions (projectID string , subs []string ) error {
134+ func deleteSubscriptions (ctx context. Context , projectID string , subs []string ) error {
137135 for _ , s := range subs {
138136 fmt .Printf ("Deleting subscription: %s\n " , s )
139- deleteSubscription (os .Stdout , projectID , s )
137+ deleteSubscription (ctx , os .Stdout , projectID , s )
140138 }
141139 return nil
142140}
143141
144- func deleteTopics (projectID string , topics []string ) error {
142+ func deleteTopics (ctx context. Context , projectID string , topics []string ) error {
145143 for _ , t := range topics {
146144 fmt .Printf ("Deleting topics: %s\n " , t )
147- deleteTopic (os .Stdout , projectID , t )
145+ deleteTopic (ctx , os .Stdout , projectID , t )
148146 }
149147 return nil
150148}
151149
152- func deleteSubscription (w io.Writer , projectID , subID string ) error {
153- ctx := t .Context ()
150+ func deleteSubscription (ctx context.Context , w io.Writer , projectID , subID string ) error {
154151 client , err := pubsub .NewClient (ctx , projectID )
155152 if err != nil {
156153 return fmt .Errorf ("pubsub.NewClient: %v" , err )
@@ -165,8 +162,7 @@ func deleteSubscription(w io.Writer, projectID, subID string) error {
165162 return nil
166163}
167164
168- func deleteTopic (w io.Writer , projectID , topicID string ) error {
169- ctx := t .Context ()
165+ func deleteTopic (ctx context.Context , w io.Writer , projectID , topicID string ) error {
170166 client , err := pubsub .NewClient (ctx , projectID )
171167 if err != nil {
172168 return fmt .Errorf ("pubsub.NewClient: %v" , err )
0 commit comments