Skip to content

Commit 71e82ba

Browse files
author
Kai Mast
authored
Add lint-go target and fix some lint warnings (many more to go) (open-lambda#136)
1 parent fe61328 commit 71e82ba

19 files changed

+262
-217
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ check-fmt:
8989
cd wasm-worker && cargo fmt --check
9090
cd bin-functions && cargo fmt --check
9191

92-
lint:
92+
lint-go:
93+
revive -exclude src/vendor/... -config golint.toml src/...
94+
95+
lint: #go-lint
9396
pylint scripts --ignore=build --disable=missing-docstring,multiple-imports,global-statement,invalid-name,W0511,W1510,R0801,W3101
9497
cd wasm-worker && cargo clippy
9598
cd bin-functions && cargo clippy

golint.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
enableAllRules = true
2+
3+
#TODO we should eventually comment all public methods
4+
[rule.package-comments]
5+
Disabled = true
6+
7+
#TODO get rid of all (or most) magic numbers
8+
[rule.add-constant]
9+
Disabled = true
10+
11+
#TODO we should handle all errors
12+
[rule.unhandled-error]
13+
Disabled = true
14+
15+
[rule.exported]
16+
Disabled = true
17+
[rule.early-return]
18+
Disabled = true
19+
[rule.deep-exit]
20+
Disabled = true
21+
[rule.function-length]
22+
Disabled = true
23+
[rule.file-header]
24+
Disabled = true
25+
[rule.banned-characters]
26+
Disabled = true
27+
[rule.max-public-structs]
28+
Disabled = true
29+
[rule.line-length-limit]
30+
Disabled = true
31+
[rule.cognitive-complexity]
32+
Disabled = true
33+
[rule.increment-decrement]
34+
Disabled = true
35+
36+
[rule.argument-limit]
37+
Arguments = [7]
38+
[rule.cyclomatic]
39+
Arguments = [10]
40+
[rule.function-result-limit]
41+
Arguments = [3]

src/boss/boss.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (b *Boss) StorageLambda(w http.ResponseWriter, r *http.Request) {
132132
Create(string(contents))
133133
}
134134

135-
func (b *Boss) DownloadLambda(w http.ResponseWriter, r *http.Request) {
135+
func (*Boss) DownloadLambda(w http.ResponseWriter, r *http.Request) {
136136
// contents, err := io.ReadAll(r.Body)
137137
if err != nil {
138138
w.WriteHeader(http.StatusInternalServerError)
@@ -145,7 +145,7 @@ func (b *Boss) DownloadLambda(w http.ResponseWriter, r *http.Request) {
145145
Download()
146146
}
147147

148-
func (b *Boss) DeleteLambda(w http.ResponseWriter, r *http.Request) {
148+
func (*Boss) DeleteLambda(w http.ResponseWriter, r *http.Request) {
149149
// contents, err := io.ReadAll(r.Body)
150150
if err != nil {
151151
w.WriteHeader(http.StatusInternalServerError)

src/boss/gcp.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
type GCPClient struct {
23-
service_account map[string]interface{} // from .json key exported from GCP service account
23+
service_account map[string]any // from .json key exported from GCP service account
2424
access_token string
2525
}
2626

@@ -195,7 +195,7 @@ func (c *GCPClient) GetAccessToken() (string, error) {
195195
return "", err
196196
}
197197

198-
var result map[string]interface{}
198+
var result map[string]any
199199
if err := json.Unmarshal([]byte(body), &result); err != nil {
200200
return "", err
201201
}
@@ -204,8 +204,8 @@ func (c *GCPClient) GetAccessToken() (string, error) {
204204
return c.access_token, nil
205205
}
206206

207-
func (c *GCPClient) get(url string) (rv map[string]interface{}, err error) {
208-
var result map[string]interface{}
207+
func (c *GCPClient) get(url string) (rv map[string]any, err error) {
208+
var result map[string]any
209209

210210
defer func() {
211211
if err != nil {
@@ -236,8 +236,8 @@ func (c *GCPClient) get(url string) (rv map[string]interface{}, err error) {
236236
return result, nil
237237
}
238238

239-
func (c *GCPClient) post(url string, payload bytes.Buffer) (rv map[string]interface{}, err error) {
240-
var result map[string]interface{}
239+
func (c *GCPClient) post(url string, payload bytes.Buffer) (rv map[string]any, err error) {
240+
var result map[string]any
241241

242242
defer func() {
243243
if err != nil {
@@ -268,7 +268,7 @@ func (c *GCPClient) post(url string, payload bytes.Buffer) (rv map[string]interf
268268
return result, nil
269269
}
270270

271-
func (c *GCPClient) GcpListInstances() (map[string]interface{}, error) {
271+
func (c *GCPClient) GcpListInstances() (map[string]any, error) {
272272
return c.get("https://www.googleapis.com/compute/v1/projects/cs320-f21/zones/us-central1-a/instances")
273273
}
274274

@@ -280,16 +280,16 @@ func (c *GCPClient) GcpIPtoInstance() (map[string]string, error) {
280280

281281
lookup := map[string]string{}
282282

283-
for _, item := range resp["items"].([]interface{}) {
284-
instance_name := item.(map[string]interface{})["name"].(string)
285-
interfaces := item.(map[string]interface{})["networkInterfaces"]
286-
for _, netif := range interfaces.([]interface{}) {
287-
ip := netif.(map[string]interface{})["networkIP"].(string)
283+
for _, item := range resp["items"].([]any) {
284+
instance_name := item.(map[string]any)["name"].(string)
285+
interfaces := item.(map[string]any)["networkInterfaces"]
286+
for _, netif := range interfaces.([]any) {
287+
ip := netif.(map[string]any)["networkIP"].(string)
288288
lookup[ip] = instance_name
289289

290-
confs := netif.(map[string]interface{})["accessConfigs"]
291-
for _, conf := range confs.([]interface{}) {
292-
iptmp := conf.(map[string]interface{})["natIP"]
290+
confs := netif.(map[string]any)["accessConfigs"]
291+
for _, conf := range confs.([]any) {
292+
iptmp := conf.(map[string]any)["natIP"]
293293
switch ip := iptmp.(type) {
294294
case string:
295295
lookup[ip] = instance_name
@@ -345,7 +345,7 @@ func (c *GCPClient) GcpInstanceName() (string, error) {
345345
return instance, nil
346346
}
347347

348-
func (c *GCPClient) Wait(resp1 map[string]interface{}, err1 error) (resp2 map[string]interface{}, err2 error) {
348+
func (c *GCPClient) Wait(resp1 map[string]any, err1 error) (resp2 map[string]any, err2 error) {
349349
if err1 != nil {
350350
return nil, fmt.Errorf("cannot Wait on on failed call: %s", err1.Error())
351351
}
@@ -376,7 +376,7 @@ func (c *GCPClient) Wait(resp1 map[string]interface{}, err1 error) (resp2 map[st
376376
return resp2, fmt.Errorf("Wait: operation timed out")
377377
}
378378

379-
func (c *GCPClient) GcpSnapshot(disk string) (map[string]interface{}, error) {
379+
func (c *GCPClient) GcpSnapshot(disk string) (map[string]any, error) {
380380
// TODO: take args from config (or better, read from service account somehow)
381381
args := GcpSnapshotArgs{
382382
Project: "cs320-f21",
@@ -398,7 +398,7 @@ func (c *GCPClient) GcpSnapshot(disk string) (map[string]interface{}, error) {
398398
return c.post(url, payload)
399399
}
400400

401-
func (c *GCPClient) LaunchGCP(SnapshotName string, VMName string) (map[string]interface{}, error) {
401+
func (c *GCPClient) LaunchGCP(SnapshotName string, VMName string) (map[string]any, error) {
402402
// TODO: take args from config (or better, read from service account somehow)
403403
args := GcpLaunchVmArgs{
404404
ServiceAccountEmail: c.service_account["client_email"].(string),

src/common/config.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"path/filepath"
1010
"syscall"
1111

12-
"github.com/urfave/cli"
12+
"github.com/urfave/cli"
1313
)
1414

1515
// Configuration is stored globally here
@@ -53,13 +53,13 @@ type Config struct {
5353

5454
// can be empty (use root zygote only), a JSON obj (specifying
5555
// the tree), or a path (to a file specifying the tree)
56-
Import_cache_tree interface{} `json:"import_cache_tree"`
56+
Import_cache_tree any `json:"import_cache_tree"`
5757

5858
// base image path for sock containers
5959
SOCK_base_path string `json:"sock_base_path"`
6060

6161
// pass through to sandbox envirenment variable
62-
Sandbox_config interface{} `json:"sandbox_config"`
62+
Sandbox_config any `json:"sandbox_config"`
6363

6464
// which OCI implementation to use for the docker sandbox (e.g., runc or runsc)
6565
Docker_runtime string `json:"docker_runtime"`
@@ -142,8 +142,8 @@ func LoadDefaults(olPath string) error {
142142
if err != nil {
143143
return err
144144
}
145-
total_mb := uint64(in.Totalram) * uint64(in.Unit) / 1024 / 1024
146-
mem_pool_mb := Max(int(total_mb-500), 500)
145+
totalMb := uint64(in.Totalram) * uint64(in.Unit) / 1024 / 1024
146+
memPoolMb := Max(int(totalMb-500), 500)
147147

148148
Conf = &Config{
149149
Worker_dir: workerDir,
@@ -154,17 +154,17 @@ func LoadDefaults(olPath string) error {
154154
Sandbox: "sock",
155155
Log_output: true,
156156
Pkgs_dir: packagesDir,
157-
Sandbox_config: map[string]interface{}{},
157+
Sandbox_config: map[string]any{},
158158
SOCK_base_path: baseImgDir,
159159
Registry_cache_ms: 5000, // 5 seconds
160-
Mem_pool_mb: mem_pool_mb,
160+
Mem_pool_mb: memPoolMb,
161161
Import_cache_tree: "",
162162
Limits: LimitsConfig{
163163
Procs: 10,
164164
Mem_mb: 50,
165165
CPU_percent: 100,
166166
Max_runtime_default: 30,
167-
Installer_mem_mb: Max(250, Min(500, mem_pool_mb/2)),
167+
Installer_mem_mb: Max(250, Min(500, memPoolMb/2)),
168168
Swappiness: 0,
169169
},
170170
Features: FeaturesConfig{
@@ -191,14 +191,14 @@ func LoadDefaults(olPath string) error {
191191
// ParseConfig reads a file and tries to parse it as a JSON string to a Config
192192
// instance.
193193
func LoadConf(path string) error {
194-
config_raw, err := ioutil.ReadFile(path)
194+
configRaw, err := ioutil.ReadFile(path)
195195
if err != nil {
196-
return fmt.Errorf("could not open config (%v): %v\n", path, err.Error())
196+
return fmt.Errorf("could not open config (%v): %v", path, err.Error())
197197
}
198198

199-
if err := json.Unmarshal(config_raw, &Conf); err != nil {
200-
log.Printf("FILE: %v\n", string(config_raw))
201-
return fmt.Errorf("could not parse config (%v): %v\n", path, err.Error())
199+
if err := json.Unmarshal(configRaw, &Conf); err != nil {
200+
log.Printf("FILE: %v\n", string(configRaw))
201+
return fmt.Errorf("could not parse config (%v): %v", path, err.Error())
202202
}
203203

204204
return checkConf()
@@ -225,9 +225,9 @@ func checkConf() error {
225225
// evicted.
226226
//
227227
// TODO: revise evictor and relax this
228-
min_mem := 2 * Max(Conf.Limits.Installer_mem_mb, Conf.Limits.Mem_mb)
229-
if min_mem > Conf.Mem_pool_mb {
230-
return fmt.Errorf("mem_pool_mb must be at least %d", min_mem)
228+
minMem := 2 * Max(Conf.Limits.Installer_mem_mb, Conf.Limits.Mem_mb)
229+
if minMem > Conf.Mem_pool_mb {
230+
return fmt.Errorf("memPoolMb must be at least %d", minMem)
231231
}
232232
} else if Conf.Sandbox == "docker" {
233233
if Conf.Pkgs_dir == "" {

src/common/stats.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type snapshotMsg struct {
5050
}
5151

5252
var initOnce sync.Once
53-
var statsChan chan interface{} = make(chan interface{}, 256)
53+
var statsChan chan any = make(chan any, 256)
5454

5555
func initTaskOnce() {
5656
initOnce.Do(func() {
@@ -148,15 +148,15 @@ func GetGoroutineID() uint64 {
148148
func Max(x int, y int) int {
149149
if x > y {
150150
return x
151-
} else {
152-
return y
153151
}
152+
153+
return y
154154
}
155155

156156
func Min(x int, y int) int {
157157
if x < y {
158158
return x
159-
} else {
160-
return y
161159
}
160+
161+
return y
162162
}

src/lambda/depTracer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type DepTracer struct {
1010
file *os.File
1111
writer *bufio.Writer
12-
events chan map[string]interface{}
12+
events chan map[string]any
1313
done chan bool
1414
}
1515

@@ -22,7 +22,7 @@ func NewDepTracer(logPath string) (*DepTracer, error) {
2222
t := &DepTracer{
2323
file: file,
2424
writer: bufio.NewWriter(file),
25-
events: make(chan map[string]interface{}, 128),
25+
events: make(chan map[string]any, 128),
2626
done: make(chan bool),
2727
}
2828
go t.run()
@@ -56,7 +56,7 @@ func (t *DepTracer) Cleanup() {
5656
}
5757

5858
func (t *DepTracer) TracePackage(p *Package) {
59-
t.events <- map[string]interface{}{
59+
t.events <- map[string]any{
6060
"type": "package",
6161
"name": p.name,
6262
"deps": p.meta.Deps,
@@ -65,15 +65,15 @@ func (t *DepTracer) TracePackage(p *Package) {
6565
}
6666

6767
func (t *DepTracer) TraceFunction(codeDir string, directDeps []string) {
68-
t.events <- map[string]interface{}{
68+
t.events <- map[string]any{
6969
"type": "function",
7070
"name": codeDir,
7171
"deps": directDeps,
7272
}
7373
}
7474

7575
func (t *DepTracer) TraceInvocation(codeDir string) {
76-
t.events <- map[string]interface{}{
76+
t.events <- map[string]any{
7777
"type": "invocation",
7878
"name": codeDir,
7979
}

src/lambda/importCache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewImportCache(codeDirs *common.DirMaker, scratchDirs *common.DirMaker, sbP
9090
return nil, fmt.Errorf("could parse import tree file (%v): %v\n", treeConf, err.Error())
9191
}
9292
}
93-
case map[string]interface{}:
93+
case map[string]any:
9494
b, err := json.Marshal(treeConf)
9595
if err != nil {
9696
return nil, err
@@ -245,7 +245,7 @@ func (cache *ImportCache) getSandboxInNode(node *ImportCacheNode, forceNew bool,
245245
}
246246

247247
// decrease refs to SB, pausing if nobody else is still using it
248-
func (cache *ImportCache) putSandboxInNode(node *ImportCacheNode, sb sandbox.Sandbox) {
248+
func (*ImportCache) putSandboxInNode(node *ImportCacheNode, sb sandbox.Sandbox) {
249249
t := common.T0("ImportCache.putSandboxInNode")
250250
defer t.T1()
251251

src/lambda/lambdaFunction.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (f *LambdaFunc) Invoke(w http.ResponseWriter, r *http.Request) {
5959

6060
// add function name to each log message so we know which logs
6161
// correspond to which LambdaFuncs
62-
func (f *LambdaFunc) printf(format string, args ...interface{}) {
62+
func (f *LambdaFunc) printf(format string, args ...any) {
6363
msg := fmt.Sprintf(format, args...)
6464
log.Printf("%s [FUNC %s]", strings.TrimRight(msg, "\n"), f.name)
6565
}
@@ -217,7 +217,7 @@ func (f *LambdaFunc) Task() {
217217
// previously initiated cleanup work. We block until we
218218
// receive the complete signal, before proceeding to
219219
// subsequent cleanup tasks in the FIFO.
220-
cleanupChan := make(chan interface{}, 32)
220+
cleanupChan := make(chan any, 32)
221221
cleanupTaskDone := make(chan bool)
222222
go func() {
223223
for {

0 commit comments

Comments
 (0)