grpcdb

package
v0.0.0-...-0ce3ae6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2018 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Overview

grpcdb is the distribution of Demars-DMC's db.DB instances using the gRPC transport to decouple local db.DB usages from applications, to using them over a network in a highly performant manner.

grpcdb allows users to initialize a database's server like they would locally and invoke the respective methods of db.DB.

Most users shouldn't use this package, but should instead use remotedb. Only the lower level users and database server deployers should use it, for functionality such as:

ln, err := net.Listen("tcp", "0.0.0.0:0")
srv := grpcdb.NewServer()
defer srv.Stop()
go func() {
	if err := srv.Serve(ln); err != nil {
		t.Fatalf("BindServer: %v", err)
	}
}()

or

addr := ":8998"
cert := "server.crt"
key := "server.key"
go func() {
	if err := grpcdb.ListenAndServe(addr, cert, key); err != nil {
		log.Fatalf("BindServer: %v", err)
	}
}()
Example
package main

import (
	"bytes"
	"context"
	"log"

	grpcdb "github.com/Demars-DMC/Demars-DMC/libs/db/remotedb/grpcdb"
	protodb "github.com/Demars-DMC/Demars-DMC/libs/db/remotedb/proto"
)

func main() {
	addr := ":8998"
	cert := "server.crt"
	key := "server.key"
	go func() {
		if err := grpcdb.ListenAndServe(addr, cert, key); err != nil {
			log.Fatalf("BindServer: %v", err)
		}
	}()

	client, err := grpcdb.NewClient(addr, cert)
	if err != nil {
		log.Fatalf("Failed to create grpcDB client: %v", err)
	}

	ctx := context.Background()
	// 1. Initialize the DB
	in := &protodb.Init{
		Type: "leveldb",
		Name: "grpc-uno-test",
		Dir:  ".",
	}
	if _, err := client.Init(ctx, in); err != nil {
		log.Fatalf("Init error: %v", err)
	}

	// 2. Now it can be used!
	query1 := &protodb.Entity{Key: []byte("Project"), Value: []byte("Tmlibs-on-gRPC")}
	if _, err := client.SetSync(ctx, query1); err != nil {
		log.Fatalf("SetSync err: %v", err)
	}

	query2 := &protodb.Entity{Key: []byte("Project")}
	read, err := client.Get(ctx, query2)
	if err != nil {
		log.Fatalf("Get err: %v", err)
	}
	if g, w := read.Value, []byte("Tmlibs-on-gRPC"); !bytes.Equal(g, w) {
		log.Fatalf("got= (%q ==> % X)\nwant=(%q ==> % X)", g, g, w, w)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr, cert, key string, opts ...grpc.ServerOption) error

ListenAndServe is a blocking function that sets up a gRPC based server at the address supplied, with the gRPC options passed in. Normally in usage, invoke it in a goroutine like you would for http.ListenAndServe.

func NewClient

func NewClient(serverAddr, serverCert string) (protodb.DBClient, error)

NewClient creates a gRPC client connected to the bound gRPC server at serverAddr. Use kind to set the level of security to either Secure or Insecure.

func NewServer

func NewServer(cert, key string, opts ...grpc.ServerOption) (*grpc.Server, error)

Types

type Security

type Security uint

Security defines how the client will talk to the gRPC server.

const (
	Insecure Security = iota
	Secure
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL