@@ -138,9 +138,6 @@ type clientHook struct {
138
138
// Place for callers to attach arbitrary metadata to the client.
139
139
metadata Metadata
140
140
141
- // done is closed when refs == 0 and calls == 0.
142
- done chan struct {}
143
-
144
141
state mutex.Mutex [clientHookState ]
145
142
}
146
143
@@ -149,7 +146,6 @@ type clientHookState struct {
149
146
resolved chan struct {}
150
147
151
148
refs int // how many open Clients reference this clientHook
152
- calls int // number of outstanding ClientHook accesses
153
149
resolvedHook * clientHook // valid only if resolved is closed
154
150
}
155
151
@@ -164,7 +160,6 @@ func NewClient(hook ClientHook) Client {
164
160
}
165
161
h := & clientHook {
166
162
ClientHook : hook ,
167
- done : make (chan struct {}),
168
163
metadata : * NewMetadata (),
169
164
state : mutex .New (clientHookState {
170
165
resolved : closedSignal ,
@@ -199,7 +194,6 @@ func newPromisedClient(hook ClientHook) (Client, *clientPromise) {
199
194
}
200
195
h := & clientHook {
201
196
ClientHook : hook ,
202
- done : make (chan struct {}),
203
197
metadata : * NewMetadata (),
204
198
state : mutex .New (clientHookState {
205
199
resolved : make (chan struct {}),
@@ -228,17 +222,19 @@ func (c Client) startCall() (hook ClientHook, resolved, released bool, finish fu
228
222
if c .h == nil {
229
223
return nil , true , false , func () {}
230
224
}
231
- l .Value ().calls ++
225
+ l .Value ().refs ++
232
226
isResolved := l .Value ().isResolved ()
233
227
l .Unlock ()
234
228
savedHook := c .h
235
229
return savedHook .ClientHook , isResolved , false , func () {
230
+ shutdown := func () {}
236
231
savedHook .state .With (func (s * clientHookState ) {
237
- s .calls --
238
- if s .refs == 0 && s . calls == 0 {
239
- close ( savedHook .done )
232
+ s .refs --
233
+ if s .refs == 0 {
234
+ shutdown = savedHook .Shutdown
240
235
}
241
236
})
237
+ shutdown ()
242
238
}
243
239
})
244
240
}
@@ -653,12 +649,8 @@ func (c Client) Release() {
653
649
cl .Unlock ()
654
650
return
655
651
}
656
- if hl .Value ().calls == 0 {
657
- close (h .done )
658
- }
659
652
hl .Unlock ()
660
653
cl .Unlock ()
661
- <- h .done
662
654
h .Shutdown ()
663
655
c .GetFlowLimiter ().Release ()
664
656
}
@@ -735,7 +727,6 @@ func (cp *clientPromise) Fulfill(c Client) {
735
727
// references to be dropped, and then shuts down the hook. The caller
736
728
// must have previously invoked cp.fulfill().
737
729
func (cp * clientPromise ) shutdown () {
738
- <- cp .h .done
739
730
cp .h .Shutdown ()
740
731
}
741
732
@@ -770,9 +761,8 @@ func (cp *clientPromise) fulfill(c Client) {
770
761
}
771
762
772
763
// Client still had references, so we're responsible for shutting it down.
773
- if l .Value ().calls == 0 {
774
- close (cp .h .done )
775
- }
764
+ defer cp .h .Shutdown ()
765
+
776
766
rh , l = resolveHook (cp .h , l ) // swaps mutex on cp.h for mutex on rh
777
767
if rh != nil {
778
768
l .Value ().refs += refs
@@ -1008,7 +998,6 @@ func ErrorClient(e error) Client {
1008
998
// Avoid NewClient because it can set a finalizer.
1009
999
h := & clientHook {
1010
1000
ClientHook : errorClient {e },
1011
- done : make (chan struct {}),
1012
1001
metadata : * NewMetadata (),
1013
1002
state : mutex .New (clientHookState {
1014
1003
resolved : closedSignal ,
0 commit comments