rplx

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2019 License: MIT Imports: 13 Imported by: 1

README

RPLX Go Report Card

Golang library for multi master replication integer variables with TTL support

Send replication data to remote nodes

Sending replication data to remote node occurs on:

  • reached SyncInterval
  • internal node buffer size exceeded limit (defaultMaxBufferSize)

Usage example

  • init clients
// Client1

grpcListener, _ = net.Listen("tcp4", "127.0.0.1:2001")

// create new rplx instance
r1 := rplx.New("nodeID-1")
go r1.StartReplicationServer(grpcListener)

// add client2 for replication 
r1.AddNode("nodeID-2", "127.0.0.1:2002", time.Second, grpc.WithInsecure())
// Client2

grpcListener, _ = net.Listen("tcp4", "127.0.0.1:2002")

// create new rplx instance
r2 := rplx.New("nodeID-2")
go r2.StartReplicationServer(grpcListener)

// add client1 for replication 
r2.AddNode("nodeID-1", "127.0.0.1:2001", time.Second, grpc.WithInsecure())
  • change variable on client1
r1.Upsert("var1", 42)
  • get variable on client2 after 1 second (sync interval)
value, _ := r2.Get("var1") 

// 42
  • update variable on client2
r2.Upsert("var1", -100)
  • get variable on client1 after 1 second (sync interval)
value, _ := r1.Get("var1") 

// -58

API

Upsert

// todo

Get

// todo

Delete

// todo

UpdateTTL

UpdateTTL(variableName string, TTL time.Time) error

Updates TTL for variable. Returns error ErrVariableNotExists if not exists

Notes

  • call delete method on variable will set TTL in Now - 1sec for this variable

ToDo

  • garbage expired variable
  • use sync.Pool for reusable structs
  • if send to remote node failed, return variables to internal node buffer (?) Check for stamp for while resend to buffer

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNodeAlreadyExists describe error for add already exists node
	ErrNodeAlreadyExists = errors.New("node already exists")
)
View Source
var (
	// ErrVariableNotExists describe error for non exists variable
	ErrVariableNotExists = errors.New("variable not exists")
)

Functions

func RegisterReplicatorServer

func RegisterReplicatorServer(s *grpc.Server, srv ReplicatorServer)

Types

type Option

type Option func(*Rplx)

Option describe Rplx option

func WithGCInterval

func WithGCInterval(interval time.Duration) Option

WithGCInterval option for set garbage collect interval

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger option for specify logger

func WithNodeMaxBufferSize

func WithNodeMaxBufferSize(s int) Option

WithNodeMaxBufferSize option for set node max buffer size

func WithReplicationChanCap

func WithReplicationChanCap(c int) Option

WithReplicationChanCap option for set replication channel capacity

type ReplicatorClient

type ReplicatorClient interface {
	Sync(ctx context.Context, in *SyncRequest, opts ...grpc.CallOption) (*SyncResponse, error)
}

ReplicatorClient is the client API for Replicator service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewReplicatorClient

func NewReplicatorClient(cc *grpc.ClientConn) ReplicatorClient

type ReplicatorServer

type ReplicatorServer interface {
	Sync(context.Context, *SyncRequest) (*SyncResponse, error)
}

ReplicatorServer is the server API for Replicator service.

type Rplx

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

Rplx describe main Rplx object

func New

func New(nodeID string, opts ...Option) *Rplx

New creates new Rplx

func (*Rplx) AddNode

func (rplx *Rplx) AddNode(nodeID string, addr string, syncInterval time.Duration, opts grpc.DialOption) error

AddNode adds and serve new remote node

func (*Rplx) Delete

func (rplx *Rplx) Delete(name string) error

Delete sets for variable ttl with -1 sec from Now, sends variable to replication (update TTL on clients) and remove variable from rplx.variables map

func (*Rplx) Get

func (rplx *Rplx) Get(name string) (int64, error)

Get returns variable v or error if variable not exists or expired if variable expired, removes variable from rplx.variables map

func (*Rplx) StartReplicationServer

func (rplx *Rplx) StartReplicationServer(ln net.Listener) error

StartReplicationServer starts grpc server for receive sync messages from remote nodes

func (*Rplx) Sync

func (rplx *Rplx) Sync(ctx context.Context, mes *SyncRequest) (*SyncResponse, error)

Sync is GRPC function, fired on incoming sync message

func (*Rplx) UpdateTTL

func (rplx *Rplx) UpdateTTL(name string, ttl time.Time) error

UpdateTTL updates TTL for variable or return error if variable not exists

func (*Rplx) Upsert

func (rplx *Rplx) Upsert(name string, delta int64)

Upsert change variable on delta or create variable, if not exists

type SyncNodeValue

type SyncNodeValue struct {
	ID                   string   `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
	Value                int64    `protobuf:"varint,2,opt,name=Value,proto3" json:"Value,omitempty"`
	Stamp                int64    `protobuf:"varint,3,opt,name=Stamp,proto3" json:"Stamp,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*SyncNodeValue) Descriptor

func (*SyncNodeValue) Descriptor() ([]byte, []int)

func (*SyncNodeValue) GetID

func (m *SyncNodeValue) GetID() string

func (*SyncNodeValue) GetStamp

func (m *SyncNodeValue) GetStamp() int64

func (*SyncNodeValue) GetValue

func (m *SyncNodeValue) GetValue() int64

func (*SyncNodeValue) ProtoMessage

func (*SyncNodeValue) ProtoMessage()

func (*SyncNodeValue) Reset

func (m *SyncNodeValue) Reset()

func (*SyncNodeValue) String

func (m *SyncNodeValue) String() string

func (*SyncNodeValue) XXX_DiscardUnknown

func (m *SyncNodeValue) XXX_DiscardUnknown()

func (*SyncNodeValue) XXX_Marshal

func (m *SyncNodeValue) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*SyncNodeValue) XXX_Merge

