ballerina/grpc package

Package overview

This package contains functions to support gRPC protocol based communication. gRPC is a framework developed by Google to support the RPC (Remote Procedure Call) protocol. The gRPC protocol is layered over HTTP/2. This protocol only supports client-initiated communication.

Protocol buffers

This is a mechanism to serialize the structured data introduced by Google and used by the gRPC framework. Specify the structured data that needs to be serialized in the .proto file. This package contains functions to automatically generate the .proto file based on the service definition of a gRPC service. A sample .proto file is shown below.

syntax = "proto3";
import "google/protobuf/wrappers.proto";
service ServerStreaming {
     rpc receiveMessage(google.protobuf.StringValue) returns (stream google.protobuf.StringValue);
}

gRPC allows client applications to directly call the server side methods using the auto-generated client stubs. Protocol buffer compilers are used to generate the stubs for the specified language. In Ballerina, the client stubs are generated using the in-built proto compiler.

Endpoints

Endpoints specify how the client and server communicate. This package supports service endpoints and client endpoints. The service endpoint specifies how the client communicates with the server. The client endpoint is automatically generated by passing the .proto file of a service to a proto compiler.

The Ballerina tooling distribution provides a proto compiler to generate the Ballerina client endpoint. The client endpoint generated using the Ballerina proto compiler has two client endpoints. Use the endpoint to suit the use case.

RPC types

RPC has four types of client to service communication methods, such as unary, server streaming, client streaming, and bidirectional streaming. All these communication types are supported by this package. Listeners are used to support the asynchronous and streaming communication.

Unary

The server sends a response to each client request. Unary supports blocking and non-blocking communication.

Server streaming

The server sends multiple responses in an asynchronous manner for each client request.

Client streaming

The client sends multiple requests while the server sends a single response to these requests.

Bidirectional Streaming

The client and server send multiple requests and responses.

Samples

Unary communication

The sample given below contains a service that sends a response to each request.

// Server endpoint configuration.
endpoint grpc:Listener ep {
   host:"localhost",
   port:9090
};

// The gRPC service which binds to the server endpoint.
service SamplegRPCService bind ep {
   // A resource that accepts a string message.
   receiveMessage (endpoint caller, string name) {
       // Print the received message.
       io:println("Received message from : " + name);
       // Send the response to the client.
       error? err = caller->send("Hi " + name + "! Greetings from gRPC service!");

       // After sending the response, call ‘complete()’ to indicate that the request was completely sent.
       _ = caller->complete();
   }
}

A proto file is automatically generated after this service is run. It is generated in the same folder as the service.

The sample given below calls the above service in a blocking manner using an auto-generated Ballerina client stub.

// Use ‘BlockingClient’ to execute the call in the blocking mode.
endpoint SamplegRPCServiceBlockingClient SamplegRPCServiceBlockingEp {
   host:"localhost",
   port:9090
};

// Create gRPC headers.
grpc:Headers headers = new;
headers.setEntry("id", "newrequest1");

// Call the method in the service using a client stub.
var responseFromServer = SamplegRPCServiceBlockingEp->receiveMessage("Ballerina", headers = headers);
match responseFromServer {
   // If a response is received, print the payload.
   (string, grpc:Headers) payload => {
       string result;
       grpc:Headers resHeaders;
       (result, resHeaders) = payload;
       io:println("Response received : " + result);
   }
   // If an error is returned, print the error message.
   error err => {
       io:println("Error while connecting grpc end-point : " + err.message);
   }
}

Server Streaming

The sample given below shows a server streaming service.

// Server endpoint configuration.
endpoint grpc:Listener ep {
   host:"localhost",
   port:9090
};

service ServerStreaming bind ep {
   // Set the Streaming Annotation to ‘true’. It specifies that the resource is capable of sending multiple responses per request.
   @grpc:ResourceConfig {streaming:true}
   receiveMessage(endpoint caller, string name) {
       string[] greets = ["Hi", "Welcome"];
       io:println("HelloWorld");
       // Send multiple responses to the client.
       foreach greet in greets {
           error? err = caller->send(greet + " " + name + "! Greetings from Ballerina service");
           // If an error is returned, print the error message. print response message otherwise.
           io:println(err.message but { () => "send reply: " + greet + " " + name });
       }
       // Once the messages are sent to the server, call ‘complete()’ to indicate that the request was completely sent.
       _ = caller->complete();
   }
}

The sample given below calls the above service using the auto-generated Ballerina client stub and listens to the multiple responses from server.

