Skip to content

Commit c4cf605

Browse files
committed
fix: add default weight calculate
1 parent e1d1074 commit c4cf605

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/modules/scf/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,27 @@ class Scf {
406406
return res.Response;
407407
}
408408

409+
async getAlias(inputs) {
410+
const publishInputs = {
411+
Action: 'GetAlias',
412+
Version: '2018-04-16',
413+
Region: inputs.region,
414+
FunctionName: inputs.functionName,
415+
Name: inputs.functionVersion || '$DEFAULT',
416+
Namespace: inputs.namespace || 'default',
417+
};
418+
const res = await this.scfClient.request(publishInputs);
419+
if (res.Response && res.Response.Error) {
420+
throw new TypeError(
421+
'API_SCF_ListAliases',
422+
JSON.stringify(res.Response),
423+
null,
424+
res.Response.RequestId,
425+
);
426+
}
427+
return res.Response;
428+
}
429+
409430
// deploy SCF flow
410431
async deploy(inputs = {}) {
411432
// whether auto create/bind role
@@ -471,6 +492,31 @@ class Scf {
471492
outputs.ConfigTrafficVersion = inputs.lastVersion;
472493
}
473494

495+
// get default alias
496+
const defualtAlias = await this.getAlias({
497+
functionName: funcInfo.FunctionName,
498+
region: this.region,
499+
namespace,
500+
});
501+
if (
502+
defualtAlias &&
503+
defualtAlias.RoutingConfig &&
504+
defualtAlias.RoutingConfig.AdditionalVersionWeights
505+
) {
506+
const weights = defualtAlias.RoutingConfig.AdditionalVersionWeights;
507+
let weightSum = 0;
508+
let lastVersion = weights[0].Version;
509+
weights.forEach((w) => {
510+
if (Number(w.Version) > Number(outputs.LastVersion)) {
511+
lastVersion = w.Version;
512+
}
513+
weightSum += w.Weight;
514+
});
515+
outputs.LastVersion = lastVersion;
516+
outputs.ConfigTrafficVersion = lastVersion;
517+
outputs.Traffic = 1 - weightSum;
518+
}
519+
474520
if (inputs.tags || inputs.events) {
475521
if (!funcInfo) {
476522
funcInfo = await this.getFunction(namespace, inputs.name);

0 commit comments

Comments
 (0)