support/bundler: fix deadlock when using time based flush

bundler.Bundler can run into a deadlock under the following
configuration:

- handler limit of 1
- high size and count thresholds such that the only reason a bundle is
flushed is because of the delay timer expiring

Under these conditions, with high concurrency, a deadlock can happen
because the background handler goroutine can exit while there is a
pending queue, which will never get flushed.

Specifically, this sequence of events:

- handler goroutine calls postHandle and receives a nil for the next
bundle, signaling it has no more work to do.

- some other goroutine calls AddWait() -> enqueueCurBundle, which sees
that b.handlerLimit cannot be exceeded and proceeds to queue the item

- handler goroutine decrements b.handlerLimit and exits

The fix is to move the decrement of b.handlerLimit into postHandle such
that the decision to exit and allowing a new handler goroutine to be
spawned cannot be re-ordered.

Fixes: googleapis/google-api-go-client#475

Change-Id: I84421e53a4dc24eec36ceb147933aab57a40aec8
Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/55950
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cody Oss <codyoss@google.com>
2 files changed
tree: c51cd2205c6fd6c148f032a4b7c9783dd68c335c
  1. abusiveexperiencereport/
  2. acceleratedmobilepageurl/
  3. accessapproval/
  4. accesscontextmanager/
  5. adexchangebuyer/
  6. adexchangebuyer2/
  7. adexchangeseller/
  8. adexperiencereport/
  9. admin/
  10. admob/
  11. adsense/
  12. adsensehost/
  13. alertcenter/
  14. analytics/
  15. analyticsreporting/
  16. androiddeviceprovisioning/
  17. androidenterprise/
  18. androidmanagement/
  19. androidpublisher/
  20. apigee/
  21. appengine/
  22. appsactivity/
  23. appstate/
  24. bigquery/
  25. bigqueryconnection/
  26. bigquerydatatransfer/
  27. bigqueryreservation/
  28. bigtableadmin/
  29. billingbudgets/
  30. binaryauthorization/
  31. blogger/
  32. books/
  33. calendar/
  34. chat/
  35. civicinfo/
  36. classroom/
  37. cloudasset/
  38. cloudbilling/
  39. cloudbuild/
  40. cloudcommerceprocurement/
  41. clouddebugger/
  42. clouderrorreporting/
  43. cloudfunctions/
  44. cloudidentity/
  45. cloudiot/
  46. cloudkms/
  47. cloudprivatecatalog/
  48. cloudprivatecatalogproducer/
  49. cloudprofiler/
  50. cloudresourcemanager/
  51. cloudscheduler/
  52. cloudsearch/
  53. cloudshell/
  54. cloudtasks/
  55. cloudtrace/
  56. commentanalyzer/
  57. composer/
  58. compute/
  59. consumersurveys/
  60. container/
  61. containeranalysis/
  62. content/
  63. customsearch/
  64. datacatalog/
  65. dataflow/
  66. datafusion/
  67. dataproc/
  68. datastore/
  69. deploymentmanager/
  70. dfareporting/
  71. dialogflow/
  72. digitalassetlinks/
  73. discovery/
  74. displayvideo/
  75. dlp/
  76. dns/
  77. docs/
  78. domainsrdap/
  79. doubleclickbidmanager/
  80. doubleclicksearch/
  81. drive/
  82. driveactivity/
  83. examples/
  84. factchecktools/
  85. fcm/
  86. file/
  87. firebase/
  88. firebasedynamiclinks/
  89. firebasehosting/
  90. firebaseml/
  91. firebaseremoteconfig/
  92. firebaserules/
  93. firestore/
  94. fitness/
  95. fusiontables/
  96. games/
  97. gamesconfiguration/
  98. gameservices/
  99. gamesmanagement/
  100. genomics/
  101. gmail/
  102. google-api-go-generator/
  103. googleapi/
  104. groupsmigration/
  105. groupssettings/
  106. healthcare/
  107. homegraph/
  108. iam/
  109. iamcredentials/
  110. iap/
  111. identitytoolkit/
  112. idtoken/
  113. indexing/
  114. integration-tests/
  115. internal/
  116. iterator/
  117. jobs/
  118. kgsearch/
  119. language/
  120. lib/
  121. libraryagent/
  122. licensing/
  123. lifesciences/
  124. logging/
  125. managedidentities/
  126. manufacturers/
  127. memcache/
  128. mirror/
  129. ml/
  130. monitoring/
  131. networkmanagement/
  132. oauth2/
  133. option/
  134. osconfig/
  135. oslogin/
  136. pagespeedonline/
  137. partners/
  138. people/
  139. playcustomapp/
  140. playmoviespartner/
  141. plus/
  142. plusdomains/
  143. policytroubleshooter/
  144. poly/
  145. prod_tt_sasportal/
  146. proximitybeacon/
  147. pubsub/
  148. qpxexpress/
  149. recommender/
  150. redis/
  151. remotebuildexecution/
  152. replicapool/
  153. replicapoolupdater/
  154. reseller/
  155. run/
  156. runtimeconfig/
  157. safebrowsing/
  158. sasportal/
  159. script/
  160. searchconsole/
  161. secretmanager/
  162. securitycenter/
  163. servicebroker/
  164. serviceconsumermanagement/
  165. servicecontrol/
  166. servicedirectory/
  167. servicemanagement/
  168. servicenetworking/
  169. serviceusage/
  170. serviceuser/
  171. sheets/
  172. siteverification/
  173. slides/
  174. sourcerepo/
  175. spanner/
  176. spectrum/
  177. speech/
  178. sql/
  179. sqladmin/
  180. storage/
  181. storagetransfer/
  182. streetviewpublish/
  183. support/
  184. surveys/
  185. tagmanager/
  186. tasks/
  187. testing/
  188. texttospeech/
  189. toolresults/
  190. tpu/
  191. tracing/
  192. translate/
  193. transport/
  194. urlshortener/
  195. vault/
  196. verifiedaccess/
  197. videointelligence/
  198. vision/
  199. webfonts/
  200. webmasters/
  201. websecurityscanner/
  202. youtube/
  203. youtubeanalytics/
  204. youtubereporting/
  205. .gitignore
  206. .hgtags
  207. api-list.json
  208. AUTHORS
  209. CHANGES.md
  210. CONTRIBUTING.md
  211. CONTRIBUTORS
  212. doc.go
  213. GettingStarted.md
  214. go.mod
  215. go.sum
  216. LICENSE
  217. license_test.go
  218. NOTES
  219. README.md
  220. RELEASING.md
  221. TODO
  222. tools.go
