Giter Club home page Giter Club logo

grpcfd's Introduction

grpcfd is a simple set of tools to allow sending unix file descriptors over grpc when the grpc connection utilizes unix file sockets.

Introduction

On a POSIX system (ie Linux), whenever a process opens a file, it had an integer 'file descriptor' (usually abbreviated fd) that acts as a handle for that open file.

On a POSIX system, an open fd can be sent over an open socket connection to another process if and only if that socket connection is over a unix file socket. This allows the other process to access that open file itself, even if it normally couldn't open it because the file was in a different mount namespace.

Since in POSIX almost everything is a file, this means shared memory, normal files, even sockets themselves can be passed along with a GRPC RPC call.

GRPC is a popular high-performance, open source universal RPC framework with excellent Go support

When using the GRPC Go implementation, you do not normally have access to the net.Conn, and thus would not be able to send file descriptors over a connection in use for GRPC.

Client Setup

Simply wrap whatever normal transport credentials you use (nil is an acceptable value) using go grpcfd.TransportCredentials as a go grpc.WithTransportCredentials go grpc.DialOption when dialing:

var creds credentials.TransportCredentials
cc, err := grpc.DialContext(ctx ,grpc.WithTransportCredentials(grpcfd.TransportCredentials(creds)))

Note: If your client already using PerRPCCredentials by default consider to use:

var creds credentials.TransportCredentials
cc, err := grpc.DialContext(ctx, grpc.WithTransportCredentials(grpcfd.TransportCredentials(creds)), grpcfd.WithChainStreamInterceptor(), grpcfd.WithChainUnaryInterceptor())

This allows to grpcfd do not overwrite your grpc.PerRPCCredentials.

Server Setup

Simply wrap whatever normal credentials you use (nil is an acceptable value) using go grpcfd.TransportCredentials as a go grpc.Creds go grpc.ServerOption when creating a go *grpc.Server

var creds credentials.TransportCredentials
server := grpc.NewServer(grpc.Creds(grpcfd.TransportCredentials(creds))

Client To Server Example

Client sending a file descriptor (fd) to Server

// Wrap existing grpc.PerRPCCredentials using grpcfd.PerRPCredentials(...)
perRPCCredentials := grpcfd.PerRPCCredentials(perRPCCredentialsCanBeNilHere)
// Extract a grpcfd.FDSender from the rpcCredentials
sender, _ := grpcfd.FromPerRPCCredentials(perRPCCredentials)
// Send a file
errCh := sender.SendFilename(filename)
select {
case err := <-errCh:
    // If and error is immediately return... the file probably doesn't exist, handle that immediately
default:
    // Don't wait for any subsequent errors... they won't arrive till after we've sent the GRPC message
    // errCh will be closed after File is sent
}
client.MyRPC(ctx,arg,grpc.PerRPCCredentials(perRPCCredentials))

Server receiving a file descriptor (fd) from a Client

func (*myRPCImpl) MyRPC(ctx,arg) {
    // Extract a grpcfd.FDRecver from the ctx.  ok == false if one is not available
    recv, ok := grpcfd.FromContext(ctx)
    // Attempt to receive a filed by using a URL of the form inode://{{dev}}/{{ino}} where dev and ino are the values
    // from 
    fileCh, err := recv.RecvFileByURL(inodeURLStr)
}

Server To Client Example

Server sending a file descriptor (fd) to Client

func (*myRPCImpl) MyRPC(ctx,arg) {
    // Extract a grpcfd.FDRecver from the ctx.  ok == false if one is not available
    sender, ok := grpcfd.FromContext(ctx)
    // Send a file 
    errCh := sender.SendFilename(filename)
    select {
    case err := <-errCh:
        // If and error is immediately return... the file probably doesn't exist, handle that immediately
    default:
        // Don't wait for any subsequent errors... they won't arrive till after we've sent the GRPC message
        // errCh will be closed after File is sent
    }
    ...
    return
}

Client receiving file descriptor (fd) from Server

// Wrap existing grpc.PerRPCCredentials using grpcfd.PerRPCredentials(...)
perRPCCredentials := grpcfd.PerRPCCredentials(perRPCCredentialsCanBeNilHere)
// Extract a grpcfd.FDRecver from the ctx.  ok == false if one is not available
recv, ok := grpcfd.FromContext(ctx)
client.MyRPC(ctx,arg,grpc.PerRPCCredentials(perRPCCredentials))
// Attempt to receive a filed by using a URL of the form inode://{{dev}}/{{ino}} where dev and ino are the values
// from 
fileCh, err := recv.RecvFileByURL(inodeURLStr)

grpcfd's People

Contributors

denis-tingaikin avatar edwarnicke avatar mixaster995 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.