Skip to content

Commit

Permalink
chore: Generify the scalar cache functions (#389)
Browse files Browse the repository at this point in the history
Create a batch version of the existing generic execute grpc function
and move setBatch and getBatch to it.

Switch the rest of the scalar calls to use the existing generic grpc
execution function. This provides a single point where we can do things
like limit concurrent calls.
  • Loading branch information
nand4011 committed Sep 6, 2024
1 parent 41ba4b5 commit ff54fea
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 204 deletions.
38 changes: 38 additions & 0 deletions momento-sdk/src/main/java/momento/sdk/ClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import io.grpc.Metadata;
import io.grpc.stub.AbstractFutureStub;
import io.grpc.stub.MetadataUtils;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -62,4 +66,38 @@ public void onFailure(@Nonnull Throwable e) {

return returnFuture;
}

protected <SdkResponse, GrpcResponse> CompletableFuture<SdkResponse> executeGrpcBatchFunction(
Consumer<StreamObserver<GrpcResponse>> stubMethod,
Function<List<GrpcResponse>, SdkResponse> successFunction,
Function<Throwable, SdkResponse> errorFunction) {

final CompletableFuture<SdkResponse> future = new CompletableFuture<>();

try {
stubMethod.accept(
new StreamObserver<GrpcResponse>() {
private final List<GrpcResponse> responses = new ArrayList<>();

@Override
public void onNext(GrpcResponse response) {
responses.add(response);
}

@Override
public void onError(Throwable t) {
future.complete(errorFunction.apply(t));
}

@Override
public void onCompleted() {
future.complete(successFunction.apply(responses));
}
});
} catch (Exception e) {
future.complete(errorFunction.apply(e));
}

return future;
}
}
Loading

0 comments on commit ff54fea

Please sign in to comment.