server

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Version is the current version for the server.
	Version = "1.7.5"

	// ProtocolVersion is the implemented RES protocol version.
	ProtocolVersion = "1.2.2"

	// DefaultAddr is the default host for client connections.
	DefaultAddr = "0.0.0.0"

	// DefaultPort is the default port for client connections.
	DefaultPort = 8080

	// DefaultWSPath is the default path for WebSocket connections.
	DefaultWSPath = "/"

	// DefaultAPIPath is the default path to web resource.
	DefaultAPIPath = "/api"

	// DefaultAPIEncoding is the default encoding for web resources.
	DefaultAPIEncoding = "json"

	// WSTimeout is the wait time for WebSocket connections to close on shutdown.
	WSTimeout = 3 * time.Second

	// MQTimeout is the wait time for the messaging client to close on shutdown.
	MQTimeout = 3 * time.Second

	// WSConnWorkerQueueSize is the size of the queue for each connection worker.
	WSConnWorkerQueueSize = 256

	// CIDPlaceholder is the placeholder tag for the connection ID.
	CIDPlaceholder = "{cid}"

	// SubscriptionCountLimit is the subscription limit of a single connection.
	SubscriptionCountLimit = 256

	// CacheWorkers is the number of goroutines handling cached resources.
	CacheWorkers = 10

	// UnsubscribeDelay is the delay for the cache to unsubscribe and evict resources no longer used.
	UnsubscribeDelay = 5 * time.Second
)

Variables

This section is empty.

Functions

func PathToRID added in v1.2.1

func PathToRID(path, query, prefix string) string

PathToRID parses a raw URL path and returns the resource ID. The prefix is the beginning of the path which is not part of the resource ID, and it should both start and end with /. Eg. "/api/"

func PathToRIDAction added in v1.2.1

func PathToRIDAction(path, query, prefix string) (string, string)

PathToRIDAction parses a raw URL path and returns the resource ID and action. The prefix is the beginning of the path which is not part of the resource ID, and it should both start and end with /. Eg. "/api/"

func RIDToPath added in v1.2.1

func RIDToPath(rid, prefix string) string

RIDToPath converts a resource ID to a URL path string. The prefix is the part of the path that should be prepended to the resource ID path, and it should both start and end with /. Eg. "/api/".

func RegisterAPIEncoderFactory added in v1.2.1

func RegisterAPIEncoderFactory(name string, f APIEncoderFactory)

RegisterAPIEncoderFactory adds an APIEncoderFactory by name. Panics if another factory with the same name is already registered.

Types

type APIEncoder added in v1.2.1

type APIEncoder interface {
	EncodeGET(*Subscription) ([]byte, error)
	EncodePOST(json.RawMessage) ([]byte, error)
	EncodeError(*reserr.Error) []byte
	NotFoundError() []byte
	ContentType() string
}

APIEncoder encodes responses to HTTP API requests.

type APIEncoderFactory added in v1.2.1

type APIEncoderFactory func(cfg Config) APIEncoder

APIEncoderFactory create an APIEncoder.

type Config

type Config struct {
	Addr         *string `json:"addr"`
	Port         uint16  `json:"port"`
	WSPath       string  `json:"wsPath"`
	APIPath      string  `json:"apiPath"`
	APIEncoding  string  `json:"apiEncoding"`
	HeaderAuth   *string `json:"headerAuth"`
	AllowOrigin  *string `json:"allowOrigin"`
	PUTMethod    *string `json:"putMethod"`
	DELETEMethod *string `json:"deleteMethod"`
	PATCHMethod  *string `json:"patchMethod"`

	TLS     bool   `json:"tls"`
	TLSCert string `json:"certFile"`
	TLSKey  string `json:"keyFile"`

	WSCompression bool `json:"wsCompression"`

	ResetThrottle     int `json:"resetThrottle"`
	ReferenceThrottle int `json:"referenceThrottle"`

	NoHTTP bool `json:"-"` // Disable start of the HTTP server. Used for testing
	// contains filtered or unexported fields
}

Config holds server configuration

func (*Config) SetDefault

func (c *Config) SetDefault()

SetDefault sets the default values

type ConnSubscriber

type ConnSubscriber interface {
	Logf(format string, v ...interface{})
	Debugf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	CID() string
	Token() json.RawMessage
	Subscribe(rid string, direct bool, throttle *rescache.Throttle) (*Subscription, error)
	Unsubscribe(sub *Subscription, direct bool, count int, tryDelete bool)
	Access(sub *Subscription, callback func(*rescache.Access))
	Send(data []byte)
	Enqueue(f func()) bool
	ExpandCID(string) string
	Disconnect(reason string)
	ProtocolVersion() int
}

ConnSubscriber represents a client connection making the subscription

type Service

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

Service is a RES gateway implementation

func NewService

func NewService(mq mq.Client, cfg Config) (*Service, error)

NewService creates a new Service

func (*Service) Debugf

func (s *Service) Debugf(format string, v ...interface{})

Debugf writes a formatted debug message

