Skip to content

Commit 8e47ea6

Browse files
authored
Update and fix linter version at v2.3.1, also address all linter issues (#156)
* Update and fix linter version at v2.3.1, also address all linter issues * Update golangci/golangci-lint-action version to v2.3.1 * Fix lint issues after a git pull
1 parent a70e1c8 commit 8e47ea6

21 files changed

+535
-75
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- name: golangci-lint
2424
uses: golangci/golangci-lint-action@v8
2525
with:
26-
version: "v2.1.6"
26+
version: "v2.3.1"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ benchmark: docker-compose-up
1010
ETCD_ENDPOINTS="127.0.0.1:2379" REDIS_ADDR="127.0.0.1:6379" REDIS_NODES="127.0.0.1:11000,127.0.0.1:11001,127.0.0.1:11002,127.0.0.1:11003,127.0.0.1:11004,127.0.0.1:11005" ZOOKEEPER_ENDPOINTS="127.0.0.1" CONSUL_ADDR="127.0.0.1:8500" AWS_ADDR="127.0.0.1:8000" MEMCACHED_ADDR="127.0.0.1:11211" POSTGRES_URL="postgres://postgres@localhost:5432/?sslmode=disable" COSMOS_ADDR="127.0.0.1:8081" go test -race -run=nonexistent -bench=.
1111

1212
lint:
13-
@(which golangci-lint && golangci-lint --version | grep 2.1.6) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v2.1.6)
13+
@(which golangci-lint && golangci-lint --version | grep 2.3.1) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/v2.3.1/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v2.3.1)
1414
golangci-lint run --fix ./...
1515

1616
goimports:

concurrent_buffer.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ func NewConcurrentBuffer(locker DistLocker, concurrentStateBackend ConcurrentBuf
3939
func (c *ConcurrentBuffer) Limit(ctx context.Context, key string) error {
4040
c.mu.Lock()
4141
defer c.mu.Unlock()
42-
if err := c.locker.Lock(ctx); err != nil {
42+
43+
err := c.locker.Lock(ctx)
44+
if err != nil {
4345
return err
4446
}
47+
4548
defer func() {
46-
if err := c.locker.Unlock(ctx); err != nil {
49+
err := c.locker.Unlock(ctx)
50+
if err != nil {
4751
c.logger.Log(err)
4852
}
4953
}()
@@ -52,9 +56,11 @@ func (c *ConcurrentBuffer) Limit(ctx context.Context, key string) error {
5256
if err != nil {
5357
return err
5458
}
59+
5560
if counter > c.capacity {
5661
// Rollback the Add() operation.
57-
if err = c.backend.Remove(ctx, key); err != nil {
62+
err = c.backend.Remove(ctx, key)
63+
if err != nil {
5864
c.logger.Log(err)
5965
}
6066

@@ -89,6 +95,7 @@ func NewConcurrentBufferInMemory(registry *Registry, ttl time.Duration, clock Cl
8995
func (c *ConcurrentBufferInMemory) Add(ctx context.Context, key string) (int64, error) {
9096
c.mu.Lock()
9197
defer c.mu.Unlock()
98+
9299
now := c.clock.Now()
93100
c.registry.DeleteExpired(now)
94101
c.registry.GetOrCreate(key, func() interface{} {
@@ -102,6 +109,7 @@ func (c *ConcurrentBufferInMemory) Add(ctx context.Context, key string) (int64,
102109
func (c *ConcurrentBufferInMemory) Remove(_ context.Context, key string) error {
103110
c.mu.Lock()
104111
defer c.mu.Unlock()
112+
105113
c.registry.Delete(key)
106114

107115
return nil
@@ -125,11 +133,16 @@ func NewConcurrentBufferRedis(cli redis.UniversalClient, key string, ttl time.Du
125133
// Add adds the request with the given key to the sorted set in Redis and returns the total number of requests in it.
126134
// It also removes the keys with expired TTL.
127135
func (c *ConcurrentBufferRedis) Add(ctx context.Context, key string) (int64, error) {
128-
var countCmd *redis.IntCmd
129-
var err error
136+
var (
137+
countCmd *redis.IntCmd
138+
err error
139+
)
140+
130141
done := make(chan struct{})
142+
131143
go func() {
132144
defer close(done)
145+
133146
_, err = c.cli.Pipelined(ctx, func(pipeliner redis.Pipeliner) error {
134147
// Remove expired items.
135148
now := c.clock.Now()
@@ -186,14 +199,20 @@ type SortedSetNode struct {
186199
// It also removes the keys with expired TTL.
187200
func (c *ConcurrentBufferMemcached) Add(ctx context.Context, element string) (int64, error) {
188201
var err error
202+
189203
done := make(chan struct{})
190204
now := c.clock.Now()
191-
var newNodes []SortedSetNode
192-
var casId uint64 = 0
205+
206+
var (
207+
newNodes []SortedSetNode
208+
casId uint64 = 0
209+
)
193210

194211
go func() {
195212
defer close(done)
213+
196214
var item *memcache.Item
215+
197216
item, err = c.cli.Get(c.key)
198217
if err != nil {
199218
if !errors.Is(err, memcache.ErrCacheMiss) {
@@ -202,17 +221,23 @@ func (c *ConcurrentBufferMemcached) Add(ctx context.Context, element string) (in
202221
} else {
203222
casId = item.CasID
204223
b := bytes.NewBuffer(item.Value)
224+
205225
var oldNodes []SortedSetNode
226+
206227
_ = gob.NewDecoder(b).Decode(&oldNodes)
207228
for _, node := range oldNodes {
208229
if node.CreatedAt > now.UnixNano() && node.Value != element {
209230
newNodes = append(newNodes, node)
210231
}
211232
}
212233
}
234+
213235
newNodes = append(newNodes, SortedSetNode{CreatedAt: now.Add(c.ttl).UnixNano(), Value: element})
236+
214237
var b bytes.Buffer
238+
215239
_ = gob.NewEncoder(&b).Encode(newNodes)
240+
216241
item = &memcache.Item{
217242
Key: c.key,
218243
Value: b.Bytes(),
@@ -245,9 +270,14 @@ func (c *ConcurrentBufferMemcached) Add(ctx context.Context, element string) (in
245270
// Remove removes the request identified by the key from the slice in Memcached.
246271
func (c *ConcurrentBufferMemcached) Remove(ctx context.Context, key string) error {
247272
var err error
273+
248274
now := c.clock.Now()
249-
var newNodes []SortedSetNode
250-
var casID uint64
275+
276+
var (
277+
newNodes []SortedSetNode
278+
casID uint64
279+
)
280+
251281
item, err := c.cli.Get(c.key)
252282
if err != nil {
253283
if errors.Is(err, memcache.ErrCacheMiss) {
@@ -256,8 +286,11 @@ func (c *ConcurrentBufferMemcached) Remove(ctx context.Context, key string) erro
256286

257287
return errors.Wrap(err, "failed to Get")
258288
}
289+
259290
casID = item.CasID
291+
260292
var oldNodes []SortedSetNode
293+
261294
_ = gob.NewDecoder(bytes.NewBuffer(item.Value)).Decode(&oldNodes)
262295
for _, node := range oldNodes {
263296
if node.CreatedAt > now.UnixNano() && node.Value != key {
@@ -266,12 +299,14 @@ func (c *ConcurrentBufferMemcached) Remove(ctx context.Context, key string) erro
266299
}
267300

268301
var b bytes.Buffer
302+
269303
_ = gob.NewEncoder(&b).Encode(newNodes)
270304
item = &memcache.Item{
271305
Key: c.key,
272306
Value: b.Bytes(),
273307
CasID: casID,
274308
}
309+
275310
err = c.cli.CompareAndSwap(item)
276311
if err != nil && (errors.Is(err, memcache.ErrCASConflict) || errors.Is(err, memcache.ErrNotStored) || errors.Is(err, memcache.ErrCacheMiss)) {
277312
return c.Remove(ctx, key)

concurrent_buffer_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
func (s *LimitersTestSuite) concurrentBuffers(capacity int64, ttl time.Duration, clock l.Clock) map[string]*l.ConcurrentBuffer {
1515
buffers := make(map[string]*l.ConcurrentBuffer)
16+
1617
for lockerName, locker := range s.lockers(true) {
1718
for bName, b := range s.concurrentBufferBackends(ttl, clock) {
1819
buffers[lockerName+":"+bName] = l.NewConcurrentBuffer(locker, b, capacity, s.logger)
@@ -34,19 +35,23 @@ func (s *LimitersTestSuite) concurrentBufferBackends(ttl time.Duration, clock l.
3435
func (s *LimitersTestSuite) TestConcurrentBufferNoOverflow() {
3536
clock := newFakeClock()
3637
capacity := int64(10)
38+
3739
ttl := time.Second
3840
for name, buffer := range s.concurrentBuffers(capacity, ttl, clock) {
3941
s.Run(name, func() {
4042
wg := sync.WaitGroup{}
4143
for i := int64(0); i < capacity; i++ {
4244
wg.Add(1)
45+
4346
go func(i int64, buffer *l.ConcurrentBuffer) {
4447
defer wg.Done()
48+
4549
key := fmt.Sprintf("key%d", i)
4650
s.NoError(buffer.Limit(context.TODO(), key))
4751
s.NoError(buffer.Done(context.TODO(), key))
4852
}(i, buffer)
4953
}
54+
5055
wg.Wait()
5156
s.NoError(buffer.Limit(context.TODO(), "last"))
5257
s.NoError(buffer.Done(context.TODO(), "last"))
@@ -57,23 +62,32 @@ func (s *LimitersTestSuite) TestConcurrentBufferNoOverflow() {
5762
func (s *LimitersTestSuite) TestConcurrentBufferOverflow() {
5863
clock := newFakeClock()
5964
capacity := int64(3)
65+
6066
ttl := time.Second
6167
for name, buffer := range s.concurrentBuffers(capacity, ttl, clock) {
6268
s.Run(name, func() {
6369
mu := sync.Mutex{}
70+
6471
var errors []error
72+
6573
wg := sync.WaitGroup{}
6674
for i := int64(0); i <= capacity; i++ {
6775
wg.Add(1)
76+
6877
go func(i int64, buffer *l.ConcurrentBuffer) {
6978
defer wg.Done()
70-
if err := buffer.Limit(context.TODO(), fmt.Sprintf("key%d", i)); err != nil {
79+
80+
err := buffer.Limit(context.TODO(), fmt.Sprintf("key%d", i))
81+
if err != nil {
7182
mu.Lock()
83+
7284
errors = append(errors, err)
85+
7386
mu.Unlock()
7487
}
7588
}(i, buffer)
7689
}
90+
7791
wg.Wait()
7892
s.Equal([]error{l.ErrLimitExhausted}, errors)
7993
})
@@ -83,6 +97,7 @@ func (s *LimitersTestSuite) TestConcurrentBufferOverflow() {
8397
func (s *LimitersTestSuite) TestConcurrentBufferExpiredKeys() {
8498
clock := newFakeClock()
8599
capacity := int64(2)
100+
86101
ttl := time.Second
87102
for name, buffer := range s.concurrentBuffers(capacity, ttl, clock) {
88103
s.Run(name, func() {
@@ -100,6 +115,7 @@ func (s *LimitersTestSuite) TestConcurrentBufferExpiredKeys() {
100115
func (s *LimitersTestSuite) TestConcurrentBufferDuplicateKeys() {
101116
clock := newFakeClock()
102117
capacity := int64(2)
118+
103119
ttl := time.Second
104120
for name, buffer := range s.concurrentBuffers(capacity, ttl, clock) {
105121
s.Run(name, func() {
@@ -115,9 +131,11 @@ func BenchmarkConcurrentBuffers(b *testing.B) {
115131
s := new(LimitersTestSuite)
116132
s.SetT(&testing.T{})
117133
s.SetupSuite()
134+
118135
capacity := int64(1)
119136
ttl := time.Second
120137
clock := newFakeClock()
138+
121139
buffers := s.concurrentBuffers(capacity, ttl, clock)
122140
for name, buffer := range buffers {
123141
b.Run(name, func(b *testing.B) {
@@ -126,5 +144,6 @@ func BenchmarkConcurrentBuffers(b *testing.B) {
126144
}
127145
})
128146
}
147+
129148
s.TearDownSuite()
130149
}

dynamodb.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,16 @@ func dynamoDBputItem(ctx context.Context, client *dynamodb.Client, input *dynamo
116116
func dynamoDBGetItem(ctx context.Context, client *dynamodb.Client, input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
117117
input.ConsistentRead = aws.Bool(true)
118118

119-
var resp *dynamodb.GetItemOutput
120-
var err error
119+
var (
120+
resp *dynamodb.GetItemOutput
121+
err error
122+
)
121123

122124
done := make(chan struct{})
125+
123126
go func() {
124127
defer close(done)
128+
125129
resp, err = client.GetItem(ctx, input)
126130
}()
127131

dynamodb_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func CreateTestDynamoDBTable(ctx context.Context, client *dynamodb.Client) error
5555

5656
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
5757
defer cancel()
58+
5859
for {
5960
resp, err := client.DescribeTable(ctx, &dynamodb.DescribeTableInput{
6061
TableName: aws.String(testDynamoDBTableName),
6162
})
62-
6363
if err == nil {
6464
return errors.Wrap(err, "failed to describe test table")
6565
}

0 commit comments

Comments
 (0)