knocigrpc

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KVService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "knocidb.KVService",
	HandlerType: (*KVServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "Get",
			Handler:    _KVService_Get_Handler,
		},
		{
			MethodName: "Put",
			Handler:    _KVService_Put_Handler,
		},
		{
			MethodName: "Delete",
			Handler:    _KVService_Delete_Handler,
		},
	},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "Batch",
			Handler:       _KVService_Batch_Handler,
			ClientStreams: true,
		},
		{
			StreamName:    "Scan",
			Handler:       _KVService_Scan_Handler,
			ServerStreams: true,
		},
	},
	Metadata: "grpc/kv.proto",
}

Functions

func EncodeBatchDelete

func EncodeBatchDelete(key []byte) []byte

EncodeBatchDelete 将一条 Delete 操作编码为批处理载荷

func EncodeBatchPut

func EncodeBatchPut(key, value []byte) []byte

EncodeBatchPut 将一条 Put 操作编码为批处理载荷

func ListenOn

func ListenOn(addr string) (net.Listener, error)

ListenOn 在指定地址创建一个 TCP 监听器

func RegisterKVServiceServer

func RegisterKVServiceServer(s *grpc.Server, srv KVServiceServer)

RegisterKVServiceServer 注册 KVService 到 gRPC Server

func Start

func Start(addr string, db *knocidb.DB, opts ...grpc.ServerOption) (*grpc.Server, net.Listener, error)

Start 在指定地址启动 gRPC 服务 可通过 opts 传入 TLS/拦截器等配置

func StartSecure

func StartSecure(addr string, db *knocidb.DB, sec ServerSecurityOptions, extra ...grpc.ServerOption) (*grpc.Server, net.Listener, error)

StartSecure 启动启用了 TLS/鉴权的 gRPC 服务

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func Dial

func Dial(addr string, opts ...grpc.DialOption) (*Client, error)

Dial 以默认配置建立到 gRPC 服务的连接(默认 insecure)

func DialSecure

func DialSecure(addr string, sec ClientSecurityOptions, extra ...grpc.DialOption) (*Client, error)

DialSecure 建立启用了 TLS/鉴权的客户端连接

func (*Client) Batch

func (c *Client) Batch(ctx context.Context, ops [][]byte, opts ...grpc.CallOption) error

Batch 发送批处理操作(客户端流),服务端一次性提交

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端连接

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, key []byte, opts ...grpc.CallOption) error

Delete 删除指定 key

func (*Client) Get

func (c *Client) Get(ctx context.Context, key []byte, opts ...grpc.CallOption) ([]byte, error)

Get 读取指定 key 的值

func (*Client) Put

func (c *Client) Put(ctx context.Context, key, value []byte, opts ...grpc.CallOption) error

Put 写入一条 key/value(内部打包为二进制载荷)

func (*Client) Scan

func (c *Client) Scan(ctx context.Context, start, end []byte, fn func(key, value []byte) bool, opts ...grpc.CallOption) error

Scan 发送范围请求并接收服务端流式返回 回调 fn 返回 false 时提前结束

type ClientSecurityOptions

type ClientSecurityOptions struct {
	TLSCACertFile  string
	TLSServerName  string
	ClientCertFile string
	ClientKeyFile  string
	EnableAuth     bool
	AuthToken      string
}

ClientSecurityOptions 指定客户端的 TLS 与鉴权配置

type KVServiceClient

type KVServiceClient interface {
	Get(context.Context, *wrapperspb.BytesValue, ...grpc.CallOption) (*wrapperspb.BytesValue, error)
	Put(context.Context, *wrapperspb.BytesValue, ...grpc.CallOption) (*emptypb.Empty, error)
	Delete(context.Context, *wrapperspb.BytesValue, ...grpc.CallOption) (*emptypb.Empty, error)
	// Batch 客户端流:发送多条 Put/Delete 操作
	Batch(context.Context, ...grpc.CallOption) (KVService_BatchClient, error)
	// Scan 服务端流:按范围返回 KV
	Scan(context.Context, *wrapperspb.BytesValue, ...grpc.CallOption) (KVService_ScanClient, error)
}

func NewKVServiceClient

func NewKVServiceClient(cc grpc.ClientConnInterface) KVServiceClient

type KVServiceServer

type KVServiceServer interface {
	Get(context.Context, *wrapperspb.BytesValue) (*wrapperspb.BytesValue, error)
	Put(context.Context, *wrapperspb.BytesValue) (*emptypb.Empty, error)
	Delete(context.Context, *wrapperspb.BytesValue) (*emptypb.Empty, error)
	// Batch 客户端流:接收多条操作(Put/Delete),在服务端批量提交
	Batch(KVService_BatchServer) error
	// Scan 服务端流:在B+Tree模式下按范围扫描并流式返回KV
	Scan(*wrapperspb.BytesValue, KVService_ScanServer) error
}

type KVService_BatchClient

type KVService_BatchClient interface {
	Send(*wrapperspb.BytesValue) error
	CloseAndRecv() (*emptypb.Empty, error)
	grpc.ClientStream
}

KVService_BatchClient 客户端流:向服务端发送批处理操作

type KVService_BatchServer

type KVService_BatchServer interface {
	SendAndClose(*emptypb.Empty) error
	Recv() (*wrapperspb.BytesValue, error)
	grpc.ServerStream
}

type KVService_ScanClient

type KVService_ScanClient interface {
	Recv() (*wrapperspb.BytesValue, error)
	grpc.ClientStream
}

type KVService_ScanServer

type KVService_ScanServer interface {
	Send(*wrapperspb.BytesValue) error
	grpc.ServerStream
}

type Server

type Server struct {

	// EnableAuth 指示是否启用基于 token 的鉴权
	EnableAuth bool
	// AuthToken 服务端校验的令牌(通过 metadata: authorization / x-api-key 传入)
	AuthToken string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(db *knocidb.DB) *Server

NewServer 创建一个 gRPC 服务器适配器

func (*Server) Batch

func (s *Server) Batch(stream KVService_BatchServer) error

Batch 客户端流:接收多条操作并批量提交

func (*Server) Delete

func (s *Server) Delete(ctx context.Context, in *wrapperspb.BytesValue) (*emptypb.Empty, error)

Delete 删除指定 key

func (*Server) Get

Get 读取指定 key 的值

func (*Server) Put

Put 写入一条 key/value(payload 自定义二进制:uint32 key_len | key | value)

func (*Server) Scan

func (s *Server) Scan(in *wrapperspb.BytesValue, stream KVService_ScanServer) error

Scan 服务端流:在B+Tree模式下按范围扫描并流式返回KV 请求载荷:uint32 start_len | start | uint32 end_len | end

type ServerSecurityOptions

type ServerSecurityOptions struct {
	TLSCertFile       string
	TLSKeyFile        string
	TLSClientCAFile   string
	RequireClientCert bool
	EnableAuth        bool
	AuthToken         string
}

ServerSecurityOptions 指定服务端的 TLS 与鉴权配置

Jump to

Keyboard shortcuts

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