// Keep track of the message that were completely received.
boolean isCompleted = false;
function main (string... args) {
   // Client endpoint configurations.
    endpoint ServerStreamingClient serverStreamingEp {
       host: "localhost",
       port: 9090
    };

    // Execute the service streaming call by registering a message listener.
    error? result = serverStreamingEp->receiveMessage("test", ServerStreamingMessageListener);
    match result {
         // If the service returns an error, print the error.
        error payloadError => {
            io:println("Error occured while sending event " + payloadError.message);
        }
        () => {
            io:println("Connected successfully to service");
        }
    }
   // Waits for the service to send the message.
   while (!isCompleted) {}
}

// Define a listener service to receive the messages from the server.
service<grpc:Service> ServerStreamingMessageListener {
   // This resource method is invoked when the service receives a message.
   onMessage (string message) {
       io:println("Response received from server: " + message);
   }
   
   // This resource method is invoked if an error is returned.
   onError (error err) {
       if (err != ()) {
           io:println("Error reported from server: " + err.message);
       }
   }

   // Invoke this resource when the server sends all the responses to the request.
   onComplete () {
       isCompleted = true;
       io:println("Server Complete Sending Responses.");
   }
}

Bidirectional Streaming

The sample given below includes a service that handles bidirectional streaming.

// Set the ‘clientStreaming’ and ‘serverStreaming’ to true. It specifies that the service supports bidirectional streaming.
@grpc:ServiceConfig {
    name: "chat",
    clientStreaming: true,
    serverStreaming: true
}
service Chat bind ep {
    // This resource method is invoked when there is a connection request from a client.
   onOpen(endpoint caller) {
       
   }
    // This resource method is invoked when the client sends a request.
   onMessage(endpoint caller, string message) {
   
   }
    // This resource method is invoked when the client returns an error while sending requests.
   onError(endpoint caller, error err) {
   
   }
    // This resource method is invoked when the client has finished sending requests.
   onComplete(endpoint caller) {
   
   }
}

The sample given below calls the above service.

endpoint grpc:Client ep;

// Call the relevant service.
var res = chatEp -> chat(ChatMessageListener);
match res {
   // If an error is returned while connecting to the service, print the error.
   error err => {
       io:print("error");
   }
   grpc:Client con => {
       ep = con;
   }
}

// Send multiple messages to the service.
error connErr = ep -> send(“Hello”);

// After sending the message, call ‘complete()’ to indicate that the request was completely sent. 
_ = ep -> complete();

Type Definitions

Type Values Description
Chunking NEVER | AUTO | ALWAYS
Compression NEVER | AUTO | ALWAYS
TransferEncoding CHUNKING

Annotations

Name Attachement Points Data Type Description
ResourceConfig resource GrpcResourceConfig

gRPC service resource configuration annotation.

ServiceConfig service GrpcServiceConfig

gRPC service configuration annotation.

ServiceDescriptor service ServiceDescriptorData

gRPC service internal annotation which is to attach service descriptor generated at compile time.

Records Summary

Record Description
ClientEndpointConfig

Represents the gRPC client endpoint configuration.

GrpcResourceConfig

gRPC service resource configuration.

GrpcServiceConfig

gRPC service configuration.

KeyStore

KeyStore record represents key store related options to be used for HTTP client/service invocation.

PayloadError

Represent all http payload related.

Protocols

Protocols record represents SSL/TLS protocol related options to be used for HTTP client/service invocation.

SecureSocket

SecureSocket struct represents SSL/TLS options to be used for gRPC client invocation.

ServiceDescriptorData

gRPC service descriptor data.

ServiceEndpointConfiguration

Represents the gRPC server endpoint configuration.

ServiceOcspStapling

OcspStapling record represents options related to check whether a certificate is revoked or not.

ServiceSecureSocket

SecureSocket struct represents SSL/TLS options to be used for gRPC service.

TrustStore

TrustStore record represents trust store related options to be used for HTTP client/service invocation.

ValidateCert

ValidateCert record represents options related to check whether a certificate is revoked or not.

Objects Summary

Object Description
CallerAction
Client

Represents the gRPC client endpoint.

GrpcClient

Represents the gRPC client.

Headers
Listener

Represents the gRPC service endpoint.

Service
Stub

gRPC Service Stub for outbound gRPC requests.

Global Variables

Name Data Type Description
CHUNKING_ALWAYS Chunking
CHUNKING_AUTO Chunking
CHUNKING_NEVER Chunking
COMPRESSION_ALWAYS Compression
COMPRESSION_AUTO Compression
COMPRESSION_NEVER Compression
TRANSFERENCODE_CHUNKING TransferEncoding

public type ClientEndpointConfig

Represents the gRPC client endpoint configuration.

Field Name Data Type Default Value Description
url string
  • The server url.
secureSocket SecureSocket
  • The SSL configurations for the client endpoint.

public type GrpcResourceConfig

gRPC service resource configuration.

