hosts.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. var __read = (this && this.__read) || function (o, n) {
  38. var m = typeof Symbol === "function" && o[Symbol.iterator];
  39. if (!m) return o;
  40. var i = m.call(o), r, ar = [], e;
  41. try {
  42. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  43. }
  44. catch (error) { e = { error: error }; }
  45. finally {
  46. try {
  47. if (r && !r.done && (m = i["return"])) m.call(i);
  48. }
  49. finally { if (e) throw e.error; }
  50. }
  51. return ar;
  52. };
  53. var __spread = (this && this.__spread) || function () {
  54. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  55. return ar;
  56. };
  57. import { getUpHosts } from '../api';
  58. /**
  59. * @description 解冻时间,key 是 host,value 为解冻时间
  60. */
  61. var unfreezeTimeMap = new Map();
  62. var Host = /** @class */ (function () {
  63. function Host(host, protocol) {
  64. this.host = host;
  65. this.protocol = protocol;
  66. }
  67. /**
  68. * @description 当前 host 是否为冻结状态
  69. */
  70. Host.prototype.isFrozen = function () {
  71. var currentTime = new Date().getTime();
  72. var unfreezeTime = unfreezeTimeMap.get(this.host);
  73. return unfreezeTime != null && unfreezeTime >= currentTime;
  74. };
  75. /**
  76. * @param {number} time 单位秒,默认 20s
  77. * @description 冻结该 host 对象,该 host 将在指定时间内不可用
  78. */
  79. Host.prototype.freeze = function (time) {
  80. if (time === void 0) { time = 20; }
  81. var unfreezeTime = new Date().getTime() + (time * 1000);
  82. unfreezeTimeMap.set(this.host, unfreezeTime);
  83. };
  84. /**
  85. * @description 解冻该 host
  86. */
  87. Host.prototype.unfreeze = function () {
  88. unfreezeTimeMap["delete"](this.host);
  89. };
  90. /**
  91. * @description 获取当前 host 的完整 url
  92. */
  93. Host.prototype.getUrl = function () {
  94. return this.protocol + "://" + this.host;
  95. };
  96. /**
  97. * @description 获取解冻时间
  98. */
  99. Host.prototype.getUnfreezeTime = function () {
  100. return unfreezeTimeMap.get(this.host);
  101. };
  102. return Host;
  103. }());
  104. export { Host };
  105. var HostPool = /** @class */ (function () {
  106. /**
  107. * @param {string[]} initHosts
  108. * @description 如果在构造时传入 initHosts,则该 host 池始终使用传入的 initHosts 做为可用的数据
  109. */
  110. function HostPool(initHosts) {
  111. if (initHosts === void 0) { initHosts = []; }
  112. this.initHosts = initHosts;
  113. /**
  114. * @description 缓存的 host 表,以 bucket 和 accessKey 作为 key
  115. */
  116. this.cachedHostsMap = new Map();
  117. }
  118. /**
  119. * @param {string} accessKey
  120. * @param {string} bucketName
  121. * @param {string[]} hosts
  122. * @param {InternalConfig['upprotocol']} protocol
  123. * @returns {void}
  124. * @description 注册可用 host
  125. */
  126. HostPool.prototype.register = function (accessKey, bucketName, hosts, protocol) {
  127. this.cachedHostsMap.set(accessKey + "@" + bucketName, hosts.map(function (host) { return new Host(host, protocol); }));
  128. };
  129. /**
  130. * @param {string} accessKey
  131. * @param {string} bucketName
  132. * @param {InternalConfig['upprotocol']} protocol
  133. * @returns {Promise<void>}
  134. * @description 刷新最新的 host 数据,如果用户在构造时该类时传入了 host 或者已经存在缓存则不会发起请求
  135. */
  136. HostPool.prototype.refresh = function (accessKey, bucketName, protocol) {
  137. var _a, _b, _c, _d;
  138. return __awaiter(this, void 0, void 0, function () {
  139. var cachedHostList, response, stashHosts;
  140. return __generator(this, function (_e) {
  141. switch (_e.label) {
  142. case 0:
  143. cachedHostList = this.cachedHostsMap.get(accessKey + "@" + bucketName) || [];
  144. if (cachedHostList.length > 0)
  145. return [2 /*return*/];
  146. if (this.initHosts.length > 0) {
  147. this.register(accessKey, bucketName, this.initHosts, protocol);
  148. return [2 /*return*/];
  149. }
  150. return [4 /*yield*/, getUpHosts(accessKey, bucketName, protocol)];
  151. case 1:
  152. response = _e.sent();
  153. if ((response === null || response === void 0 ? void 0 : response.data) != null) {
  154. stashHosts = __spread((((_b = (_a = response.data.up) === null || _a === void 0 ? void 0 : _a.acc) === null || _b === void 0 ? void 0 : _b.main) || []), (((_d = (_c = response.data.up) === null || _c === void 0 ? void 0 : _c.acc) === null || _d === void 0 ? void 0 : _d.backup) || []));
  155. this.register(accessKey, bucketName, stashHosts, protocol);
  156. }
  157. return [2 /*return*/];
  158. }
  159. });
  160. });
  161. };
  162. /**
  163. * @param {string} accessKey
  164. * @param {string} bucketName
  165. * @param {InternalConfig['upprotocol']} protocol
  166. * @returns {Promise<Host | null>}
  167. * @description 获取一个可用的上传 Host,排除已冻结的
  168. */
  169. HostPool.prototype.getUp = function (accessKey, bucketName, protocol) {
  170. return __awaiter(this, void 0, void 0, function () {
  171. var cachedHostList, availableHostList, priorityQueue;
  172. return __generator(this, function (_a) {
  173. switch (_a.label) {
  174. case 0: return [4 /*yield*/, this.refresh(accessKey, bucketName, protocol)];
  175. case 1:
  176. _a.sent();
  177. cachedHostList = this.cachedHostsMap.get(accessKey + "@" + bucketName) || [];
  178. if (cachedHostList.length === 0)
  179. return [2 /*return*/, null];
  180. availableHostList = cachedHostList.filter(function (host) { return !host.isFrozen(); });
  181. if (availableHostList.length > 0)
  182. return [2 /*return*/, availableHostList[0]
  183. // 无可用的,去取离解冻最近的 host
  184. ];
  185. priorityQueue = cachedHostList
  186. .slice().sort(function (hostA, hostB) { return (hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0); });
  187. return [2 /*return*/, priorityQueue[0]];
  188. }
  189. });
  190. });
  191. };
  192. return HostPool;
  193. }());
  194. export { HostPool };
  195. //# sourceMappingURL=hosts.js.map