Hone logo
Hone
Problems

Building a Streaming RPC Server and Client in Go

This challenge focuses on implementing a streaming Remote Procedure Call (RPC) service in Go using gRPC. Streaming RPCs are crucial for handling large datasets or continuous data flows efficiently, allowing for bidirectional communication between client and server. You'll build both a server and a client that exchange data streams.

Problem Description

You are tasked with creating a simple streaming RPC service that calculates the cumulative sum of a sequence of numbers provided by the client. The server will receive a stream of integers from the client, calculate the cumulative sum at each point, and stream the cumulative sums back to the client.

What needs to be achieved:

  1. Define a gRPC service: Define a service with a single method called CumulativeSum that accepts a stream of integers and returns a stream of cumulative sums.
  2. Implement the server: Create a gRPC server that implements the CumulativeSum method. The server should:
    • Receive a stream of integers from the client.
    • Maintain a running sum.
    • For each integer received, calculate the cumulative sum (running sum + received integer).
    • Stream the calculated cumulative sum back to the client.
  3. Implement the client: Create a gRPC client that:
    • Connects to the server.
    • Sends a stream of integers to the server.
    • Receives the stream of cumulative sums from the server.
    • Prints the received cumulative sums to the console.

Key Requirements:

  • Use gRPC for communication.
  • Implement bidirectional streaming.
  • Handle potential errors gracefully.
  • The server should correctly calculate the cumulative sum.
  • The client should receive and display the cumulative sums.

Expected Behavior:

The client sends a sequence of integers to the server. The server calculates the cumulative sum after receiving each integer and sends it back to the client. The client prints each received cumulative sum.

Edge Cases to Consider:

  • Empty input stream from the client.
  • Large input streams (consider memory usage on the server).
  • Error handling (e.g., server encountering an error during processing).

Examples

Example 1:

Input (Client): [1, 2, 3, 4, 5]
Output (Client): [1, 3, 6, 10, 15]
Explanation: The server receives 1, calculates 1, sends 1. Receives 2, calculates 3, sends 3. Receives 3, calculates 6, sends 6. Receives 4, calculates 10, sends 10. Receives 5, calculates 15, sends 15.

Example 2:

Input (Client): [10, -5, 2, 8]
Output (Client): [10, 5, 7, 15]
Explanation: The server receives 10, calculates 10, sends 10. Receives -5, calculates 5, sends 5. Receives 2, calculates 7, sends 7. Receives 8, calculates 15, sends 15.

Example 3: (Empty Input)

Input (Client): []
Output (Client): []
Explanation: If the client sends an empty stream, the server should not send any cumulative sums.

Constraints

  • The input integers can be positive, negative, or zero.
  • The maximum number of integers in the input stream is 1000.
  • The integers in the input stream will be within the range of -1000 to 1000.
  • The server should respond within a reasonable time (e.g., less than 1 second for a stream of 1000 integers).
  • Use standard Go libraries and the gRPC library.

Notes

  • You'll need to define a protocol buffer (.proto) file to define the gRPC service and messages.
  • Use gRPC's streaming capabilities to implement the bidirectional communication.
  • Consider using goroutines to handle the streaming operations concurrently.
  • Focus on clarity and readability in your code. Error handling is important, but the core functionality of calculating and streaming the cumulative sum is the primary focus.
  • Remember to generate the Go code from your .proto file using the gRPC tools.
Loading editor...
go