binder

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2016 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package binder - Defines the type used to synchronize user sessions with documents using operational transforms to allow zero collision asynchronous editing of a text document.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTransformNegDelete = errors.New("transform contained negative delete")
	ErrTransformTooLong   = errors.New("transform insert length exceeded the limit")
	ErrTransformTooOld    = errors.New("transform diff greater than transform archive")
)

Errors for the internal Operational Transform model.

View Source
var (
	ErrReadOnlyPortal = errors.New("attempting to send transforms through a READ ONLY portal")
)

Errors for the binder portal type.

View Source
var (
	ErrTimeout = errors.New("timed out")
)

Errors used throughout the package.

Functions

This section is empty.

Types

type ClientInfo

type ClientInfo struct {
	UserID    string `json:"user_id"`
	SessionID string `json:"session_id"`
}

ClientInfo - Contains user session credentials (user id and session id).

type ClientUpdate

type ClientUpdate struct {
	ClientInfo ClientInfo `json:"client"`
	Message    Message    `json:"message"`
}

ClientUpdate - A struct broadcast to all clients containing an update/message.

type Config

type Config struct {
	FlushPeriod           int64       `json:"flush_period_ms" yaml:"flush_period_ms"`
	RetentionPeriod       int64       `json:"retention_period_s" yaml:"retention_period_s"`
	ClientKickPeriod      int64       `json:"kick_period_ms" yaml:"kick_period_ms"`
	CloseInactivityPeriod int64       `json:"close_inactivity_period_s" yaml:"close_inactivity_period_s"`
	ModelConfig           ModelConfig `json:"transform_model" yaml:"transform_model"`
}

Config - Holds configuration options for a binder.

func NewConfig

func NewConfig() Config

NewConfig - Returns a fully defined Binder configuration with the default values for each field.

type Error

type Error struct {
	ID  string
	Err error
}

Error - A binder has encountered a problem and needs to close. In order for this to happen it needs to inform its owner that it should be shut down. Error is a structure used to carry our error message and our ID over an error channel. A Error with the Err set to nil can be used as a graceful shutdown request.

type Message

type Message struct {
	Content  string `json:"content,omitempty"`
	Position *int64 `json:"position,omitempty"`
	Active   bool   `json:"active"`
}

Message - Content, optional cursor position and a flag representing whether the client is active.

type Model

type Model interface {
	/* PushTransform - Push a single transform to our model, and if successful, return the updated
	 * transform along with the new version of the document.
	 */
	PushTransform(ot OTransform) (OTransform, int, error)

	/* Returns true, if the model have unapplied transforms that can be flushed */
	IsDirty() bool

	/* FlushTransforms - apply all unapplied transforms to content, and delete old applied
	 * in accordance with our retention period. Returns a bool indicating whether any changes
	 * were applied, and an error in case a fatal problem was encountered.
	 */
	FlushTransforms(content *string, secondsRetention int64) (bool, error)

	/* GetVersion - returns the current version of the document.
	 */
	GetVersion() int
}

Model - an interface that represents an internal operation transform model of a particular type. Initially text is the only supported transform model, however, the plan will eventually be to have different models for various types of document that should all be supported by our binder.

func CreateTextModel

func CreateTextModel(config ModelConfig) Model

CreateTextModel - Returns a fresh transform model, with the version set to 1.

type ModelConfig

type ModelConfig struct {
	MaxDocumentSize    uint64 `json:"max_document_size" yaml:"max_document_size"`
	MaxTransformLength uint64 `json:"max_transform_length" yaml:"max_transform_length"`
}

ModelConfig - Holds configuration options for a transform model.

func NewModelConfig

func NewModelConfig() ModelConfig

NewModelConfig - Returns a default ModelConfig.

type OModel

type OModel struct {
	Version   int
	Applied   []OTransform
	Unapplied []OTransform
	// contains filtered or unexported fields
}

