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:
parent
11bee6194a
commit
1260b75f63
|
@ -6,9 +6,19 @@ import (
|
||||||
protodb "github.com/tendermint/tmlibs/proto"
|
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
|
var opts []grpc.DialOption
|
||||||
if !secure {
|
if kind == Insecure {
|
||||||
opts = append(opts, grpc.WithInsecure())
|
opts = append(opts, grpc.WithInsecure())
|
||||||
}
|
}
|
||||||
cc, err := grpc.Dial(serverAddr, opts...)
|
cc, err := grpc.Dial(serverAddr, opts...)
|
||||||
|
|
|
@ -12,12 +12,12 @@ import (
|
||||||
func Example() {
|
func Example() {
|
||||||
addr := ":8998"
|
addr := ":8998"
|
||||||
go func() {
|
go func() {
|
||||||
if err := grpcdb.BindRemoteDBServer(addr); err != nil {
|
if err := grpcdb.BindServer(addr); err != nil {
|
||||||
log.Fatalf("BindRemoteDBServer: %v", err)
|
log.Fatalf("BindServer: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
client, err := grpcdb.NewClient(addr, false)
|
client, err := grpcdb.NewClient(addr, grpcdb.Insecure)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create grpcDB client: %v", err)
|
log.Fatalf("Failed to create grpcDB client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package grpcdb
|
package grpcdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"context"
|
"context"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -13,7 +13,10 @@ import (
|
||||||
protodb "github.com/tendermint/tmlibs/proto"
|
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)
|
ln, err := net.Listen("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -30,11 +33,24 @@ type server struct {
|
||||||
|
|
||||||
var _ protodb.DBServer = (*server)(nil)
|
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) {
|
func (s *server) Init(ctx context.Context, in *protodb.Init) (*protodb.Entity, error) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
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)
|
s.db = db.NewDB(in.Name, db.DBBackendType(in.Type), in.Dir)
|
||||||
return &protodb.Entity{TimeAt: time.Now().Unix()}, nil
|
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)
|
var nothing = new(protodb.Nothing)
|
||||||
|
|
||||||
func (s *server) DeleteSync(ctx context.Context, in *protodb.Entity) (*protodb.Nothing, error) {
|
func (s *server) DeleteSync(ctx context.Context, in *protodb.Entity) (*protodb.Nothing, error) {
|
||||||
s.db.DeleteSync(in.Key)
|
s.db.DeleteSync(in.Key)
|
||||||
return nothing, nil
|
return nothing, nil
|
||||||
|
|
Loading…
Reference in New Issue