Giter VIP home page Giter VIP logo

grpc-bi-directional-stream's Introduction

Golang GRPC bi-directional stream

Implement a FindMaxNumber RPC Bi-Directional Streaming Client and Server system using Go GRPC framework as following.

  • The function takes a stream of Request message that has one integer, and returns a stream of Responses that represent the current maximum between all these integers.

  • Client will be having a cryptographic public key and client will be identified using his private key. Client will sign every request message in the stream.

  • Each requested message should be verified against the signature at the server end. Only those numbers will be considered to be processed whose sign is successfully verified.

Example: The client will send a stream of number (1,5,3,6,2,20) and each number will be signed by the private key of the client and the server will respond with a stream of numbers (1,5,6,20).

=======================

Firstly needs to define the protobuf file. We also use ecdsa to generate keypair for client. For cryptography function, we create utility functions in cryptoutil package.

Protobuf file includes two messages Request, Response and one rpc FindMaxNumber takes input stream is Request and returns Response stream.

service Math {
        rpc FindMaxNumber(stream Request) returns (stream Response);
}
message Request {
	int32 number = 1;	
	bytes publicKey = 2;
	bytes r = 3;
	bytes s = 4;
}
message Response {
        int32 res = 1;
}

For inconvenient, we create function for generating slice of integers for sending.

Client starts connecting to server and get rpc stream. Using rpc stream to send Request message to server. Request message includes integer number, signature of number and public key of client in bytes.

After sending is complete, we need to call CloseSend of stream to terminate sending process. Client also having Receive stream function, which receiving response from server. The sending and receiving stream runs on two goroutines.

On server side, our grpc server listens on 8888 port. We define FindMaxNumber for listening incoming integers from clients. Server receives Request message, verifies the signature by public key with help of cryptoutil. Server maintains a max variable to check with verified integer. If the received integer is greater than max then updates max and sends the integer back to client.

Source code

  • api: Contains protobuf file and command line for generate pb.go file.

  • client: The main client entry for starting send/receive stream.

  • cryptoutil: Functions for generate keypair, sign, verify and hash.

  • mathclient: Contains struct and function to execute send/receive stream. Each mathclient acts as one client instance.

  • server: Contains grpc server for listening clients connection, stream function for receive/send data.

  • test: Includes some integration and load test cases.

I have fixed bugs related to sign and verify function. Actually, size of signature is not always 64 bytes. It depends on size of r and s. With the number signature, there are many cases the signature size is 63 bytes.

grpc-bi-directional-stream's People

Contributors

huyntsgs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.