OModel - A representation of the transform model surrounding a document session. This keeps track of changes submitted and recently applied in order to distribute those changes to clients.

func (*OModel) FlushTransforms

func (m *OModel) FlushTransforms(content *string, secondsRetention int64) (bool, error)

FlushTransforms - apply all unapplied transforms and append them to the applied stack, then remove old entries from the applied stack. Accepts retention as an indicator for how many seconds applied transforms should be retained. Returns a bool indicating whether any changes were applied.

func (*OModel) GetVersion

func (m *OModel) GetVersion() int

GetVersion - returns the current version of the document.

func (*OModel) IsDirty

func (m *OModel) IsDirty() bool

IsDirty - Check if there is any unapplied transforms.

func (*OModel) PushTransform

func (m *OModel) PushTransform(ot OTransform) (OTransform, int, error)

PushTransform - Inserts a transform onto the unapplied stack and increments the version number of the document. Whilst doing so it fixes the transform in relation to earlier transforms it was unaware of, this fixed version gets sent back for distributing across other clients.

type OTransform

type OTransform struct {
	Position  int    `json:"position" yaml:"position"`
	Delete    int    `json:"num_delete" yaml:"num_delete"`
	Insert    string `json:"insert" yaml:"insert"`
	Version   int    `json:"version" yaml:"version"`
	TReceived int64  `json:"received,omitempty" yaml:"received,omitempty"`
}

OTransform - A representation of a transformation relating to a leap document. This can either be a text addition, a text deletion, or both.

type Portal

type Portal interface {
	// UserID - Returns the user identifier associated with this binder session.
	UserID() string

	// SessionID - Returns the unique session identifier associated with this binder session.
	SessionID() string

	// BaseVersion - Returns the version of the binder as it was when this session opened.
	BaseVersion() int

	// Document - Returns the document contents as it was when this session opened.
	Document() store.Document

	// ReleaseDocument - Releases the cached document.
	ReleaseDocument()

	// TransformReadChan - Get the channel for reading transforms from other binder clients.
	TransformReadChan() <-chan OTransform

	// UpdateReadChan - Get the channel for reading meta updates from other binder clients.
	UpdateReadChan() <-chan ClientUpdate

	// SendTransform - Submits an operational transform to the document, this call adds the
	// transform to the stack of pending changes and broadcasts it to all other connected clients.
	// The transform must be submitted with the target version (the version that the client believed
	// it was, at the time it was made), and the actual version is returned.
	SendTransform(ot OTransform, timeout time.Duration) (int, error)

	// SendMessage - Broadcasts a message out to all other connected clients.
	SendMessage(message Message)

	// Exit - Inform the binder that this client is shutting down, this call will block until
	// acknowledged by the binder. Therefore, you may specify a timeout.
	Exit(timeout time.Duration)
}

Portal - An interface used by clients to contact a connected binder type.

type Type

type Type interface {
	// ID - Returns the ID of this binder.
	ID() string

	// GetUsers - Returns a list of connected users.
	GetUsers(timeout time.Duration) ([]string, error)

	// KickUser - Kick a connected user from this binder.
	KickUser(userID string, timeout time.Duration) error

	// Subscribe - Register a new client as an editor of this binder document.
	Subscribe(userID string, timeout time.Duration) (Portal, error)

	// SubscribeReadOnly - Register a new client as a read only viewer of this binder document.
	SubscribeReadOnly(userID string, timeout time.Duration) (Portal, error)

	// Close - Close the binder and shut down all clients, also flushes and cleans up the document.
	Close()
}

Type - Provides thread safe implementations of binder and session creation.

func New

func New(
	id string,
	block store.Type,
	config Config,
	errorChan chan<- Error,
	log log.Modular,
	stats metrics.Aggregator,
) (Type, error)

New - Creates a binder targeting an existing document determined via an ID. Must provide a store.Type to acquire the document and apply future updates to.

Jump to

Keyboard shortcuts

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