Skip to content

Commit 8170253

Browse files
committed
Replace CountDownLatch with DispatchSemaphore in generated code.
1 parent c8f9390 commit 8170253

11 files changed

+80
-80
lines changed

Examples/Echo/Generated/echo.client.pb.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
*/
4141

4242
import Foundation
43+
import Dispatch
4344
import gRPC
4445

45-
4646
/// Type for errors thrown from generated client code.
4747
public enum Echo_EchoClientError : Error {
4848
case endOfStream
@@ -61,7 +61,7 @@ public class Echo_EchoGetCall {
6161
/// Run the call. Blocks until the reply is received.
6262
fileprivate func run(request: Echo_EchoRequest,
6363
metadata: Metadata) throws -> Echo_EchoResponse {
64-
let latch = CountDownLatch(1)
64+
let sem = DispatchSemaphore(value: 0)
6565
var callResult : CallResult!
6666
var response : Echo_EchoResponse?
6767
let requestData = try request.serializeProtobuf()
@@ -73,9 +73,9 @@ public class Echo_EchoGetCall {
7373
if let responseData = callResult.resultData {
7474
response = try? Echo_EchoResponse(protobuf:responseData)
7575
}
76-
latch.signal()
76+
sem.signal()
7777
}
78-
latch.wait()
78+
_ = sem.wait(timeout: DispatchTime.distantFuture)
7979
if let response = response {
8080
return response
8181
} else {
@@ -96,22 +96,22 @@ public class Echo_EchoExpandCall {
9696
// Call this once with the message to send.
9797
fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
9898
let requestData = try request.serializeProtobuf()
99-
let latch = CountDownLatch(1)
99+
let sem = DispatchSemaphore(value: 0)
100100
try call.start(.serverStreaming,
101101
metadata:metadata,
102102
message:requestData)
103103
{callResult in
104-
latch.signal()
104+
sem.signal()
105105
}
106-
latch.wait()
106+
_ = sem.wait(timeout: DispatchTime.distantFuture)
107107
return self
108108
}
109109

110110
// Call this to wait for a result. Blocks.
111111
public func receive() throws -> Echo_EchoResponse {
112112
var returnError : Echo_EchoClientError?
113113
var response : Echo_EchoResponse!
114-
let latch = CountDownLatch(1)
114+
let sem = DispatchSemaphore(value: 0)
115115
do {
116116
try call.receiveMessage() {(responseData) in
117117
if let responseData = responseData {
@@ -122,9 +122,9 @@ public class Echo_EchoExpandCall {
122122
} else {
123123
returnError = Echo_EchoClientError.endOfStream
124124
}
125-
latch.signal()
125+
sem.signal()
126126
}
127-
latch.wait()
127+
_ = sem.wait(timeout: DispatchTime.distantFuture)
128128
}
129129
if let returnError = returnError {
130130
throw returnError
@@ -144,13 +144,13 @@ public class Echo_EchoCollectCall {
144144

145145
// Call this to start a call.
146146
fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
147-
let latch = CountDownLatch(1)
147+
let sem = DispatchSemaphore(value: 0)
148148
try self.call.start(.clientStreaming,
149149
metadata:metadata)
150150
{callResult in
151-
latch.signal()
151+
sem.signal()
152152
}
153-
latch.wait()
153+
_ = sem.wait(timeout: DispatchTime.distantFuture)
154154
return self
155155
}
156156

@@ -164,7 +164,7 @@ public class Echo_EchoCollectCall {
164164
public func closeAndReceive() throws -> Echo_EchoResponse {
165165
var returnError : Echo_EchoClientError?
166166
var returnResponse : Echo_EchoResponse!
167-
let latch = CountDownLatch(1)
167+
let sem = DispatchSemaphore(value: 0)
168168
do {
169169
try call.receiveMessage() {(responseData) in
170170
if let responseData = responseData,
@@ -173,10 +173,10 @@ public class Echo_EchoCollectCall {
173173
} else {
174174
returnError = Echo_EchoClientError.invalidMessageReceived
175175
}
176-
latch.signal()
176+
sem.signal()
177177
}
178178
try call.close(completion:{})
179-
latch.wait()
179+
_ = sem.wait(timeout: DispatchTime.distantFuture)
180180
} catch (let error) {
181181
throw error
182182
}
@@ -197,20 +197,20 @@ public class Echo_EchoUpdateCall {
197197
}
198198

199199
fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
200-
let latch = CountDownLatch(1)
200+
let sem = DispatchSemaphore(value: 0)
201201
try self.call.start(.bidiStreaming,
202202
metadata:metadata)
203203
{callResult in
204-
latch.signal()
204+
sem.signal()
205205
}
206-
latch.wait()
206+
_ = sem.wait(timeout: DispatchTime.distantFuture)
207207
return self
208208
}
209209

210210
public func receive() throws -> Echo_EchoResponse {
211211
var returnError : Echo_EchoClientError?
212212
var returnMessage : Echo_EchoResponse!
213-
let latch = CountDownLatch(1)
213+
let sem = DispatchSemaphore(value: 0)
214214
do {
215215
try call.receiveMessage() {(data) in
216216
if let data = data {
@@ -221,9 +221,9 @@ public class Echo_EchoUpdateCall {
221221
} else {
222222
returnError = Echo_EchoClientError.endOfStream
223223
}
224-
latch.signal()
224+
sem.signal()
225225
}
226-
latch.wait()
226+
_ = sem.wait(timeout: DispatchTime.distantFuture)
227227
}
228228
if let returnError = returnError {
229229
throw returnError
@@ -237,11 +237,11 @@ public class Echo_EchoUpdateCall {
237237
}
238238

239239
public func closeSend() throws {
240-
let latch = CountDownLatch(1)
240+
let sem = DispatchSemaphore(value: 0)
241241
try call.close() {
242-
latch.signal()
242+
sem.signal()
243243
}
244-
latch.wait()
244+
_ = sem.wait(timeout: DispatchTime.distantFuture)
245245
}
246246
}
247247

@@ -301,4 +301,4 @@ public class Echo_EchoService {
301301
public func update() throws -> Echo_EchoUpdateCall {
302302
return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
303303
}
304-
}
304+
}

Examples/Echo/Generated/echo.server.pb.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
*/
4141

4242
import Foundation
43-
import gRPC
4443
import Dispatch
44+
import gRPC
4545

4646
/// Type for errors thrown from generated server code.
4747
public enum Echo_EchoServerError : Error {
@@ -150,15 +150,15 @@ public class Echo_EchoCollectSession : Echo_EchoSession {
150150

151151
/// Receive a message. Blocks until a message is received or the client closes the connection.
152152
public func receive() throws -> Echo_EchoRequest {
153-
let latch = CountDownLatch(1)
153+
let sem = DispatchSemaphore(value: 0)
154154
var requestMessage : Echo_EchoRequest?
155155
try self.handler.receiveMessage() {(requestData) in
156156
if let requestData = requestData {
157157
requestMessage = try? Echo_EchoRequest(protobuf:requestData)
158158
}
159-
latch.signal()
159+
sem.signal()
160160
}
161-
latch.wait()
161+
_ = sem.wait(timeout: DispatchTime.distantFuture)
162162
if requestMessage == nil {
163163
throw Echo_EchoServerError.endOfStream
164164
}
@@ -199,7 +199,7 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
199199

200200
/// Receive a message. Blocks until a message is received or the client closes the connection.
201201
public func receive() throws -> Echo_EchoRequest {
202-
let latch = CountDownLatch(1)
202+
let sem = DispatchSemaphore(value: 0)
203203
var requestMessage : Echo_EchoRequest?
204204
try self.handler.receiveMessage() {(requestData) in
205205
if let requestData = requestData {
@@ -209,9 +209,9 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
209209
print("error \(error)")
210210
}
211211
}
212-
latch.signal()
212+
sem.signal()
213213
}
214-
latch.wait()
214+
_ = sem.wait(timeout: DispatchTime.distantFuture)
215215
if let requestMessage = requestMessage {
216216
return requestMessage
217217
} else {
@@ -226,13 +226,13 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
226226

227227
/// Close a connection. Blocks until the connection is closed.
228228
public func close() throws {
229-
let latch = CountDownLatch(1)
229+
let sem = DispatchSemaphore(value: 0)
230230
try self.handler.sendStatus(statusCode:self.statusCode,
231231
statusMessage:self.statusMessage,
232232
trailingMetadata:self.trailingMetadata) {
233-
latch.signal()
233+
sem.signal()
234234
}
235-
latch.wait()
235+
_ = sem.wait(timeout: DispatchTime.distantFuture)
236236
}
237237

238238
/// Run the session. Internal.

0 commit comments

Comments
 (0)