Skip to content

Commit 664c4a1

Browse files
Merge pull request #2 from enverbisevac/main
Code refactored to use linters, sec, vet and other go tools
2 parents 6c0f496 + a2303ae commit 664c4a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+626
-434
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI workflow
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.16.2
20+
21+
- name: Check
22+
run: make check
23+
24+
- name: Test
25+
run: make test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ format:
3232
@echo "Checking that go fmt does not make any changes..."
3333
@test -z $$(go fmt $(go list ./...)) || (echo "go fmt would make a change. Please verify and commit the proposed changes"; exit 1)
3434
@echo "Checking go fmt complete"
35+
@echo "Running goimports"
36+
@test -z $$(goimports -w ./..) || (echo "goimports would make a change. Please verify and commit the proposed changes"; exit 1)
3537

3638
PHONY+= lint
3739
lint: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint
@@ -43,10 +45,8 @@ lint: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint
4345
-E bodyclose \
4446
-E exhaustive \
4547
-E exportloopref \
46-
-E gci \
4748
-E gofmt \
4849
-E goimports \
49-
-E goimports \
5050
-E gosec \
5151
-E noctx \
5252
-E nolintlint \
@@ -75,21 +75,27 @@ $(GOPATH)/bin/golint:
7575
@echo "🔘 Installing golint ... (`date '+%H:%M:%S'`)"
7676
@GO111MODULE=off go get -u golang.org/x/lint/golint
7777

78+
$(GOPATH)/bin/goimports:
79+
@echo "🔘 Installing goimports ... (`date '+%H:%M:%S'`)"
80+
@GO111MODULE=off go get -u golang.org/x/tools/cmd/goimports
81+
7882
$(GOPATH)/bin/gosec:
7983
@echo "🔘 Installing gosec ... (`date '+%H:%M:%S'`)"
8084
@curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(GOPATH)/bin
8185

8286

8387
PHONY+= tools
84-
tools: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec
88+
tools: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec $(GOPATH)/bin/goimports
8589

8690
PHONY+= update-tools
87-
update-tools: delete-tools $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec
91+
update-tools: delete-tools $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec $(GOPATH)/bin/goimports
8892

8993
PHONY+= delete-tools
9094
delete-tools:
9195
@rm $(GOPATH)/bin/golangci-lint
9296
@rm $(GOPATH)/bin/gosec
9397
@rm $(GOPATH)/bin/golint
98+
@rm $(GOPATH)/bin/goimports
99+
94100

95101
.PHONY: all tidy generate build clean test lint

cache/cache.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package cache
22

33
import (
4-
"github.com/drone/ff-golang-server-sdk/logger"
4+
"github.com/drone/ff-golang-server-sdk.v1/logger"
5+
56
"time"
67
)
78

9+
// Cache wrapper to integrate any 3rd party implementation
810
type Cache interface {
911
Set(key, value interface{}) (evicted bool)
1012
Contains(key interface{}) bool
11-
ContainsOrAdd(key, value interface{}) (ok, evicted bool)
1213
Get(key interface{}) (value interface{}, ok bool)
13-
GetOldest() (key, value interface{}, ok bool)
1414
Keys() []interface{}
1515
Len() int
16-
Peek(key interface{}) (value interface{}, ok bool)
17-
PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool)
1816
Purge()
1917
Remove(key interface{}) (present bool)
20-
RemoveOldest() (key, value interface{}, ok bool)
2118
Resize(size int) (evicted int)
2219
Updated() time.Time
2320
SetLogger(logger logger.Logger)

cache/lru.go

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,98 @@
11
package cache
22

33
import (
4-
"github.com/drone/ff-golang-server-sdk/logger"
4+
"github.com/drone/ff-golang-server-sdk.v1/logger"
55
lru "github.com/hashicorp/golang-lru"
6+
67
"reflect"
78
"time"
89
)
910

