Skip to content

Commit

Permalink
Added few more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Shailesh Jagannath Padave committed Nov 29, 2024
1 parent 586ccfc commit 73444a0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.conductoross.grpcbin;

import complex.Complex;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

import java.util.Random;
Expand Down Expand Up @@ -49,7 +51,7 @@ public void sayHelloWithDelay(HelloRequest request, StreamObserver<HelloResponse

System.out.println("Delaying for " + delayInSeconds + " seconds...");
TimeUnit.SECONDS.sleep(delayInSeconds);

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Expand Down Expand Up @@ -94,19 +96,87 @@ public void sayHelloOverloaded(org.conductoross.grpcbin.HelloRequest request,
return;
}

// Simulate some processing (delay) to emulate load
int delay = new Random().nextInt(5) + 1; // Random delay between 1 and 5 seconds
TimeUnit.SECONDS.sleep(delay);

// Simulate some processing time
String message = "Hello, " + request.getName();
HelloResponse response = HelloResponse.newBuilder()
.setMessage(message)
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
// Release the permit after processing
semaphore.release();
}
}

@Override
public void sayHelloWithFailureTypes(org.conductoross.grpcbin.HelloRequest request,
io.grpc.stub.StreamObserver<org.conductoross.grpcbin.HelloResponse> responseObserver) {
Random random = new Random();

// 10% chance of bad request (400)
if (random.nextInt(100) < 10) {
responseObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Bad Request")));
return;
}

// 20% chance of service unavailable (503)
if (random.nextInt(100) < 20) {
responseObserver.onError(new StatusRuntimeException(Status.UNAVAILABLE.withDescription("Service Unavailable")));
return;
}

// 5% chance of rate limiting (429)
if (random.nextInt(100) < 5) {
responseObserver.onError(new StatusRuntimeException(Status.RESOURCE_EXHAUSTED.withDescription("Rate limit exceeded")));
return;
}

// Normal response
String message = "Hello, " + request.getName();
HelloResponse response = HelloResponse.newBuilder()
.setMessage(message)
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();

}

@Override
public void sayHelloWithExternalDependency(org.conductoross.grpcbin.HelloRequest request,
io.grpc.stub.StreamObserver<org.conductoross.grpcbin.HelloResponse> responseObserver) {
// Simulate external system delay (API, DB)
int externalDelay = random.nextInt(10) + 1; // 1 to 10 seconds
try {
System.out.println("Simulating external API delay: " + externalDelay + " seconds");
TimeUnit.SECONDS.sleep(externalDelay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

// Simulate a 10% chance of failure from the external system
if (random.nextInt(100) < 10) {
responseObserver.onError(new RuntimeException("External API failure"));
return;
}

// Generate response based on the external system’s success
String message = "Hello, " + request.getName() + ". Data retrieved successfully from external service!";

// Build the response with dynamic content
HelloResponse response = HelloResponse.newBuilder()
.setMessage(message)
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}

@Override
public void complexRequestStream(Complex.ComplexMessage request, StreamObserver<Complex.ComplexMessage> responseObserver) {
for (int i = 0; i < 100; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/proto/helloworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ service HelloWorldService {
rpc SayHelloWithDelay (HelloRequest) returns (HelloResponse) {}
rpc SayHelloIntermittentFailures (HelloRequest) returns (HelloResponse) {}
rpc SayHelloServiceDown (HelloRequest) returns (HelloResponse) {}
rpc sayHelloWithFailureTypes (HelloRequest) returns (HelloResponse) {}
rpc SayHelloOverloaded (HelloRequest) returns (HelloResponse) {}
rpc sayHelloWithExternalDependency (HelloRequest) returns (HelloResponse) {}
rpc ComplexRequest(complex.ComplexMessage) returns (complex.ComplexMessage) {}
rpc ComplexRequestStream(complex.ComplexMessage) returns (stream complex.ComplexMessage) {}
}
Expand Down

0 comments on commit 73444a0

Please sign in to comment.