title: Using Google PubSub with Golang. The most efficient (cost) way to poll the service
body: <p>We're in the process of moving from AMQP to Google's Pubsub. </p>
  
  <p><a href="https://cloud.google.com/pubsub/subscriber" rel="noreferrer">The docs suggest</a> that pull might be the best choice for us since we're using compute engine and can't open our workers to receive via the push service.</p>
  
  <p>It also says that pull might incur additional costs depending on usage:</p>
  
  <blockquote>
    <p>If polling is used, high network usage may be incurred if you are
    opening connections frequently and closing them immediately.</p>
  </blockquote>
  
  <p>We'd created a test subscriber in go that runs in a loop as so:</p>
  
  <pre><code>func main() {
      jsonKey, err := ioutil.ReadFile("pubsub-key.json")
      if err != nil {
          log.Fatal(err)
      }
      conf, err := google.JWTConfigFromJSON(
          jsonKey,
          pubsub.ScopeCloudPlatform,
          pubsub.ScopePubSub,
      )
      if err != nil {
          log.Fatal(err)
      }
      ctx := cloud.NewContext("xxx", conf.Client(oauth2.NoContext))
  
      msgIDs, err := pubsub.Publish(ctx, "topic1", &amp;pubsub.Message{
          Data: []byte("hello world"),
      })
  
      if err != nil {
          log.Println(err)
      }
  
      log.Printf("Published a message with a message id: %s\n", msgIDs[0])
  
      for {
          msgs, err := pubsub.Pull(ctx, "subscription1", 1)
          if err != nil {
              log.Println(err)
          }
  
          if len(msgs) &gt; 0 {
              log.Printf("New message arrived: %v, len: %d\n", msgs[0].ID, len(msgs))
              if err := pubsub.Ack(ctx, "subscription1", msgs[0].AckID); err != nil {
                  log.Fatal(err)
              }
              log.Println("Acknowledged message")
              log.Printf("Message: %s", msgs[0].Data)
          }
      }
  }
  </code></pre>
  
  <p>The question I have though is really whether this is the correct / recommended way to go about pulling messages.</p>
  
  <p>We recieve about 100msg per second throughout the day. I'm not sure if running it in an endless loop is going to bankrupt us and can't find any other decent go examples.</p>