Skip to content

Commit c26ec29

Browse files
committed
fix closing of table client
1 parent 5a7b6a1 commit c26ec29

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

internal/table/client.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -138,54 +138,63 @@ func (c *Client) createSession(ctx context.Context, opts ...createSessionOption)
138138
return nil, xerrors.WithStackTrace(errClosedClient)
139139
}
140140

141-
c.spawnedGoroutines.Add(1)
142-
go func() {
143-
defer c.spawnedGoroutines.Done()
144-
145-
var (
146-
s *session
147-
err error
148-
)
149-
150-
createSessionCtx := xcontext.WithoutDeadline(ctx)
141+
select {
142+
case <-c.done:
143+
return nil, xerrors.WithStackTrace(errClosedClient)
151144

152-
if timeout := c.config.CreateSessionTimeout(); timeout > 0 {
153-
var cancel context.CancelFunc
154-
createSessionCtx, cancel = context.WithTimeout(createSessionCtx, timeout)
155-
defer cancel()
156-
}
145+
case <-ctx.Done():
146+
return nil, xerrors.WithStackTrace(ctx.Err())
157147

158-
s, err = c.build(createSessionCtx)
148+
default:
149+
c.spawnedGoroutines.Add(1)
150+
go func() {
151+
defer c.spawnedGoroutines.Done()
159152

160-
closeSession := func(s *session) {
161-
if s == nil {
162-
return
163-
}
153+
var (
154+
s *session
155+
err error
156+
)
164157

165-
closeSessionCtx := xcontext.WithoutDeadline(ctx)
158+
createSessionCtx := xcontext.WithoutDeadline(ctx)
166159

167-
if timeout := c.config.DeleteTimeout(); timeout > 0 {
160+
if timeout := c.config.CreateSessionTimeout(); timeout > 0 {
168161
var cancel context.CancelFunc
169-
createSessionCtx, cancel = context.WithTimeout(closeSessionCtx, timeout)
162+
createSessionCtx, cancel = context.WithTimeout(createSessionCtx, timeout)
170163
defer cancel()
171164
}
172165

173-
_ = s.Close(ctx)
174-
}
166+
s, err = c.build(createSessionCtx)
175167

176-
select {
177-
case ch <- result{
178-
s: s,
179-
err: err,
180-
}: // nop
168+
closeSession := func(s *session) {
169+
if s == nil {
170+
return
171+
}
181172

182-
case <-c.done:
183-
closeSession(s)
173+
closeSessionCtx := xcontext.WithoutDeadline(ctx)
184174

185-
case <-ctx.Done():
186-
closeSession(s)
187-
}
188-
}()
175+
if timeout := c.config.DeleteTimeout(); timeout > 0 {
176+
var cancel context.CancelFunc
177+
createSessionCtx, cancel = context.WithTimeout(closeSessionCtx, timeout)
178+
defer cancel()
179+
}
180+
181+
_ = s.Close(ctx)
182+
}
183+
184+
select {
185+
case ch <- result{
186+
s: s,
187+
err: err,
188+
}: // nop
189+
190+
case <-c.done:
191+
closeSession(s)
192+
193+
case <-ctx.Done():
194+
closeSession(s)
195+
}
196+
}()
197+
}
189198

190199
select {
191200
case <-c.done:

0 commit comments

Comments
 (0)