@@ -111,87 +111,96 @@ func (c *Client) createSession(ctx context.Context, opts ...createSessionOption)
111111 if c .isClosed () {
112112 return nil , errClosedClient
113113 }
114+
115+ options := createSessionOptions {}
116+ for _ , o := range opts {
117+ o (& options )
118+ }
119+
114120 defer func () {
115121 if s == nil {
116122 return
117123 }
118- options := createSessionOptions {}
119- for _ , o := range opts {
120- o (& options )
121- }
122124 for _ , onCreate := range options .onCreate {
123125 onCreate (s )
124126 }
125127 s .onClose = append (s .onClose , options .onClose ... )
126128 }()
127- type result struct {
128- s * session
129- err error
130- }
131-
132- ch := make (chan result )
133129
134- c . spawnedGoroutines . Add ( 1 )
135- go func () {
136- defer c . spawnedGoroutines . Done ( )
130+ select {
131+ case <- c . done :
132+ return nil , xerrors . WithStackTrace ( errClosedClient )
137133
138- var (
134+ default :
135+ type result struct {
139136 s * session
140137 err error
141- )
142-
143- createSessionCtx := xcontext .WithoutDeadline (ctx )
144-
145- if timeout := c .config .CreateSessionTimeout (); timeout > 0 {
146- var cancel context.CancelFunc
147- createSessionCtx , cancel = context .WithTimeout (createSessionCtx , timeout )
148- defer cancel ()
149138 }
150139
151- s , err = c . build ( createSessionCtx )
140+ ch := make ( chan result )
152141
153- closeSession := func (s * session ) {
154- if s == nil {
155- return
156- }
142+ c .spawnedGoroutines .Add (1 )
143+ go func () {
144+ defer c .spawnedGoroutines .Done ()
145+
146+ var (
147+ s * session
148+ err error
149+ )
157150
158- closeSessionCtx := xcontext .WithoutDeadline (ctx )
151+ createSessionCtx := xcontext .WithoutDeadline (ctx )
159152
160- if timeout := c .config .DeleteTimeout (); timeout > 0 {
153+ if timeout := c .config .CreateSessionTimeout (); timeout > 0 {
161154 var cancel context.CancelFunc
162- createSessionCtx , cancel = context .WithTimeout (closeSessionCtx , timeout )
155+ createSessionCtx , cancel = context .WithTimeout (createSessionCtx , timeout )
163156 defer cancel ()
164157 }
165158
166- _ = s .Close (ctx )
167- }
159+ s , err = c .build (createSessionCtx )
168160
169- select {
170- case ch <- result {
171- s : s ,
172- err : err ,
173- }: // nop
161+ closeSession := func (s * session ) {
162+ if s == nil {
163+ return
164+ }
165+
166+ closeSessionCtx := xcontext .WithoutDeadline (ctx )
167+
168+ if timeout := c .config .DeleteTimeout (); timeout > 0 {
169+ var cancel context.CancelFunc
170+ createSessionCtx , cancel = context .WithTimeout (closeSessionCtx , timeout )
171+ defer cancel ()
172+ }
173+
174+ _ = s .Close (ctx )
175+ }
174176
177+ select {
178+ case ch <- result {
179+ s : s ,
180+ err : err ,
181+ }: // nop
182+
183+ case <- c .done :
184+ closeSession (s )
185+
186+ case <- ctx .Done ():
187+ closeSession (s )
188+ }
189+ }()
190+
191+ select {
175192 case <- c .done :
176- closeSession ( s )
193+ return nil , xerrors . WithStackTrace ( errClosedClient )
177194
178195 case <- ctx .Done ():
179- closeSession (s )
180- }
181- }()
196+ return nil , xerrors .WithStackTrace (ctx .Err ())
182197
183- select {
184- case r := <- ch :
185- if r .err != nil {
186- return nil , xerrors .WithStackTrace (r .err )
198+ case r := <- ch :
199+ if r .err != nil {
200+ return nil , xerrors .WithStackTrace (r .err )
201+ }
202+ return r .s , nil
187203 }
188- return r .s , nil
189-
190- case <- c .done :
191- return nil , xerrors .WithStackTrace (errClosedClient )
192-
193- case <- ctx .Done ():
194- return nil , xerrors .WithStackTrace (ctx .Err ())
195204 }
196205}
197206
0 commit comments