Field Name Data Type Default Value Description
streaming boolean
  • gRPC server streaming flag. This flag sets to true when service resource is considered as server streaming.

public type GrpcServiceConfig

gRPC service configuration.

Field Name Data Type Default Value Description
name string
  • gRPC resource name. This applies only for client streaming and bidirectional streaming where we can define only one resource. In order to generate proto file, we need resource name.
clientStreaming boolean
  • gRPC client streaming service flag. This applies only for servicestub streaming and bidirectional streaming. Flag sets to true, if the service is client/bidirectional streaming.
serverStreaming boolean
  • gRPC server streaming service flag. This applies only for client streaming and bidirectional streaming. Flag sets to true, if the service is bidirectional streaming.

public type KeyStore

KeyStore record represents key store related options to be used for HTTP client/service invocation.

Field Name Data Type Default Value Description
path string

File path to key store file

password string

Key store password

public type PayloadError

Represent all http payload related.

Field Name Data Type Default Value Description
message string

The error message

cause error

The error which caused the entity error

public type Protocols

Protocols record represents SSL/TLS protocol related options to be used for HTTP client/service invocation.

Field Name Data Type Default Value Description
name string

SSL Protocol to be used. eg TLS1.2

versions string[]

SSL/TLS protocols to be enabled. eg TLSv1,TLSv1.1,TLSv1.2

public type SecureSocket

SecureSocket struct represents SSL/TLS options to be used for gRPC client invocation.

Field Name Data Type Default Value Description
trustStore TrustStore
  • TrustStore related options.
keyStore KeyStore
  • KeyStore related options.
protocol Protocols
  • SSL/TLS protocol related options.
certValidation ValidateCert
  • Certificate validation against CRL or OCSP related options.
ciphers string[]
  • List of ciphers to be used. eg: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA.
verifyHostname boolean true
  • Enable/disable host name verification.
shareSession boolean true
  • Enable/disable new ssl session creation.
ocspStapling boolean
  • Enable/disable ocsp stapling.

public type ServiceDescriptorData

gRPC service descriptor data.

Field Name Data Type Default Value Description
descriptor string
  • gRPC server descriptor. Service descriptor sets at compile time.

public type ServiceEndpointConfiguration

Represents the gRPC server endpoint configuration.

Field Name Data Type Default Value Description
host string
  • The server hostname.
port int
  • The server port.
secureSocket ServiceSecureSocket
  • The SSL configurations for the client endpoint.

public type ServiceOcspStapling

OcspStapling record represents options related to check whether a certificate is revoked or not.

Field Name Data Type Default Value Description
enable boolean

The status of OcspStapling

cacheSize int

Maximum size of the cache

cacheValidityPeriod int

Time duration of cache validity period

public type ServiceSecureSocket

SecureSocket struct represents SSL/TLS options to be used for gRPC service.

Field Name Data Type Default Value Description
trustStore TrustStore
  • TrustStore related options.
keyStore KeyStore
  • KeyStore related options.
protocol Protocols
  • SSL/TLS protocol related options.
certValidation ValidateCert
  • Certificate validation against CRL or OCSP related options.
ciphers string[]
  • List of ciphers to be used. eg: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA.
sslVerifyClient string
  • The type of client certificate verification.
shareSession boolean true
  • Enable/disable new ssl session creation.
ocspStapling ServiceOcspStapling
  • Enable/disable ocsp stapling.

public type TrustStore

TrustStore record represents trust store related options to be used for HTTP client/service invocation.

Field Name Data Type Default Value Description
path string

File path to trust store file

password string

Trust store password

public type ValidateCert

ValidateCert record represents options related to check whether a certificate is revoked or not.

Field Name Data Type Default Value Description
enable boolean

The status of validateCertEnabled

cacheSize int

Maximum size of the cache

cacheValidityPeriod int

Time duration of cache validity period

public type CallerAction object

  • <CallerAction> send(any res) returns (error)

    Sends outbound response to the caller.

    Parameter Name Data Type Default Value Description
    res any
    • The outbound response message.
    Return Type Description
    error
  • <CallerAction> complete() returns (error)

    Informs the caller, server finished sending messages.

    Return Type Description
    error
  • <CallerAction> isCancelled() returns (boolean)

    Checks whether the connection is closed by the caller.

    Return Type Description
    boolean
  • <CallerAction> sendError(int statusCode, string message) returns (error)

    Sends server error to the caller.

    Parameter Name Data Type Default Value Description
    statusCode int
    • Error status code.
    message string
    • Error message.
    Return Type Description
    error

public type Client object