README.md

Google APIs Client Library for Go

Getting Started

$ go get google.golang.org/api/tasks/v1
$ go get google.golang.org/api/moderator/v1
$ go get google.golang.org/api/urlshortener/v1
... etc ...

and using:

package main

import (
	"net/http"

	"google.golang.org/api/urlshortener/v1"
)

func main() {
	svc, err := urlshortener.New(http.DefaultClient)
	// ...
}

Status

GoDoc

These are auto-generated Go libraries from the Google Discovery Service's JSON description files of the available “new style” Google APIs.

Due to the auto-generated nature of this collection of libraries, complete APIs or specific versions can appear or go away without notice. As a result, you should always locally vendor any API(s) that your code relies upon.

These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.

If you're working with Google Cloud Platform APIs such as Datastore or Pub/Sub, consider using the Cloud Client Libraries for Go instead. These are the new and idiomatic Go libraries targeted specifically at Google Cloud Platform Services.

The generator itself and the code it produces are beta. Some APIs are alpha/beta, and indicated as such in the import path (e.g., “google.golang.org/api/someapi/v1alpha”).

Application Default Credentials Example

Application Default Credentials provide a simplified way to obtain credentials for authenticating with Google APIs.

The Application Default Credentials authenticate as the application itself, which make them great for working with Google Cloud APIs like Storage or Datastore. They are the recommended form of authentication when building applications that run on Google Compute Engine or Google App Engine.

Default credentials are provided by the golang.org/x/oauth2/google package. To use them, add the following import:

import "golang.org/x/oauth2/google"

Some credentials types require you to specify scopes, and service entry points may not inject them. If you encounter this situation you may need to specify scopes as follows:

import (
        "context"
        "golang.org/x/oauth2/google"
        "google.golang.org/api/compute/v1"
)

func main() {
        // Use oauth2.NoContext if there isn't a good context to pass in.
        ctx := context.Background()

        client, err := google.DefaultClient(ctx, compute.ComputeScope)
        if err != nil {
                //...
        }
        computeService, err := compute.New(client)
        if err != nil {
                //...
        }
}

If you need a oauth2.TokenSource, use the DefaultTokenSource function:

ts, err := google.DefaultTokenSource(ctx, scope1, scope2, ...)
if err != nil {
        //...
}
client := oauth2.NewClient(ctx, ts)

See also: golang.org/x/oauth2/google package documentation.