Skip to content

Commit 954e9b8

Browse files
authored
Add support for proper usage of Context (#239)
* Update interfaces & impls to pass context
1 parent ee2f1be commit 954e9b8

Some content is hidden

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

49 files changed

+749
-675
lines changed

.golangci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
version: "2"
2+
run:
3+
modules-download-mode: readonly
4+
linters:
5+
default: none
6+
enable:
7+
- errcheck
8+
- goheader
9+
- govet
10+
- ineffassign
11+
- staticcheck
12+
- unused
13+
settings:
14+
goheader:
15+
values:
16+
regexp:
17+
decade: '[0-9]'
18+
year: '[0-9]'
19+
template: Copyright (C) 20{{ decade }}{{ year }} by Posit Software, PBC
20+
exclusions:
21+
generated: lax
22+
presets:
23+
- comments
24+
- common-false-positives
25+
- legacy
26+
- std-error-handling
27+
paths:
28+
- mock_.*\.go$
29+
rules:
30+
- linters:
31+
- staticcheck
32+
text: "QF1008|ST1001|ST1005|ST1019|SA1019|QF1003|ST1012|SA1029"
33+
formatters:
34+
enable:
35+
- gofmt
36+
- goimports
37+
settings:
38+
goimports:
39+
local-prefixes:
40+
- github.com/rstudio
41+
- rspm
42+
exclusions:
43+
generated: lax
44+
paths:
45+
- mock_.*\.go$

examples/cmd/markdownRenderer/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type cacheQueueWrapper struct {
7373
// AddressedPush is part of the cache's queue interface. Since we're using the
7474
// rsqueue/Queue package for message queuing, we can simply wrap the queue's
7575
// AddressedPush method and hand off the wrapped queue to the cache.
76-
func (q *cacheQueueWrapper) AddressedPush(priority uint64, groupId int64, address string, work rscache.QueueWork) error {
77-
return q.Queue.AddressedPush(priority, groupId, address, work)
76+
func (q *cacheQueueWrapper) AddressedPush(ctx context.Context, priority uint64, groupId int64, address string, work rscache.QueueWork) error {
77+
return q.Queue.AddressedPush(ctx, priority, groupId, address, work)
7878
}
7979

8080
func init() {
@@ -90,6 +90,8 @@ func (l *leveler) Level() slog.Level {
9090
}
9191

9292
func main() {
93+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
94+
defer stop()
9395

9496
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
9597
Level: &leveler{level: slog.LevelDebug},
@@ -286,12 +288,12 @@ func main() {
286288
JobLifecycleWrapper: &metrics.EmptyJobLifecycleWrapper{},
287289
}
288290
ag := agent.NewAgent(agentCfg)
289-
go ag.Run(func(n listener.Notification) {
291+
go ag.Run(ctx, func(n listener.Notification) {
290292
// Since we don't enforce how notifications are sent, the agent sends a
291293
// notification to this callback method when work is completed. Here, we
292294
// simply pass those messages on to the store, which knows how to send
293295
// notifications.
294-
err = exampleStore.Notify(notifytypes.ChannelMessages, n)
296+
err = exampleStore.Notify(ctx, notifytypes.ChannelMessages, n)
295297
if err != nil {
296298
log.Printf("Error notifying of queue work complete: %s", err)
297299
}
@@ -300,7 +302,7 @@ func main() {
300302
// Start HTTP services and listen until the application exits.
301303
router := mux.NewRouter()
302304
handler := handlers.NewHttpHandler(address, router, cache)
303-
ctx, cancel := context.WithCancel(context.Background())
305+
ctx, cancel := context.WithCancel(ctx)
304306
go handler.Start(ctx)
305307
// Cancel the handler's context when the application exits for graceful
306308
// shutdown.

examples/cmd/markdownRenderer/storage/chunks.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
// Copyright (C) 2022 by RStudio, PBC
44

55
import (
6+
"context"
67
"time"
78

89
"github.com/rstudio/platform-lib/v2/examples/cmd/markdownRenderer/notifytypes"
@@ -15,7 +16,7 @@ type ExampleChunkWaiter struct {
1516
awb broadcaster.Broadcaster
1617
}
1718

18-
func (cw *ExampleChunkWaiter) WaitForChunk(c *types.ChunkNotification) {
19+
func (cw *ExampleChunkWaiter) WaitForChunk(ctx context.Context, c *types.ChunkNotification) {
1920
timeout := time.NewTimer(c.Timeout)
2021
defer timeout.Stop()
2122

@@ -46,15 +47,15 @@ func NewExampleChunkWaiter(awb broadcaster.Broadcaster) *ExampleChunkWaiter {
4647
}
4748

4849
type ChunkNotifierStore interface {
49-
Notify(channelName string, n interface{}) error
50+
Notify(ctx context.Context, channelName string, n interface{}) error
5051
}
5152

5253
type ExampleChunkNotifier struct {
5354
store ChunkNotifierStore
5455
}
5556

56-
func (cn *ExampleChunkNotifier) Notify(c *types.ChunkNotification) error {
57-
return cn.store.Notify(notifytypes.ChannelMessages, notifytypes.NewChunkNotification(c.Address, c.Chunk))
57+
func (cn *ExampleChunkNotifier) Notify(ctx context.Context, c *types.ChunkNotification) error {
58+
return cn.store.Notify(ctx, notifytypes.ChannelMessages, notifytypes.NewChunkNotification(c.Address, c.Chunk))
5859
}
5960

6061
func NewExampleChunkNotifier(cstore ChunkNotifierStore) *ExampleChunkNotifier {

examples/cmd/markdownRenderer/storage/chunks_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
// Copyright (C) 2022 by RStudio, PBC
44

55
import (
6+
"context"
67
"errors"
78
"testing"
89
"time"
@@ -39,7 +40,7 @@ type dummyStore struct {
3940
notify error
4041
}
4142

42-
func (d *dummyStore) Notify(channel string, n interface{}) error {
43+
func (d *dummyStore) Notify(ctx context.Context, channel string, n interface{}) error {
4344
return d.notify
4445
}
4546

@@ -49,6 +50,7 @@ type ChunksSuite struct {
4950
var _ = check.Suite(&ChunksSuite{})
5051

5152
func (s *ChunksSuite) TestWait(c *check.C) {
53+
ctx := context.Background()
5254
awb := &fakeBroadcaster{
5355
l: make(chan listener.Notification),
5456
}
@@ -63,28 +65,29 @@ func (s *ChunksSuite) TestWait(c *check.C) {
6365
go func() {
6466
awb.l <- notifytypes.NewChunkNotification("", 4)
6567
}()
66-
w.WaitForChunk(cn)
68+
w.WaitForChunk(ctx, cn)
6769

6870
// Test waiting for chunk (timeout)
6971
cn = &storagetypes.ChunkNotification{
7072
Timeout: time.Millisecond,
7173
Chunk: 3,
7274
}
73-
w.WaitForChunk(cn)
75+
w.WaitForChunk(ctx, cn)
7476
}
7577

7678
func (s *ChunksSuite) TestNotify(c *check.C) {
79+
ctx := context.Background()
7780
cstore := &dummyStore{
7881
notify: errors.New("some error"),
7982
}
8083
cn := NewExampleChunkNotifier(cstore)
8184
c.Assert(cn, check.DeepEquals, &ExampleChunkNotifier{store: cstore})
8285

83-
err := cn.Notify(&storagetypes.ChunkNotification{})
86+
err := cn.Notify(ctx, &storagetypes.ChunkNotification{})
8487
c.Assert(err, check.ErrorMatches, "some error")
8588

8689
cstore.notify = nil
87-
err = cn.Notify(&storagetypes.ChunkNotification{})
90+
err = cn.Notify(ctx, &storagetypes.ChunkNotification{})
8891
c.Assert(err, check.IsNil)
8992
}
9093

0 commit comments

Comments
 (0)