Represents the gRPC client endpoint.

  • <Client> init(ClientEndpointConfig config)

    Gets called when the endpoint is being initialize during package init time.

    Parameter Name Data Type Default Value Description
    config ClientEndpointConfig
    • The ClientEndpointConfig of the endpoint.
  • <Client> register(typedesc serviceType)

    Gets called every time a service attaches itself to this endpoint - also happens at package init time. Not supported in client connector.

    Parameter Name Data Type Default Value Description
    serviceType typedesc
    • The type of the service to be registered.
  • <Client> start()

    Starts the registered service.

  • <Client> stop()

    Stops the registered.

  • <Client> getCallerActions() returns (GrpcClient)

    Returns the client connection that servicestub code uses.

    Return Type Description
    GrpcClient

public type GrpcClient object

Represents the gRPC client.

Field Name Data Type Default Value Description
port int
  • The server port.
host string
  • The server host name.
  • <GrpcClient> send(any res) returns (error)

    Sends request message to the server.

    Parameter Name Data Type Default Value Description
    res any
    • The inbound request message.
    Return Type Description
    error
  • <GrpcClient> complete() returns (error)

    Informs the server, caller finished sending messages.

    Return Type Description
    error
  • <GrpcClient> sendError(int statusCode, string message) returns (error)

    Sends error response to the server.

    Parameter Name Data Type Default Value Description
    statusCode int
    • Error status code.
    message string
    • Error message.
    Return Type Description
    error

public type Headers object

  • <Headers> exists(string headerName) returns (boolean)

    Check whether the requested header exists.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
    Return Type Description
    boolean
  • <Headers> get(string headerName) returns (string)

    Returns the header value with the specified header name. If there are more than one header value for the specified header name, the first value is returned.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
    Return Type Description
    string
  • <Headers> getAll(string headerName) returns (string[])

    Gets transport headers from the request.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
    Return Type Description
    string[]
  • <Headers> setEntry(string headerName, string headerValue)

    Sets the value of a transport header.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
    headerValue string
    • The header value.
  • <Headers> addEntry(string headerName, string headerValue)

    Adds the specified key/value pair as an HTTP header to the request.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
    headerValue string
    • The header value.
  • <Headers> remove(string headerName)

    Removes a transport header from the request.

    Parameter Name Data Type Default Value Description
    headerName string
    • The header name.
  • <Headers> removeAll()

    Removes all transport headers from the message.

public type Listener object

Represents the gRPC service endpoint.

Field Name Data Type Default Value Description
id int
  • <Listener> init(ServiceEndpointConfiguration config)

    Gets called when the endpoint is being initialize during package init time.

    Parameter Name Data Type Default Value Description
    config ServiceEndpointConfiguration
    • The ServiceEndpointConfiguration of the endpoint.
  • <Listener> register(typedesc serviceType)

    Gets called every time a service attaches itself to this endpoint - also happens at package init time. Not supported in client connector.

    Parameter Name Data Type Default Value Description
    serviceType typedesc
    • The type of the service to be registered.
  • <Listener> start()

    Starts the registered service.

  • <Listener> stop()

    Stops the registered service.

  • <Listener> getCallerActions() returns (CallerAction)

    Returns the client connection that servicestub code uses.

    Return Type Description
    CallerAction

public type Service object

public type Stub object

gRPC Service Stub for outbound gRPC requests.

Field Name Data Type Default Value Description
client Client
  • <Stub> initStub(any clientEndpoint, string stubType, string descriptorKey, map descriptorMap)

    Init native function for initialize the Stub.

    Parameter Name Data Type Default Value Description
    clientEndpoint any
    • client endpoint struct.
    stubType string
    • Service Stub type. possible values: blocking, nonblocking.
    descriptorKey string
    • Proto descriptor key. Key of proto descriptor.
    descriptorMap map
    • Proto descriptor map. descriptor map with all dependent descriptors.
  • <Stub> blockingExecute(string methodID, any payload) returns ((any,Headers) | error)

    Blocking execute function implementation of the gRPC client stub.

    Parameter Name Data Type Default Value Description
    methodID string
    • remote procedure call id.
    payload any
    • Any type of request payload.
    Return Type Description
    (any,Headers) | error
  • <Stub> nonBlockingExecute(string methodID, any payload, typedesc listenerService) returns (error)

    Non Blocking execute function implementation of the gRPC client stub.

    Parameter Name Data Type Default Value Description
    methodID string
    • remote procedure call id.
    payload any
    • Any type of request payload.
    listenerService typedesc
    • call back listener service.
    Return Type Description
    error
  • <Stub> streamingExecute(string methodID, typedesc listenerService) returns (Client | error)

    Blocking execute function implementation of the gRPC client stub.

    Parameter Name Data Type Default Value Description
    methodID string
    • remote procedure call id.
    listenerService typedesc
    • call back listener service.
    Return Type Description
    Client | error