Skip to content

Commit acb95db

Browse files
fix: improve performance of CloudRegionOrZone.parse and error message from PublisherImpl (#1206)
1 parent 9d93800 commit acb95db

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/CloudRegionOrZone.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.google.cloud.pubsublite;
1818

1919
import com.google.api.gax.rpc.ApiException;
20+
import com.google.api.gax.rpc.StatusCode.Code;
2021
import com.google.auto.value.AutoOneOf;
22+
import com.google.cloud.pubsublite.internal.CheckedApiException;
2123
import java.io.Serializable;
2224

2325
@AutoOneOf(CloudRegionOrZone.Kind.class)
@@ -54,12 +56,16 @@ public static CloudRegionOrZone of(CloudZone zone) {
5456
}
5557

5658
public static CloudRegionOrZone parse(String value) throws ApiException {
57-
try {
58-
return of(CloudZone.parse(value));
59-
} catch (ApiException e) {
60-
// pass
59+
String[] splits = value.split("-", -1);
60+
switch (splits.length) {
61+
case 2:
62+
return of(CloudRegion.of(value));
63+
case 3:
64+
return of(CloudZone.parse(value));
65+
default:
66+
throw new CheckedApiException("Invalid location: " + value, Code.INVALID_ARGUMENT)
67+
.underlying;
6168
}
62-
return of(CloudRegion.of(value));
6369
}
6470

6571
/** {@inheritDoc} */

google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/wire/PublisherImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,20 @@ public ApiFuture<Offset> publish(Message message) {
231231
PubSubMessage proto = message.toProto();
232232
try (CloseableMonitor.Hold h = batcherMonitor.enter()) {
233233
ApiService.State currentState = state();
234-
checkState(
235-
currentState == ApiService.State.RUNNING,
236-
"Cannot publish when Publisher state is %s.",
237-
currentState.name());
238-
return batcher.add(proto);
234+
switch (currentState) {
235+
case FAILED:
236+
throw new CheckedApiException(
237+
"Cannot publish when publisher has failed.",
238+
failureCause(),
239+
Code.FAILED_PRECONDITION);
240+
case STARTING:
241+
case RUNNING:
242+
return batcher.add(proto);
243+
default:
244+
throw new CheckedApiException(
245+
"Cannot publish when Publisher state is " + currentState.name(),
246+
Code.FAILED_PRECONDITION);
247+
}
239248
} catch (CheckedApiException e) {
240249
onPermanentError(e);
241250
return ApiFutures.immediateFailedFuture(e);

0 commit comments

Comments
 (0)