serverless-plugin-go 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cz.toml +1 -1
- package/CHANGELOG.md +5 -0
- package/README.md +18 -5
- package/index.js +14 -5
- package/index.test.js +55 -1
- package/package.json +2 -1
package/.cz.toml
CHANGED
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -42,11 +42,24 @@ Default values:
|
|
|
42
42
|
```yaml
|
|
43
43
|
custom:
|
|
44
44
|
go:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
# folder where go.mod file lives, if set `handler` property should be set relatively to that folder
|
|
46
|
+
baseDir: '.'
|
|
47
|
+
|
|
48
|
+
# Final target destination of the binary outputs
|
|
49
|
+
binDir: '.bin'
|
|
50
|
+
|
|
51
|
+
# Compile command that should be used to build binaries
|
|
52
|
+
cmd: 'go build -ldflags="-s -w"'
|
|
53
|
+
|
|
54
|
+
# If enabled, builds function every directory (useful for monorepo where go.mod is managed by each function
|
|
55
|
+
monorepo: false
|
|
56
|
+
|
|
57
|
+
# Number of concurrent process used to compile functions, you can fine tune this number to your needs.
|
|
58
|
+
# Alternatively you can use the env var `SP_GO_CONCURRENCY` to override this configuration
|
|
59
|
+
concurrency: 5
|
|
60
|
+
|
|
61
|
+
# Environment variables used to compile binaries
|
|
62
|
+
env:
|
|
50
63
|
GOOS: 'linux' # Default compile OS
|
|
51
64
|
CGO_ENABLED: '0' # By default CGO is disabled
|
|
52
65
|
```
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import cp from "child_process";
|
|
|
3
3
|
import prettyHrtime from "pretty-hrtime";
|
|
4
4
|
import chalk from "chalk";
|
|
5
5
|
import path from "path";
|
|
6
|
+
import plimit from "p-limit";
|
|
6
7
|
import AdmZip from "adm-zip";
|
|
7
8
|
import { readFileSync } from "fs";
|
|
8
9
|
|
|
@@ -29,8 +30,11 @@ export default class Go {
|
|
|
29
30
|
},
|
|
30
31
|
cmd: 'go build -ldflags="-s -w"',
|
|
31
32
|
monorepo: false,
|
|
33
|
+
concurrency: 5,
|
|
32
34
|
};
|
|
33
35
|
|
|
36
|
+
this.config = this.getConfig();
|
|
37
|
+
|
|
34
38
|
this.hooks = {
|
|
35
39
|
"before:deploy:function:packageFunction": this.compileFunction.bind(this),
|
|
36
40
|
"before:package:createDeploymentArtifacts":
|
|
@@ -81,12 +85,14 @@ export default class Go {
|
|
|
81
85
|
|
|
82
86
|
const timeStart = process.hrtime();
|
|
83
87
|
|
|
84
|
-
|
|
88
|
+
this.logDebug(`using concurrency limit ${this.config.concurrency}...`);
|
|
89
|
+
|
|
90
|
+
const limit = plimit(this.config.concurrency);
|
|
85
91
|
|
|
86
|
-
|
|
92
|
+
let compiles = names.map((funcName) => {
|
|
87
93
|
const func = this.serverless.service.functions[funcName];
|
|
88
|
-
|
|
89
|
-
}
|
|
94
|
+
return limit(() => this.compile(funcName, func));
|
|
95
|
+
});
|
|
90
96
|
|
|
91
97
|
await Promise.all(compiles);
|
|
92
98
|
|
|
@@ -122,7 +128,7 @@ export default class Go {
|
|
|
122
128
|
* @throws {Error} if compilation command fails
|
|
123
129
|
*/
|
|
124
130
|
async compile(name, func, artifact = true) {
|
|
125
|
-
const config = this.
|
|
131
|
+
const config = this.config;
|
|
126
132
|
const arch = this.getFunctionArch(name);
|
|
127
133
|
const runtime = this.getFunctionRuntime(name);
|
|
128
134
|
|
|
@@ -240,6 +246,9 @@ export default class Go {
|
|
|
240
246
|
config.env[env] = `${config.env[env]}`;
|
|
241
247
|
}
|
|
242
248
|
|
|
249
|
+
config.concurrency =
|
|
250
|
+
parseInt(process.env.SP_GO_CONCURRENCY) || config.concurrency;
|
|
251
|
+
|
|
243
252
|
return config;
|
|
244
253
|
}
|
|
245
254
|
|
package/index.test.js
CHANGED
|
@@ -324,7 +324,7 @@ describe("Go Plugin", () => {
|
|
|
324
324
|
);
|
|
325
325
|
});
|
|
326
326
|
|
|
327
|
-
it("
|
|
327
|
+
it("Compiles Go functions", async () => {
|
|
328
328
|
config.service.setFunctions({
|
|
329
329
|
testFunc1: {
|
|
330
330
|
name: "testFunc1",
|
|
@@ -346,4 +346,58 @@ describe("Go Plugin", () => {
|
|
|
346
346
|
`.bin/testFunc1`,
|
|
347
347
|
);
|
|
348
348
|
});
|
|
349
|
+
|
|
350
|
+
it("Should get concurrency config", async () => {
|
|
351
|
+
config.service.setFunctions({
|
|
352
|
+
testFunc1: {
|
|
353
|
+
name: "testFunc1",
|
|
354
|
+
runtime: "provided.al2",
|
|
355
|
+
handler: "functions/func2/main.go",
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
const plugin = new Go(config);
|
|
360
|
+
|
|
361
|
+
expect(plugin.config.concurrency).to.be.equal(
|
|
362
|
+
plugin.defaultConfig.concurrency,
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("Should get concurrency from env vars", async () => {
|
|
367
|
+
config.service.setFunctions({
|
|
368
|
+
testFunc1: {
|
|
369
|
+
name: "testFunc1",
|
|
370
|
+
runtime: "provided.al2",
|
|
371
|
+
handler: "functions/func2/main.go",
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
process.env["SP_GO_CONCURRENCY"] = "10";
|
|
376
|
+
|
|
377
|
+
const plugin = new Go(config);
|
|
378
|
+
|
|
379
|
+
expect(plugin.config.concurrency).to.be.equal(10);
|
|
380
|
+
|
|
381
|
+
delete process.env["SP_GO_CONCURRENCY"];
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("Should get default concurrency if env var can't be parsed", async () => {
|
|
385
|
+
config.service.setFunctions({
|
|
386
|
+
testFunc1: {
|
|
387
|
+
name: "testFunc1",
|
|
388
|
+
runtime: "provided.al2",
|
|
389
|
+
handler: "functions/func2/main.go",
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
process.env["SP_GO_CONCURRENCY"] = "something";
|
|
394
|
+
|
|
395
|
+
const plugin = new Go(config);
|
|
396
|
+
|
|
397
|
+
expect(plugin.config.concurrency).to.be.equal(
|
|
398
|
+
plugin.defaultConfig.concurrency,
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
delete process.env["SP_GO_CONCURRENCY"];
|
|
402
|
+
});
|
|
349
403
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serverless-plugin-go",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Serverless Framework plugin that compiles Go functions on the fly",
|
|
6
6
|
"main": "index.js",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"adm-zip": "^0.5.10",
|
|
43
43
|
"chalk": "^5.3.0",
|
|
44
|
+
"p-limit": "^5.0.0",
|
|
44
45
|
"pretty-hrtime": "^1.0.3",
|
|
45
46
|
"util": "^0.12.5"
|
|
46
47
|
},
|