10-
type lruCache struct {
11+
// LRUCache is thread-safe LAST READ USED Cache
12+
type LRUCache struct {
1113
*lru.Cache
1214
logger logger.Logger
1315
lastUpdate time.Time
1416
}
1517

16-
func NewLruCache(size int, logger logger.Logger) (*lruCache, error) {
18+
//NewLruCache creates a new LRU instance
19+
func NewLruCache(size int, logger logger.Logger) (*LRUCache, error) {
1720
cache, err := lru.New(size)
1821
if err != nil {
1922
logger.Errorf("Error initializing LRU cache, err: %v", err)
2023
return nil, err
2124
}
2225
logger.Infof("Cache successfully initialized with size: %d", size)
23-
return &lruCache{
26+
return &LRUCache{
2427
Cache: cache,
2528
logger: logger,
2629
}, nil
2730
}
2831

29-
func (lru *lruCache) getTime() time.Time {
32+
func (lru *LRUCache) getTime() time.Time {
3033
return time.Now()
3134
}
3235

33-
func (lru *lruCache) Set(key interface{}, value interface{}) (evicted bool) {
36+
// Set a new value if it is different from the previous one.
37+
// Returns true if an eviction occurred.
38+
func (lru *LRUCache) Set(key interface{}, value interface{}) (evicted bool) {
3439
prev, _ := lru.Get(key)
3540
if !reflect.DeepEqual(prev, value) {
3641
add := lru.Cache.Add(key, value)
3742
lru.lastUpdate = lru.getTime()
38-
lru.logger.Infof("cache value changed for key %s with value %v", key, value)
43+
lru.logger.Debugf("cache value changed for key %s with value %v", key, value)
3944
return add
4045
}
4146
return false
4247
}
4348

44-
func (lru *lruCache) Contains(key interface{}) bool {
49+
// Contains checks if a key is in the cache
50+
func (lru *LRUCache) Contains(key interface{}) bool {
4551
return lru.Cache.Contains(key)
4652
}
4753

48-
func (lru *lruCache) ContainsOrAdd(key interface{}, value interface{}) (ok bool, evicted bool) {
49-
ok, evicted = lru.Cache.ContainsOrAdd(key, value)
50-
lru.lastUpdate = lru.getTime()
51-
return
52-
}
53-
54-
func (lru *lruCache) Get(key interface{}) (value interface{}, ok bool) {
54+
// Get looks up a key's value from the cache.
55+
func (lru *LRUCache) Get(key interface{}) (value interface{}, ok bool) {
5556
return lru.Cache.Get(key)
5657
}
5758

58-
func (lru *lruCache) GetOldest() (key interface{}, value interface{}, ok bool) {
59-
return lru.Cache.GetOldest()
60-
}
61-
62-
func (lru *lruCache) Keys() []interface{} {
59+
// Keys returns a slice of the keys in the cache, from oldest to newest.
60+
func (lru *LRUCache) Keys() []interface{} {
6361
return lru.Cache.Keys()
6462
}
6563

66-
func (lru *lruCache) Len() int {
64+
// Len returns the number of items in the cache.
65+
func (lru *LRUCache) Len() int {
6766
return lru.Cache.Len()
6867
}
6968

70-
func (lru *lruCache) Peek(key interface{}) (value interface{}, ok bool) {
71-
return lru.Cache.Peek(key)
72-
}
73-
74-
func (lru *lruCache) PeekOrAdd(key interface{}, value interface{}) (previous interface{}, ok bool, evicted bool) {
75-
previous, ok, evicted = lru.Cache.PeekOrAdd(key, value)
76-
lru.lastUpdate = lru.getTime()
77-
return
78-
}
79-
80-
func (lru *lruCache) Purge() {
69+
// Purge is used to completely clear the cache.
70+
func (lru *LRUCache) Purge() {
8171
lru.Cache.Purge()
8272
lru.lastUpdate = lru.getTime()
8373
}
8474

85-
func (lru *lruCache) Remove(key interface{}) (present bool) {
75+
// Remove removes the provided key from the cache.
76+
func (lru *LRUCache) Remove(key interface{}) (present bool) {
8677
present = lru.Cache.Remove(key)
8778
lru.lastUpdate = lru.getTime()
8879
if present {
89-
lru.logger.Infof("Cache item successfully removed %v", key)
90-
}
91-
return
92-
}
93-
94-
func (lru *lruCache) RemoveOldest() (key interface{}, value interface{}, ok bool) {
95-
key, value, ok = lru.Cache.RemoveOldest()
96-
lru.lastUpdate = lru.getTime()
97-
if ok {
98-
lru.logger.Infof("Cache oldest item successfully removed %v with value %v", key, value)
80+
lru.logger.Debugf("Cache item successfully removed %v", key)
9981
}
10082
return
10183
}
10284

103-
func (lru *lruCache) Resize(size int) (evicted int) {
85+
// Resize changes the cache size.
86+
func (lru *LRUCache) Resize(size int) (evicted int) {
10487
return lru.Cache.Resize(size)
10588
}
10689

107-
func (lru *lruCache) Updated() time.Time {
90+
// Updated lastUpdate information
91+
func (lru *LRUCache) Updated() time.Time {
10892
return lru.lastUpdate
10993
}
11094

111-
func (lru lruCache) SetLogger(logger logger.Logger) {
95+
// SetLogger set logger
96+
func (lru LRUCache) SetLogger(logger logger.Logger) {
11297
lru.logger = logger
11398
}

cache/persist.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package cache
22

33
import (
4-
"github.com/drone/ff-golang-server-sdk/dto"
5-
"github.com/drone/ff-golang-server-sdk/evaluation"
6-
"github.com/drone/ff-golang-server-sdk/logger"
7-
"github.com/drone/ff-golang-server-sdk/storage"
4+
"github.com/drone/ff-golang-server-sdk.v1/dto"
5+
"github.com/drone/ff-golang-server-sdk.v1/evaluation"
6+
"github.com/drone/ff-golang-server-sdk.v1/logger"
7+
"github.com/drone/ff-golang-server-sdk.v1/storage"
88
"github.com/mitchellh/mapstructure"
99
)
1010

11+
// Persistence persist cache data to a storage
1112
type Persistence struct {
1213
store storage.Storage
1314
cache Cache
1415
logger logger.Logger
1516
}
1617

18+
// NewPersistence creates a new instance for persisting data
1719
func NewPersistence(store storage.Storage, cache Cache, logger logger.Logger) Persistence {
1820
return Persistence{store: store, cache: cache, logger: logger}
1921
}
2022

23+
// SaveToStore saves data to declared storage only if last update
24+
// is greater than persisted time
2125
func (p Persistence) SaveToStore() error {
2226
if p.cache.Updated().Before(p.store.PersistedAt()) {
2327
return nil
@@ -39,7 +43,9 @@ func (p Persistence) SaveToStore() error {
3943
}
4044

4145
for key, val := range temp {
42-
p.store.Set(key, val)
46+
if err := p.store.Set(key, val); err != nil {
47+
p.logger.Debugf("error while storing data, err: %v", err)
48+
}
4349
}
4450
err := p.store.Persist()
4551
if err != nil {
@@ -48,6 +54,7 @@ func (p Persistence) SaveToStore() error {
4854
return nil
4955
}
5056

57+
// LoadFromStore loads all stored data into specified cache
5158
func (p *Persistence) LoadFromStore() error {
5259
p.logger.Info("Loading cache data from store")
5360
err := p.store.Load()

0 commit comments

Comments
 (0)