func (*Service) Errorf added in v1.3.0

func (s *Service) Errorf(format string, v ...interface{})

Errorf writes a formatted error message

func (*Service) GetWSHandlerFunc

func (s *Service) GetWSHandlerFunc() http.Handler

GetWSHandlerFunc returns the websocket http.Handler Used for testing purposes

func (*Service) Logf

func (s *Service) Logf(format string, v ...interface{})

Logf writes a formatted log message

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Service) SetLogger

func (s *Service) SetLogger(l logger.Logger) *Service

SetLogger sets the logger

func (*Service) Start

func (s *Service) Start() (err error)

Start connects the Service to the nats server

func (*Service) Stop

func (s *Service) Stop(err error)

Stop closes the connection to the nats server

func (*Service) StopChannel

func (s *Service) StopChannel() <-chan error

StopChannel returns a channel that will pass a value when the service has stopped.

func (*Service) Tracef added in v1.3.0

func (s *Service) Tracef(format string, v ...interface{})

Tracef writes a formatted trace message

type Subscription

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

Subscription represents a resource subscription made by a client connection

func NewSubscription

func NewSubscription(c ConnSubscriber, rid string, throttle *rescache.Throttle) *Subscription

NewSubscription creates a new Subscription

func (*Subscription) CID

func (s *Subscription) CID() string

CID returns the unique connection ID of the client connection

func (*Subscription) CanCall

func (s *Subscription) CanCall(action string, cb func(err error))

CanCall checks asynchronously if the client connection has access to call the actionn. If access is denied, the callback will be called with an error describing the reason. If access is granted, the callback will be called with err being nil.

func (*Subscription) CanGet

func (s *Subscription) CanGet(cb func(err error))

CanGet checks asynchronously if the client connection has access to get (read) the resource. If access is denied, the callback will be called with an error describing the reason. If access is granted, the callback will be called with err being nil.

func (*Subscription) CollectionValues added in v1.2.1

func (s *Subscription) CollectionValues() []codec.Value

CollectionValues returns the subscriptions collection values. Panics if the subscription is not a loaded collection.

func (*Subscription) Dispose

func (s *Subscription) Dispose()

Dispose removes any resourceSubscription and sets the subscription state to stateDisposed

func (*Subscription) Error

func (s *Subscription) Error() error

Error returns any error that occurred when loading the subscribed resource.

func (*Subscription) Event

func (s *Subscription) Event(event *rescache.ResourceEvent)

Event passes an event to the subscription to be processed.

func (*Subscription) GetRPCResources

func (s *Subscription) GetRPCResources() *rpc.Resources

GetRPCResources returns a rpc.Resources object. It will lock the subscription and queue any events until ReleaseRPCResources is called.

func (*Subscription) IsReady added in v1.2.1

func (s *Subscription) IsReady() bool

IsReady returns true if the subscription and all of its dependencies are loaded.

func (*Subscription) IsSent

func (s *Subscription) IsSent() bool

IsSent returns true if the subscribed resource has been sent to the client.

func (*Subscription) Loaded

func (s *Subscription) Loaded(resourceSub *rescache.ResourceSubscription, err error)

Loaded is called by rescache when the subscribed resource has been loaded. If the resource was successfully loaded, err will be nil. If an error occurred when loading the resource, resourceSub will be nil, and err will be the error.

func (*Subscription) ModelValues added in v1.2.1

func (s *Subscription) ModelValues() map[string]codec.Value

ModelValues returns the subscriptions model values. Panics if the subscription is not a loaded model.

func (*Subscription) OnReady added in v1.2.1

func (s *Subscription) OnReady(cb func())

OnReady gets a callback that should be called once the subscribed resource and all its referenced resources recursively, has been loaded from the rescache. If the resource is already ready, the callback will directly be called.

func (*Subscription) RID

func (s *Subscription) RID() string

RID returns the subscription's resource ID

func (*Subscription) Reaccess

func (s *Subscription) Reaccess(t *rescache.Throttle)

Reaccess adds a reaccess event to the eventQueue, triggering a new access request to be sent to the service.

func (*Subscription) Ref added in v1.2.1

func (s *Subscription) Ref(rid string) *Subscription

Ref returns the referenced subscription, or nil if subscription has no such reference.

func (*Subscription) ReleaseRPCResources

func (s *Subscription) ReleaseRPCResources()

ReleaseRPCResources will unlock all resources locked by GetRPCResource, unqueue any events, and mark the subscription as sent.

func (*Subscription) ResourceName

func (s *Subscription) ResourceName() string

ResourceName returns the resource name part of the subscription's resource ID

func (*Subscription) ResourceQuery

func (s *Subscription) ResourceQuery() string

ResourceQuery returns the query part of the subscription's resource ID

func (*Subscription) ResourceType

func (s *Subscription) ResourceType() rescache.ResourceType

ResourceType returns the resource type of the subscribed resource

func (*Subscription) Token

func (s *Subscription) Token() json.RawMessage

Token returns the access token held by the subscription's client connection

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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