Skip to content

Commit 6c463b1

Browse files
committed
remove trace.ContextTable and trace.WithTable
1 parent 88e74f0 commit 6c463b1

File tree

4 files changed

+53
-105
lines changed

4 files changed

+53
-105
lines changed

internal/table/client.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func newClient(
6262
builder SessionBuilder,
6363
config config.Config,
6464
) *client {
65-
onDone := trace.TableOnInit(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
65+
onDone := trace.TableOnInit(config.Trace(), &ctx)
6666
if builder == nil {
6767
builder = func(ctx context.Context) (s Session, err error) {
6868
return newSession(ctx, cc, config)
@@ -213,7 +213,7 @@ func (c *client) createSession(ctx context.Context) (s Session, err error) {
213213
c.config.CreateSessionTimeout(),
214214
)
215215

216-
onDone := trace.TableOnPoolSessionNew(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
216+
onDone := trace.TableOnPoolSessionNew(c.config.Trace(), &ctx)
217217

218218
defer func() {
219219
onDone(s, err)
@@ -285,15 +285,28 @@ func (c *client) createSession(ctx context.Context) (s Session, err error) {
285285
}
286286
}
287287

288-
// Get returns first idle session from the client and removes it from
289-
// there. If no items stored in client it creates new one returns it.
290-
func (c *client) Get(ctx context.Context) (s Session, err error) {
288+
type getOptions struct {
289+
t trace.Table
290+
}
291+
292+
type getOption func(o *getOptions)
293+
294+
func withTrace(t trace.Table) getOption {
295+
return func(o *getOptions) {
296+
o.t = o.t.Compose(t)
297+
}
298+
}
299+
300+
func (c *client) get(ctx context.Context, opts ...getOption) (s Session, err error) {
291301
var (
292302
i = 0
293-
t = c.config.Trace().Compose(trace.ContextTable(ctx))
303+
o = getOptions{t: c.config.Trace()}
294304
)
305+
for _, opt := range opts {
306+
opt(&o)
307+
}
295308

296-
onDone := trace.TableOnPoolGet(t, &ctx)
309+
onDone := trace.TableOnPoolGet(o.t, &ctx)
297310
defer func() {
298311
onDone(s, i, err)
299312
}()
@@ -329,7 +342,7 @@ func (c *client) Get(ctx context.Context) (s Session, err error) {
329342
// are less than maximum amount of touched session. That is, we want to
330343
// be fair here and not to lock more goroutines than we could ship
331344
// session to.
332-
s, err = c.waitFromCh(ctx, t)
345+
s, err = c.waitFromCh(ctx, o.t)
333346
if err != nil {
334347
err = errors.WithStackTrace(err)
335348
}
@@ -345,6 +358,12 @@ func (c *client) Get(ctx context.Context) (s Session, err error) {
345358
return s, nil
346359
}
347360

361+
// Get returns first idle session from the client and removes it from
362+
// there. If no items stored in client it creates new one returns it.
363+
func (c *client) Get(ctx context.Context) (s Session, err error) {
364+
return c.get(ctx)
365+
}
366+
348367
func (c *client) waitFromCh(ctx context.Context, t trace.Table) (s Session, err error) {
349368
var (
350369
ch *chan Session
@@ -403,7 +422,7 @@ func (c *client) waitFromCh(ctx context.Context, t trace.Table) (s Session, err
403422
// Get() or Take() calls. In other way it will produce unexpected behavior or
404423
// panic.
405424
func (c *client) Put(ctx context.Context, s Session) (err error) {
406-
onDone := trace.TableOnPoolPut(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx, s)
425+
onDone := trace.TableOnPoolPut(c.config.Trace(), &ctx, s)
407426
defer func() {
408427
onDone(err)
409428
}()
@@ -447,7 +466,7 @@ func (c *client) Put(ctx context.Context, s Session) (err error) {
447466
// It returns first error occurred during stale sessions' deletion.
448467
// Note that even on error it calls Close() on each session.
449468
func (c *client) Close(ctx context.Context) (err error) {
450-
onDone := trace.TableOnClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
469+
onDone := trace.TableOnClose(c.config.Trace(), &ctx)
451470
defer func() {
452471
onDone(err)
453472
}()
@@ -851,7 +870,7 @@ func (c *client) closeSession(ctx context.Context, s Session, opts ...closeSessi
851870
}
852871

853872
if h.withTrace {
854-
onDone := trace.TableOnPoolSessionClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx, s)
873+
onDone := trace.TableOnPoolSessionClose(c.config.Trace(), &ctx, s)
855874
defer onDone()
856875
}
857876

internal/table/client_test.go

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,14 @@ func TestSessionPoolCloseWhenWaiting(t *testing.T) {
195195
mustGetSession(t, p)
196196

197197
go func() {
198-
_, err := p.Get(
199-
trace.WithTable(
200-
context.Background(),
201-
trace.Table{
202-
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
203-
get <- struct{}{}
204-
return nil
205-
},
198+
_, err := p.get(
199+
context.Background(),
200+
withTrace(trace.Table{
201+
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
202+
get <- struct{}{}
203+
return nil
206204
},
207-
),
205+
}),
208206
)
209207
got <- err
210208
}()
@@ -326,7 +324,7 @@ func TestSessionPoolClose(t *testing.T) {
326324
}
327325

328326
func TestRaceWgClosed(t *testing.T) {
329-
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
327+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
330328
defer cancel()
331329

332330
defer func() {
@@ -622,20 +620,18 @@ func TestSessionPoolSizeLimitOverflow(t *testing.T) {
622620
}
623621
}
624622
go func() {
625-
session, err := p.Get(
626-
trace.WithTable(
627-
context.Background(),
628-
trace.Table{
629-
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
630-
get <- struct{}{}
631-
return nil
632-
},
633-
OnPoolWait: func(trace.TablePoolWaitStartInfo) func(trace.TablePoolWaitDoneInfo) {
634-
wait <- struct{}{}
635-
return nil
636-
},
623+
session, err := p.get(
624+
context.Background(),
625+
withTrace(trace.Table{
626+
OnPoolGet: func(trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
627+
get <- struct{}{}
628+
return nil
629+
},
630+
OnPoolWait: func(trace.TablePoolWaitStartInfo) func(trace.TablePoolWaitDoneInfo) {
631+
wait <- struct{}{}
632+
return nil
637633
},
638-
),
634+
}),
639635
)
640636
got <- sessionAndError{session, err}
641637
}()
@@ -1239,17 +1235,7 @@ func mustResetTimer(t *testing.T, ch <-chan time.Duration, exp time.Duration) {
12391235
func mustCreateSession(t *testing.T, p *client) Session {
12401236
wg := sync.WaitGroup{}
12411237
defer wg.Wait()
1242-
s, err := p.createSession(trace.WithTable(
1243-
context.Background(),
1244-
trace.Table{
1245-
OnPoolSessionNew: func(info trace.TablePoolSessionNewStartInfo) func(trace.TablePoolSessionNewDoneInfo) {
1246-
wg.Add(1)
1247-
return func(info trace.TablePoolSessionNewDoneInfo) {
1248-
wg.Done()
1249-
}
1250-
},
1251-
},
1252-
))
1238+
s, err := p.createSession(context.Background())
12531239
if err != nil {
12541240
t.Helper()
12551241
t.Fatalf("%s: %v", caller(), err)
@@ -1272,27 +1258,7 @@ func mustPutSession(t *testing.T, p *client, s Session) {
12721258
wg := sync.WaitGroup{}
12731259
defer wg.Wait()
12741260
if err := p.Put(
1275-
trace.WithTable(
1276-
context.Background(),
1277-
trace.Table{
1278-
OnPoolPut: func(info trace.TablePoolPutStartInfo) func(trace.TablePoolPutDoneInfo) {
1279-
wg.Add(1)
1280-
return func(info trace.TablePoolPutDoneInfo) {
1281-
wg.Done()
1282-
}
1283-
},
1284-
OnPoolSessionClose: func(
1285-
info trace.TablePoolSessionCloseStartInfo,
1286-
) func(
1287-
doneInfo trace.TablePoolSessionCloseDoneInfo,
1288-
) {
1289-
wg.Add(1)
1290-
return func(info trace.TablePoolSessionCloseDoneInfo) {
1291-
wg.Done()
1292-
}
1293-
},
1294-
},
1295-
),
1261+
context.Background(),
12961262
s,
12971263
); err != nil {
12981264
t.Helper()
@@ -1303,23 +1269,7 @@ func mustPutSession(t *testing.T, p *client, s Session) {
13031269
func mustClose(t *testing.T, p *client) {
13041270
wg := sync.WaitGroup{}
13051271
defer wg.Wait()
1306-
if err := p.Close(
1307-
trace.WithTable(
1308-
context.Background(),
1309-
trace.Table{
1310-
OnPoolSessionClose: func(
1311-
info trace.TablePoolSessionCloseStartInfo,
1312-
) func(
1313-
doneInfo trace.TablePoolSessionCloseDoneInfo,
1314-
) {
1315-
wg.Add(1)
1316-
return func(info trace.TablePoolSessionCloseDoneInfo) {
1317-
wg.Done()
1318-
}
1319-
},
1320-
},
1321-
),
1322-
); err != nil {
1272+
if err := p.Close(context.Background()); err != nil {
13231273
t.Helper()
13241274
t.Fatalf("%s: %v", caller(), err)
13251275
}

internal/table/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *session) isClosing() bool {
8989
}
9090

9191
func newSession(ctx context.Context, cc grpc.ClientConnInterface, config config.Config) (s Session, err error) {
92-
onDone := trace.TableOnSessionNew(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
92+
onDone := trace.TableOnSessionNew(config.Trace(), &ctx)
9393
defer func() {
9494
onDone(s, err)
9595
}()

trace/context.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)