hub

package
v0.0.0-...-2ec0910 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2021 License: MIT Imports: 13 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConditionFactories = map[string]func() Condition{
	new(KeyEqualCondition).GetName(): func() Condition {
		return new(KeyEqualCondition)
	},
	new(MetadataKeyEqualCondition).GetName(): func() Condition {
		return new(MetadataKeyEqualCondition)
	},
}

ConditionFactories is where you can add custom conditions

Functions

This section is empty.

Types

type Client

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

Client will subscribe to the upstream hub over the provided grpc conn.

func NewClient

func NewClient(namespace []string, conditions []byte, conn *grpc.ClientConn) *Client

NewClient returns a new initialized client which connect to hub server, for Pub Sub Mechanism.

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, kv *hub.KV) error

Publish the provided kv to the hub server.

func (*Client) PublishList

func (c *Client) PublishList(ctx context.Context, msgSender <-chan *hub.KV) error

PublishList publishes received message over the sender channel to the hub server until the ctx is cancelled/done or msgSender is closed.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, msgRcv chan<- *hub.KV) error

Subscribe the given Client to upstream hub for receiving published messages. The channel on which message would be received should have enough buffer to hold for fast sender hub. It's a blocking API.

func (*Client) UnSubscribe

func (c *Client) UnSubscribe()

UnSubscribe the current client from the upstream hub.

type Condition

type Condition interface {
	// GetName returns the condition's name.
	GetName() string

	// Fulfills returns true if the request is fulfilled by the condition.
	Fulfills(request *Request) bool
}

Condition either do or do not fulfill a subscription request.

type Conditions

type Conditions map[string]Condition

Conditions is a collection of conditions.

func (Conditions) Fulfills

func (cs Conditions) Fulfills(request *Request) bool

Fulfills returns true if all condition are fulfilled.

func (Conditions) MarshalJSON

func (cs Conditions) MarshalJSON() ([]byte, error)

MarshalJSON marshals a list of conditions to json.

func (Conditions) UnmarshalJSON

func (cs Conditions) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshal a list of conditions from json.

type DurationKind

type DurationKind int

DurationKind represents what kind of duration has been sent.

const (
	// Broadcast duration
	Broadcast DurationKind = iota
	// Stream duration is overall stream duration
	Stream
)

type HookMetadata

type HookMetadata struct {
	DurationKind
	// if this was error
	Err error
}

HookMetadata contains some embedded information for the hook.

type Hooker

type Hooker interface {
	PubHook(msg *hub.KV)
	SubHook(ops SubscriberOPS)
	Duration(metadata HookMetadata, d time.Duration)
}

Hooker implementation provides hook methods that gets called during the pub and sub operations.

type Hub

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

Hub provides gRPC implementation of Publish/Subscribe messaging paradigm.

func NewHub

func NewHub(grpcServer *grpc.Server, opts ...Options) (*Hub, error)

NewHub returns an initialized hub server, and register the gRPC implementation of Publish/Subscribe messaging paradigm over the provided grpcServer instance.

func (*Hub) Close

func (h *Hub) Close() error

Close closes the Hub

func (*Hub) Publish

func (h *Hub) Publish(msg *hub.KV) error

Publish publishes the given msg on this server for all subscriber. It's an async method.

type KVMetadata

type KVMetadata map[string]string

KVMetadata used in publisher context.

type KeyEqualCondition

type KeyEqualCondition struct {
	Equals string `json:"equals"`
}

KeyEqualCondition is a subscription condition, that will only match when the provided key is equal to Equals.

func (*KeyEqualCondition) Fulfills

func (c *KeyEqualCondition) Fulfills(request *Request) bool

Fulfills returns true if the given key is same as in KeyEqualCondition.Equals

func (*KeyEqualCondition) GetName

func (c *KeyEqualCondition) GetName() string

GetName returns the condition's name.

type MetadataKeyEqualCondition

type MetadataKeyEqualCondition struct {
	Equals  string `json:"equals"`
	KeyName string `json:"key_name"`
}

MetadataKeyEqualCondition is a subscription condition, which only match when the provided metadata key equals to the Equals

func (*MetadataKeyEqualCondition) Fulfills

func (c *MetadataKeyEqualCondition) Fulfills(request *Request) bool

Fulfills returns true if the given MetadataKeyEqualCondition.KeyName value is same as in MetadataKeyEqualCondition.Equals

func (*MetadataKeyEqualCondition) GetName

func (c *MetadataKeyEqualCondition) GetName() string

GetName returns the condition's name.

type NoopHook

type NoopHook struct {
}

NoopHook implements Hooker interface and does nothing.

func (NoopHook) Duration

func (n NoopHook) Duration(metadata HookMetadata, d time.Duration)

func (NoopHook) PubHook

func (n NoopHook) PubHook(msg *hub.KV)

func (NoopHook) SubHook

func (n NoopHook) SubHook(ops SubscriberOPS)

type Options

type Options func(*Hub)

Options Configure the Hub with different parameters.

func WithHooker

func WithHooker(hooker Hooker) Options

WithHooker configures the hub to use the passed Hooker implementation.

func WithPublisherTimeout

func WithPublisherTimeout(d time.Duration) Options

WithPublisherTimeout configures the hub with timeout duration that hub respect before timing out the publishing at server.

func WithSubscriberBufferCount

func WithSubscriberBufferCount(count int) Options

WithSubscriberBufferCount configures the hub with the count which is then used to create a buffer channel for each subscriber that connect with hub server.

type Request

type Request struct {
	Key string `json:"key"`

	// Metadata is the request's propagated Metadata.
	Metadata KVMetadata `json:"metadata"`
}

Request is the Condition request object.

type SubscriberOPS

type SubscriberOPS int

SubscriberOPS represents what kind of Subscriber Action was performed.

const (
	SubscriberCreate SubscriberOPS = 0
	SubscriberDelete SubscriberOPS = 1
)

Jump to

Keyboard shortcuts

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