func main() { flag.Parse() if *projID == "" { log.Fatal("-p is required") } if *subName == "" { log.Fatal("-s is required") } quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt) ctx := context.Background() client, err := pubsub.NewClient(ctx, *projID) if err != nil { log.Fatalf("creating pubsub client: %v", err) } sub := client.Subscription(*subName) it, err := sub.Pull(ctx, pubsub.MaxExtension(time.Minute)) if err != nil { fmt.Printf("error constructing iterator: %v", err) return } defer it.Stop() go func() { <-quit it.Stop() }() for i := 0; i < *numConsume; i++ { m, err := it.Next() if err == pubsub.Done { break } if err != nil { fmt.Printf("advancing iterator: %v", err) break } fmt.Printf("got message: %v\n", string(m.Data)) m.Done(true) } }
// pullMessages reads messages from a subscription, and farms them out to a // number of goroutines for processing. func pullMessages(client *pubsub.Client, argv []string) { checkArgs(argv, 3) sub := client.Subscription(argv[1]) workers, err := strconv.Atoi(argv[2]) if err != nil { log.Fatalf("Atoi failed, %v", err) } rep := newReporter("Received") defer rep.Stop() msgs := make(chan *pubsub.Message) for i := 0; i < int(workers); i++ { go processMessages(msgs, rep, !*reportMPS) } it, err := sub.Pull(context.Background(), pubsub.MaxExtension(time.Minute)) if err != nil { log.Fatalf("failed to construct iterator: %v", err) } defer it.Stop() for !shouldQuit() { m, err := it.Next() if err != nil { log.Fatalf("error reading from iterator: %v", err) } msgs <- m } // Shut down all processMessages goroutines. close(msgs) // The deferred call to it.Stop will block until each m.Done has been // called on each message. }
ctx := context.Background() var psClient *pubsub.Client if KeyPath != "" { psClient = JWTClientInit(&ctx) } else { psClient = GCEClientInit(&ctx, Gceproject) } if psClient == nil { log.Errorf("PubSub client is nil") os.Exit(1) } log.Debugf("client: %#v", psClient) // Create message iterator from client sub := psClient.Subscription(subscription) it, err := sub.Pull(ctx, pubsub.MaxExtension(time.Minute*1)) if err != nil { log.Errorf("error creating pubsub iterator: %v", err) } defer it.Stop() msgs := make(chan *pubsub.Message) go func() { for !shouldQuit(quit) { m, err := it.Next() if err != nil { switch err { case pubsub.Done: log.Infof("pubsub interator finished") default: log.Errorf("error reading from iterator: %v", err)
func NewSubscriber(ctx context.Context, cfg Config) (pubsub.Subscriber, error) { client, err := gpubsub.NewClient(ctx, cfg.ProjectID) if err != nil { return nil, err } return &subscriber{ sub: subscriptionImpl{sub: client.Subscription(cfg.Subscription)}, ctx: ctx, name: cfg.Subscription, stop: make(chan chan error, 1), }, nil } var ( defaultMaxMessages = gpubsub.MaxPrefetch(10) defaultMaxExtension = gpubsub.MaxExtension(60 * time.Second) ) // Start will start pulling from pubsub via a pubsub.Iterator. func (s *subscriber) Start() <-chan pubsub.SubscriberMessage { output := make(chan pubsub.SubscriberMessage) go func(s *subscriber, output chan pubsub.SubscriberMessage) { defer close(output) var ( iter iterator msg message err error ) iter, err = s.sub.Pull(s.ctx, defaultMaxMessages, defaultMaxExtension) if err != nil {