Skip to content

Commit 3accda2

Browse files
authored
make WithTag thread safe (#9)
* update deps * make WithTag thread safe * update ci file * fix lint
1 parent 568d038 commit 3accda2

File tree

7 files changed

+145
-360
lines changed

7 files changed

+145
-360
lines changed

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
build:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v4
1515

1616
- name: Set up Go
17-
uses: actions/setup-go@v2
17+
uses: actions/setup-go@v5
1818
with:
19-
go-version: 1.19
19+
go-version-file: './go.mod'
2020

2121
- name: Build
2222
run: go build -v ./...

context.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ var ctxOptionsKey ctxKeyType
1919
// to skip the commenting on Query.
2020
//
2121
// client.T.Query().All(sqlcomment.Skip(ctx))
22-
//
2322
func Skip(ctx context.Context) context.Context {
2423
c, ok := ctx.Value(ctxOptionsKey).(*ctxOptions)
2524
if !ok {
@@ -31,6 +30,7 @@ func Skip(ctx context.Context) context.Context {
3130

3231
// WithTag stores the key and val pair on the context.
3332
// for example, if you want to add `route` tag to your SQL comment, put the url path on request context:
33+
//
3434
// middleware := func(next http.Handler) http.Handler {
3535
// fn := func(w http.ResponseWriter, r *http.Request) {
3636
// ctx := sqlcomment.WithTag(r.Context(), "route", r.URL.Path)
@@ -43,8 +43,13 @@ func WithTag(ctx context.Context, key, val string) context.Context {
4343
if !ok {
4444
return context.WithValue(ctx, ctxOptionsKey, &ctxOptions{tags: Tags{key: val}})
4545
}
46-
t.tags[key] = val
47-
return ctx
46+
// Create a copy of the existing ctxOptions to avoid modifying the original
47+
newTags := make(Tags)
48+
for k, v := range t.tags {
49+
newTags[k] = v
50+
}
51+
newTags[key] = val
52+
return context.WithValue(ctx, ctxOptionsKey, &ctxOptions{tags: newTags})
4853
}
4954

5055
// FromContext returns the tags stored in ctx, if any.

context_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sqlcomment
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"sync"
7+
"testing"
8+
)
9+
10+
func TestWithTagThreadSafety(t *testing.T) {
11+
ctx := context.Background()
12+
var wg sync.WaitGroup
13+
const numGoroutines = 100
14+
results := make(map[int]Tags)
15+
mu := sync.Mutex{}
16+
for i := 0; i < numGoroutines; i++ {
17+
wg.Add(1)
18+
go func(i int) {
19+
defer wg.Done()
20+
// Add a unique tag for each goroutine
21+
newCtx := WithTag(ctx, "goroutine", string(rune('A'+i)))
22+
tags := FromContext(newCtx)
23+
// Store the result in the map
24+
mu.Lock()
25+
results[i] = tags
26+
mu.Unlock()
27+
}(i)
28+
}
29+
30+
wg.Wait()
31+
// Verify that each goroutine has its own independent tags
32+
assert.Equal(t, numGoroutines, len(results))
33+
for i := 0; i < numGoroutines; i++ {
34+
assert.Contains(t, results[i], "goroutine")
35+
assert.Equal(t, string(rune('A'+i)), results[i]["goroutine"])
36+
}
37+
}

examples/ent/migrate/migrate.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/otel/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (mcc CustomCommenter) Tag(ctx context.Context) sqlcomment.Tags {
3131
}
3232
}
3333

34-
func Example_OTELIntegration() {
34+
func Example_otelIntegration() {
3535
tp := initTracer()
3636
defer func() {
3737
if err := tp.Shutdown(context.Background()); err != nil {

go.mod

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
module ariga.io/sqlcomment
22

3-
go 1.19
3+
go 1.23.0
4+
5+
toolchain go1.24.3
46

57
require (
6-
entgo.io/ent v0.11.3-0.20220816070906-2b54aadcce3a
7-
github.com/mattn/go-sqlite3 v1.14.14
8-
github.com/stretchr/testify v1.8.0
9-
go.opencensus.io v0.23.0
10-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0
11-
go.opentelemetry.io/otel v1.0.1
12-
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.0.1
13-
go.opentelemetry.io/otel/sdk v1.0.1
8+
entgo.io/ent v0.14.4
9+
github.com/mattn/go-sqlite3 v1.14.28
10+
github.com/stretchr/testify v1.10.0
11+
go.opencensus.io v0.24.0
12+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0
13+
go.opentelemetry.io/otel v1.37.0
14+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.37.0
15+
go.opentelemetry.io/otel/sdk v1.37.0
1416
)
1517

1618
require (
17-
ariga.io/atlas v0.6.0 // indirect
19+
ariga.io/atlas v0.31.1-0.20250212144724-069be8033e83 // indirect
1820
github.com/agext/levenshtein v1.2.1 // indirect
1921
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
22+
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
23+
github.com/bmatcuk/doublestar v1.3.4 // indirect
2024
github.com/davecgh/go-spew v1.1.1 // indirect
21-
github.com/felixge/httpsnoop v1.0.2 // indirect
25+
github.com/felixge/httpsnoop v1.0.4 // indirect
26+
github.com/go-logr/logr v1.4.3 // indirect
27+
github.com/go-logr/stdr v1.2.2 // indirect
2228
github.com/go-openapi/inflect v0.19.0 // indirect
2329
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
24-
github.com/google/go-cmp v0.5.6 // indirect
25-
github.com/google/uuid v1.3.0 // indirect
30+
github.com/google/go-cmp v0.7.0 // indirect
31+
github.com/google/uuid v1.6.0 // indirect
2632
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
2733
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
2834
github.com/pmezard/go-difflib v1.0.0 // indirect
29-
github.com/zclconf/go-cty v1.8.0 // indirect
30-
go.opentelemetry.io/otel/internal/metric v0.24.0 // indirect
31-
go.opentelemetry.io/otel/metric v0.24.0 // indirect
32-
go.opentelemetry.io/otel/trace v1.0.1 // indirect
33-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
34-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
35-
golang.org/x/text v0.3.7 // indirect
35+
github.com/zclconf/go-cty v1.14.4 // indirect
36+
github.com/zclconf/go-cty-yaml v1.1.0 // indirect
37+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
38+
go.opentelemetry.io/otel/metric v1.37.0 // indirect
39+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
40+
golang.org/x/mod v0.23.0 // indirect
41+
golang.org/x/sys v0.34.0 // indirect
42+
golang.org/x/text v0.21.0 // indirect
3643
gopkg.in/yaml.v3 v3.0.1 // indirect
3744
)

0 commit comments

Comments
 (0)