pool.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*!
  2. * Copyright 2018 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. export declare const CLIENT_TERMINATED_ERROR_MSG = "The client has already been terminated";
  17. /**
  18. * An auto-resizing pool that distributes concurrent operations over multiple
  19. * clients of type `T`.
  20. *
  21. * ClientPool is used within Firestore to manage a pool of GAPIC clients and
  22. * automatically initializes multiple clients if we issue more than 100
  23. * concurrent operations.
  24. *
  25. * @private
  26. * @internal
  27. */
  28. export declare class ClientPool<T> {
  29. private readonly concurrentOperationLimit;
  30. private readonly maxIdleClients;
  31. private readonly clientFactory;
  32. private readonly clientDestructor;
  33. private grpcEnabled;
  34. /**
  35. * Stores each active clients and how many operations it has outstanding.
  36. */
  37. private activeClients;
  38. /**
  39. * A set of clients that have seen RST_STREAM errors (see
  40. * https://github.com/googleapis/nodejs-firestore/issues/1023) and should
  41. * no longer be used.
  42. */
  43. private failedClients;
  44. /**
  45. * Whether the Firestore instance has been terminated. Once terminated, the
  46. * ClientPool can longer schedule new operations.
  47. */
  48. private terminated;
  49. /**
  50. * Deferred promise that is resolved when there are no active operations on
  51. * the client pool after terminate() has been called.
  52. */
  53. private terminateDeferred;
  54. /**
  55. * @param concurrentOperationLimit The number of operations that each client
  56. * can handle.
  57. * @param maxIdleClients The maximum number of idle clients to keep before
  58. * garbage collecting.
  59. * @param clientFactory A factory function called as needed when new clients
  60. * are required.
  61. * @param clientDestructor A cleanup function that is called when a client is
  62. * disposed of.
  63. */
  64. constructor(concurrentOperationLimit: number, maxIdleClients: number, clientFactory: (requiresGrpc: boolean) => T, clientDestructor?: (client: T) => Promise<void>);
  65. /**
  66. * Returns an already existing client if it has less than the maximum number
  67. * of concurrent operations or initializes and returns a new client.
  68. *
  69. * @private
  70. * @internal
  71. */
  72. private acquire;
  73. /**
  74. * Reduces the number of operations for the provided client, potentially
  75. * removing it from the pool of active clients.
  76. * @private
  77. * @internal
  78. */
  79. private release;
  80. /**
  81. * Given the current operation counts, determines if the given client should
  82. * be garbage collected.
  83. * @private
  84. * @internal
  85. */
  86. private shouldGarbageCollectClient;
  87. /**
  88. * The number of currently registered clients.
  89. *
  90. * @return Number of currently registered clients.
  91. * @private
  92. * @internal
  93. */
  94. get size(): number;
  95. /**
  96. * The number of currently active operations.
  97. *
  98. * @return Number of currently active operations.
  99. * @private
  100. * @internal
  101. */
  102. get opCount(): number;
  103. /**
  104. * The currently active clients.
  105. *
  106. * @return The currently active clients.
  107. * @private
  108. * @internal
  109. */
  110. get _activeClients(): Map<T, {
  111. activeRequestCount: number;
  112. grpcEnabled: boolean;
  113. }>;
  114. /**
  115. * Runs the provided operation in this pool. This function may create an
  116. * additional client if all existing clients already operate at the concurrent
  117. * operation limit.
  118. *
  119. * @param requestTag A unique client-assigned identifier for this operation.
  120. * @param op A callback function that returns a Promise. The client T will
  121. * be returned to the pool when callback finishes.
  122. * @return A Promise that resolves with the result of `op`.
  123. * @private
  124. * @internal
  125. */
  126. run<V>(requestTag: string, requiresGrpc: boolean, op: (client: T) => Promise<V>): Promise<V>;
  127. terminate(): Promise<void>;
  128. }