func (dst *SyncNodeValue) XXX_Merge(src proto.Message)

func (*SyncNodeValue) XXX_Size

func (m *SyncNodeValue) XXX_Size() int

func (*SyncNodeValue) XXX_Unmarshal

func (m *SyncNodeValue) XXX_Unmarshal(b []byte) error

type SyncRequest

type SyncRequest struct {
	NodeID string `protobuf:"bytes,1,opt,name=NodeID,proto3" json:"NodeID,omitempty"`
	// map key - variable name
	Variables            map[string]*SyncVariable `` /* 159-byte string literal not displayed */
	XXX_NoUnkeyedLiteral struct{}                 `json:"-"`
	XXX_unrecognized     []byte                   `json:"-"`
	XXX_sizecache        int32                    `json:"-"`
}

func (*SyncRequest) Descriptor

func (*SyncRequest) Descriptor() ([]byte, []int)

func (*SyncRequest) GetNodeID

func (m *SyncRequest) GetNodeID() string

func (*SyncRequest) GetVariables

func (m *SyncRequest) GetVariables() map[string]*SyncVariable

func (*SyncRequest) ProtoMessage

func (*SyncRequest) ProtoMessage()

func (*SyncRequest) Reset

func (m *SyncRequest) Reset()

func (*SyncRequest) String

func (m *SyncRequest) String() string

func (*SyncRequest) XXX_DiscardUnknown

func (m *SyncRequest) XXX_DiscardUnknown()

func (*SyncRequest) XXX_Marshal

func (m *SyncRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*SyncRequest) XXX_Merge

func (dst *SyncRequest) XXX_Merge(src proto.Message)

func (*SyncRequest) XXX_Size

func (m *SyncRequest) XXX_Size() int

func (*SyncRequest) XXX_Unmarshal

func (m *SyncRequest) XXX_Unmarshal(b []byte) error

type SyncResponse

type SyncResponse struct {
	Code                 int64    `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*SyncResponse) Descriptor

func (*SyncResponse) Descriptor() ([]byte, []int)

func (*SyncResponse) GetCode

func (m *SyncResponse) GetCode() int64

func (*SyncResponse) ProtoMessage

func (*SyncResponse) ProtoMessage()

func (*SyncResponse) Reset

func (m *SyncResponse) Reset()

func (*SyncResponse) String

func (m *SyncResponse) String() string

func (*SyncResponse) XXX_DiscardUnknown

func (m *SyncResponse) XXX_DiscardUnknown()

func (*SyncResponse) XXX_Marshal

func (m *SyncResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*SyncResponse) XXX_Merge

func (dst *SyncResponse) XXX_Merge(src proto.Message)

func (*SyncResponse) XXX_Size

func (m *SyncResponse) XXX_Size() int

func (*SyncResponse) XXX_Unmarshal

func (m *SyncResponse) XXX_Unmarshal(b []byte) error

type SyncVariable

type SyncVariable struct {
	// map key - nodeID
	NodesValues          map[string]*SyncNodeValue `` /* 163-byte string literal not displayed */
	TTL                  int64                     `protobuf:"varint,2,opt,name=TTL,proto3" json:"TTL,omitempty"`
	TTLStamp             int64                     `protobuf:"varint,3,opt,name=TTLStamp,proto3" json:"TTLStamp,omitempty"`
	XXX_NoUnkeyedLiteral struct{}                  `json:"-"`
	XXX_unrecognized     []byte                    `json:"-"`
	XXX_sizecache        int32                     `json:"-"`
}

func (*SyncVariable) Descriptor

func (*SyncVariable) Descriptor() ([]byte, []int)

func (*SyncVariable) GetNodesValues

func (m *SyncVariable) GetNodesValues() map[string]*SyncNodeValue

func (*SyncVariable) GetTTL

func (m *SyncVariable) GetTTL() int64

func (*SyncVariable) GetTTLStamp

func (m *SyncVariable) GetTTLStamp() int64

func (*SyncVariable) ProtoMessage

func (*SyncVariable) ProtoMessage()

func (*SyncVariable) Reset

func (m *SyncVariable) Reset()

func (*SyncVariable) String

func (m *SyncVariable) String() string

func (*SyncVariable) XXX_DiscardUnknown

func (m *SyncVariable) XXX_DiscardUnknown()

func (*SyncVariable) XXX_Marshal

func (m *SyncVariable) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*SyncVariable) XXX_Merge

func (dst *SyncVariable) XXX_Merge(src proto.Message)

func (*SyncVariable) XXX_Size

func (m *SyncVariable) XXX_Size() int

func (*SyncVariable) XXX_Unmarshal

func (m *SyncVariable) XXX_Unmarshal(b []byte) error

Jump to

Keyboard shortcuts

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