grpcdb: Better readability for docs and constructor names

* Added some docs for NewClient, BindServer, *server.Init
* Security level clarified, whether "secure" for https
or "insecure" for non-https gRPC connections.
This commit is contained in:
Emmanuel T Odeke 2018-03-16 14:54:15 -07:00 committed by Christopher Goes
parent 11bee6194a
commit 1260b75f63
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
3 changed files with 35 additions and 8 deletions

View File

@ -6,9 +6,19 @@ import (
protodb "github.com/tendermint/tmlibs/proto"
)
func NewClient(serverAddr string, secure bool) (protodb.DBClient, error) {
// Security defines how the client will talk to the gRPC server.
type Security uint
const (
Insecure Security = iota
Secure
)
// 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 NewClient(serverAddr string, kind Security) (protodb.DBClient, error) {
var opts []grpc.DialOption
if !secure {
if kind == Insecure {
opts = append(opts, grpc.WithInsecure())
}
cc, err := grpc.Dial(serverAddr, opts...)

View File

@ -12,12 +12,12 @@ import (
func Example() {
addr := ":8998"
go func() {
if err := grpcdb.BindRemoteDBServer(addr); err != nil {
log.Fatalf("BindRemoteDBServer: %v", err)
if err := grpcdb.BindServer(addr); err != nil {
log.Fatalf("BindServer: %v", err)
}
}()
client, err := grpcdb.NewClient(addr, false)
client, err := grpcdb.NewClient(addr, grpcdb.Insecure)
if err != nil {
log.Fatalf("Failed to create grpcDB client: %v", err)
}

View File

@ -1,8 +1,8 @@
package grpcdb
import (
"log"
"context"
"log"
"net"
"sync"
"time"
@ -13,7 +13,10 @@ import (
protodb "github.com/tendermint/tmlibs/proto"
)
func BindRemoteDBServer(addr string, opts ...grpc.ServerOption) error {
// BindServer 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 BindServer(addr string, opts ...grpc.ServerOption) error {
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
@ -30,11 +33,24 @@ type server struct {
var _ protodb.DBServer = (*server)(nil)
// Init initializes the server's database. Only one type of database
// can be initialized per server.
//
// Dir is the directory on the file system in which the DB will be stored(if backed by disk) (TODO: remove)
//
// Name is representative filesystem entry's basepath
//
// Type can be either one of:
// * cleveldb (if built with gcc enabled)
// * fsdb
// * memdB
// * leveldb
// See https://godoc.org/github.com/tendermint/tmlibs/db#DBBackendType
func (s *server) Init(ctx context.Context, in *protodb.Init) (*protodb.Entity, error) {
s.mu.Lock()
defer s.mu.Unlock()
log.Printf("in: %+v\n", in)
log.Printf("in: %+v\n", in)
s.db = db.NewDB(in.Name, db.DBBackendType(in.Type), in.Dir)
return &protodb.Entity{TimeAt: time.Now().Unix()}, nil
}
@ -45,6 +61,7 @@ func (s *server) Delete(ctx context.Context, in *protodb.Entity) (*protodb.Nothi
}
var nothing = new(protodb.Nothing)
func (s *server) DeleteSync(ctx context.Context, in *protodb.Entity) (*protodb.Nothing, error) {
s.db.DeleteSync(in.Key)
return nothing, nil