Skip to content

Commit 861cc91

Browse files
committed
ci: upgrade to golangci-lint 2
1 parent 80d443f commit 861cc91

File tree

10 files changed

+143
-128
lines changed

10 files changed

+143
-128
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
caddy/go.sum
3030
3131
- name: golangci-lint
32-
uses: golangci/golangci-lint-action@v6
32+
uses: golangci/golangci-lint-action@v7
3333
with:
3434
version: latest
3535
args: --timeout=30m

.golangci.yml

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1-
---
1+
version: "2"
22
run:
33
tests: true
4-
54
linters:
6-
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
7-
enable-all: true
5+
default: all
86
disable:
7+
- canonicalheader
8+
- cyclop
9+
- depguard
910
- errcheck
10-
- lll
11-
- wsl
12-
- testpackage
1311
- exhaustruct
14-
- paralleltest
15-
- cyclop
1612
- forcetypeassert
17-
- tagliatelle
18-
- varnamelen
19-
- nonamedreturns
20-
- testableexamples
21-
- musttag
22-
- depguard
23-
- mnd
24-
25-
# Go 1.22+
2613
- intrange
27-
28-
# weird issues
14+
- lll
15+
- mnd
16+
- musttag
2917
- nolintlint
30-
- canonicalheader
31-
32-
issues:
33-
exclude-rules:
34-
- path: _test\.go
35-
linters:
36-
- gochecknoglobals
37-
- funlen
38-
- godox
39-
- noctx
40-
- wrapcheck
41-
- goconst
18+
- nonamedreturns
19+
- paralleltest
20+
- tagliatelle
21+
- testableexamples
22+
- testpackage
23+
- varnamelen
24+
- wsl
25+
exclusions:
26+
generated: lax
27+
presets:
28+
- comments
29+
- common-false-positives
30+
- legacy
31+
- std-error-handling
32+
rules:
33+
- linters:
34+
- funlen
35+
- gochecknoglobals
36+
- goconst
37+
- godox
38+
- noctx
39+
- wrapcheck
40+
path: _test\.go
41+
paths:
42+
- third_party$
43+
- builtin$
44+
- examples$
45+
formatters:
46+
enable:
47+
- gci
48+
- gofmt
49+
- gofumpt
50+
- goimports
51+
exclusions:
52+
generated: lax
53+
paths:
54+
- third_party$
55+
- builtin$
56+
- examples$

bolt.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -166,41 +166,6 @@ func (t *BoltTransport) Dispatch(update *Update) error {
166166
return nil
167167
}
168168

169-
// persist stores update in the database.
170-
func (t *BoltTransport) persist(updateID string, updateJSON []byte) error {
171-
if err := t.db.Update(func(tx *bolt.Tx) error {
172-
bucket, err := tx.CreateBucketIfNotExists([]byte(t.bucketName))
173-
if err != nil {
174-
return fmt.Errorf("error when creating Bolt DB bucket: %w", err)
175-
}
176-
177-
seq, err := bucket.NextSequence()
178-
if err != nil {
179-
return fmt.Errorf("error when generating Bolt DB sequence: %w", err)
180-
}
181-
prefix := make([]byte, 8)
182-
binary.BigEndian.PutUint64(prefix, seq)
183-
184-
// The sequence value is prepended to the update id to create an ordered list
185-
key := bytes.Join([][]byte{prefix, []byte(updateID)}, []byte{})
186-
187-
// The DB is append-only
188-
bucket.FillPercent = 1
189-
190-
t.lastSeq = seq
191-
t.lastEventID = updateID
192-
if err := bucket.Put(key, updateJSON); err != nil {
193-
return fmt.Errorf("unable to put value in Bolt DB: %w", err)
194-
}
195-
196-
return t.cleanup(bucket, seq)
197-
}); err != nil {
198-
return fmt.Errorf("bolt error: %w", err)
199-
}
200-
201-
return nil
202-
}
203-
204169
// AddSubscriber adds a new subscriber to the transport.
205170
func (t *BoltTransport) AddSubscriber(s *LocalSubscriber) error {
206171
select {
@@ -211,7 +176,7 @@ func (t *BoltTransport) AddSubscriber(s *LocalSubscriber) error {
211176

212177
t.Lock()
213178
t.subscribers.Add(s)
214-
toSeq := t.lastSeq //nolint:ifshort
179+
toSeq := t.lastSeq
215180
t.Unlock()
216181

217182
if s.RequestLastEventID != "" {
@@ -248,6 +213,29 @@ func (t *BoltTransport) GetSubscribers() (string, []*Subscriber, error) {
248213
return t.lastEventID, getSubscribers(t.subscribers), nil
249214
}
250215

216+
// Close closes the Transport.
217+
func (t *BoltTransport) Close() (err error) {
218+
t.closedOnce.Do(func() {
219+
close(t.closed)
220+
221+
t.Lock()
222+
defer t.Unlock()
223+
224+
t.subscribers.Walk(0, func(s *LocalSubscriber) bool {
225+
s.Disconnect()
226+
227+
return true
228+
})
229+
err = t.db.Close()
230+
})
231+
232+
if err == nil {
233+
return nil
234+
}
235+
236+
return fmt.Errorf("unable to close Bolt DB: %w", err)
237+
}
238+
251239
//nolint:gocognit
252240
func (t *BoltTransport) dispatchHistory(s *LocalSubscriber, toSeq uint64) error {
253241
err := t.db.View(func(tx *bolt.Tx) error {
@@ -303,27 +291,39 @@ func (t *BoltTransport) dispatchHistory(s *LocalSubscriber, toSeq uint64) error
303291
return nil
304292
}
305293

306-
// Close closes the Transport.
307-
func (t *BoltTransport) Close() (err error) {
308-
t.closedOnce.Do(func() {
309-
close(t.closed)
294+
// persist stores update in the database.
295+
func (t *BoltTransport) persist(updateID string, updateJSON []byte) error {
296+
if err := t.db.Update(func(tx *bolt.Tx) error {
297+
bucket, err := tx.CreateBucketIfNotExists([]byte(t.bucketName))
298+
if err != nil {
299+
return fmt.Errorf("error when creating Bolt DB bucket: %w", err)
300+
}
310301

311-
t.Lock()
312-
defer t.Unlock()
302+
seq, err := bucket.NextSequence()
303+
if err != nil {
304+
return fmt.Errorf("error when generating Bolt DB sequence: %w", err)
305+
}
306+
prefix := make([]byte, 8)
307+
binary.BigEndian.PutUint64(prefix, seq)
313308

314-
t.subscribers.Walk(0, func(s *LocalSubscriber) bool {
315-
s.Disconnect()
309+
// The sequence value is prepended to the update id to create an ordered list
310+
key := bytes.Join([][]byte{prefix, []byte(updateID)}, []byte{})
316311

317-
return true
318-
})
319-
err = t.db.Close()
320-
})
312+
// The DB is append-only
313+
bucket.FillPercent = 1
321314

322-
if err == nil {
323-
return nil
315+
t.lastSeq = seq
316+
t.lastEventID = updateID
317+
if err := bucket.Put(key, updateJSON); err != nil {
318+
return fmt.Errorf("unable to put value in Bolt DB: %w", err)
319+
}
320+
321+
return t.cleanup(bucket, seq)
322+
}); err != nil {
323+
return fmt.Errorf("bolt error: %w", err)
324324
}
325325

326-
return fmt.Errorf("unable to close Bolt DB: %w", err)
326+
return nil
327327
}
328328

329329
// cleanup removes entries in the history above the size limit, triggered probabilistically.

demo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestEmptyBodyAndJWT(t *testing.T) {
3030

3131
defer resp.Body.Close()
3232
body, _ := io.ReadAll(resp.Body)
33-
assert.Equal(t, "", string(body))
33+
assert.Empty(t, string(body))
3434
}
3535

3636
func TestBodyAndJWT(t *testing.T) {

hub_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func TestNewHub(t *testing.T) {
2929

3030
assert.IsType(t, &viper.Viper{}, h.config)
3131

32-
assert.False(t, h.opt.anonymous)
33-
assert.Equal(t, defaultCookieName, h.opt.cookieName)
34-
assert.Equal(t, 40*time.Second, h.opt.heartbeat)
35-
assert.Equal(t, 5*time.Second, h.opt.dispatchTimeout)
36-
assert.Equal(t, 600*time.Second, h.opt.writeTimeout)
32+
assert.False(t, h.anonymous)
33+
assert.Equal(t, defaultCookieName, h.cookieName)
34+
assert.Equal(t, 40*time.Second, h.heartbeat)
35+
assert.Equal(t, 5*time.Second, h.dispatchTimeout)
36+
assert.Equal(t, 600*time.Second, h.writeTimeout)
3737
}
3838

3939
func TestNewHubWithConfig(t *testing.T) {

publish_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func FuzzPublish(f *testing.F) {
377377

378378
assert.Equal(t, http.StatusOK, resp.StatusCode)
379379
if id == "" {
380-
assert.NotEqual(t, "", string(body))
380+
assert.NotEmpty(t, string(body))
381381

382382
return
383383
}

subscribe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (h *Hub) getWriteDeadline(s *LocalSubscriber) (deadline time.Time) {
8484
deadline = time.Now().Add(h.writeTimeout)
8585
}
8686

87-
if s.Claims != nil && s.Claims.ExpiresAt != nil && (deadline == time.Time{} || s.Claims.ExpiresAt.Time.Before(deadline)) {
87+
if s.Claims != nil && s.Claims.ExpiresAt != nil && (deadline.Equal(time.Time{}) || s.Claims.ExpiresAt.Before(deadline)) {
8888
deadline = s.Claims.ExpiresAt.Time
8989
}
9090

subscribe_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ func (r *subscribeRecorder) WriteString(str string) (int, error) {
112112
return 0, os.ErrDeadlineExceeded
113113
}
114114

115-
return r.ResponseRecorder.WriteString(str)
115+
return r.WriteString(str)
116116
}
117117

118118
func (r *subscribeRecorder) FlushError() error {
119119
if time.Now().After(r.writeDeadline) {
120120
return os.ErrDeadlineExceeded
121121
}
122122

123-
r.ResponseRecorder.Flush()
123+
r.Flush()
124124

125125
return nil
126126
}
@@ -554,7 +554,7 @@ func TestSubscriptionEvents(t *testing.T) {
554554
body, _ := io.ReadAll(resp.Body)
555555

556556
assert.Equal(t, http.StatusOK, resp.StatusCode)
557-
assert.Equal(t, "", string(body))
557+
assert.Empty(t, string(body))
558558
}()
559559

560560
go func() {

subscriber.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ func (s *Subscriber) Match(u *Update) bool {
8585
return s.MatchTopics(u.Topics, u.Private)
8686
}
8787

88+
func (s *Subscriber) MarshalLogObject(enc zapcore.ObjectEncoder) error {
89+
enc.AddString("id", s.ID)
90+
enc.AddString("last_event_id", s.RequestLastEventID)
91+
if s.RemoteAddr != "" {
92+
enc.AddString("remote_addr", s.RemoteAddr)
93+
}
94+
if s.AllowedPrivateTopics != nil {
95+
if err := enc.AddArray("topic_selectors", stringArray(s.AllowedPrivateTopics)); err != nil {
96+
return fmt.Errorf("log error: %w", err)
97+
}
98+
}
99+
if s.SubscribedTopics != nil {
100+
if err := enc.AddArray("topics", stringArray(s.SubscribedTopics)); err != nil {
101+
return fmt.Errorf("log error: %w", err)
102+
}
103+
}
104+
105+
return nil
106+
}
107+
88108
// getSubscriptions return the list of subscriptions associated to this subscriber.
89109
func (s *Subscriber) getSubscriptions(topic, context string, active bool) []subscription {
90110
var subscriptions []subscription //nolint:prealloc
@@ -110,23 +130,3 @@ func (s *Subscriber) getSubscriptions(topic, context string, active bool) []subs
110130

111131
return subscriptions
112132
}
113-
114-
func (s *Subscriber) MarshalLogObject(enc zapcore.ObjectEncoder) error {
115-
enc.AddString("id", s.ID)
116-
enc.AddString("last_event_id", s.RequestLastEventID)
117-
if s.RemoteAddr != "" {
118-
enc.AddString("remote_addr", s.RemoteAddr)
119-
}
120-
if s.AllowedPrivateTopics != nil {
121-
if err := enc.AddArray("topic_selectors", stringArray(s.AllowedPrivateTopics)); err != nil {
122-
return fmt.Errorf("log error: %w", err)
123-
}
124-
}
125-
if s.SubscribedTopics != nil {
126-
if err := enc.AddArray("topics", stringArray(s.SubscribedTopics)); err != nil {
127-
return fmt.Errorf("log error: %w", err)
128-
}
129-
}
130-
131-
return nil
132-
}

transport.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ func RegisterTransportFactory(scheme string, factory TransportFactory) {
2626
transportFactoriesMu.Unlock()
2727
}
2828

29-
// Deprecated: directly instantiate the transport or use transports Caddy modules.
30-
func NewTransport(u *url.URL, l Logger) (Transport, error) { //nolint:ireturn
31-
transportFactoriesMu.RLock()
32-
f, ok := transportFactories[u.Scheme]
33-
transportFactoriesMu.RUnlock()
34-
35-
if !ok {
36-
return nil, &TransportError{dsn: u.Redacted(), msg: "no such transport available"}
37-
}
38-
39-
return f(u, l)
40-
}
41-
4229
// Transport provides methods to dispatch and persist updates.
4330
type Transport interface {
4431
// Dispatch dispatches an update to all subscribers.
@@ -54,6 +41,19 @@ type Transport interface {
5441
Close() error
5542
}
5643

44+
// Deprecated: directly instantiate the transport or use transports Caddy modules.
45+
func NewTransport(u *url.URL, l Logger) (Transport, error) { //nolint:ireturn
46+
transportFactoriesMu.RLock()
47+
f, ok := transportFactories[u.Scheme]
48+
transportFactoriesMu.RUnlock()
49+
50+
if !ok {
51+
return nil, &TransportError{dsn: u.Redacted(), msg: "no such transport available"}
52+
}
53+
54+
return f(u, l)
55+
}
56+
5757
// TransportSubscribers provides a method to retrieve the list of active subscribers.
5858
type TransportSubscribers interface {
5959
// GetSubscribers gets the last event ID and the list of active subscribers at this time.

0 commit comments

Comments
 (0)