natschannel

package module
v0.0.0-...-fa040c7 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

Jrpc2 NATS Channel

Build Coverage Status License

Status: EXPERIMENTAL

Package natschannel provides an implementation of the jrpc2 module's Channel interface using NATS as the transport.

Documentation

GoDoc

Full go doc style documentation for the project can be viewed online without installing this package by using the excellent GoDoc site here: http://godoc.org/github.com/41north/natschannel.go

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/41north/natschannel.go

Installation

$ go get -u github.com/41north/natschannel.go

Add this import line to the file you're working in:

import "github.com/41north/natschannel.go"

Quick Start

// connects to the nats server and binds the channel to the 'foo.bar' subject
channel, err := natschannel.Dial("nats://localhost:4222", "foo.bar")

// wraps an existing nats connection and binds the channel to the 'foo.bar' subject
channel, err := natschannel.New(conn, "foo.bar")

License

natschannel.go is licensed under the Apache 2.0 License

Contact

If you want to get in touch drop us an email at hello@41north.dev

Documentation

Index

Examples

Constants

View Source
const (
	DefaultInboxSize = 256
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

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

Channel implements the jrpc2 Channel interface over a NATS connection.

func Dial

func Dial(url string, subject string, options ...Option) (*Channel, error)

Dial dials the specified nats url ("nats://...") and binds it to the provided subject with the given options.

Example
channel, err := natschannel.Dial("nats://localhost:4222", "foo.bar")
if err != nil {
	panic(err)
}

err = channel.Close()
Output:

func New

func New(conn *nats.Conn, subject string, options ...Option) (*Channel, error)

New wraps the given nats.Conn to implement the Channel interface.

Example
conn, err := nats.Connect("nats://localhost:4222")
if err != nil {
	panic(err)
}
channel, err := natschannel.New(conn, "foo.bar")
if err != nil {
	panic(err)
}

err = channel.Close()
Output:

func (*Channel) Close

func (c *Channel) Close() error

Close implements the corresponding method of the Channel interface. Any active subscriptions are drained, the inbox channel closed and then the connection closed.

func (*Channel) Group

func (c *Channel) Group() string

func (*Channel) InboxSize

func (c *Channel) InboxSize() int

func (*Channel) Recv

func (c *Channel) Recv() ([]byte, error)

Recv implements the corresponding method of the Channel interface. The last message to have been received is read and it's payload returned.

func (*Channel) Send

func (c *Channel) Send(data []byte) error

Send implements the corresponding method of the jrpc2 Channel interface. Data is sent in a single NATS message.

type Option

type Option = func(opts *Options) error

Option is a builder function for modifying Options.

func Group

func Group(group string) Option

Group specifies a NATS work queue group that the Channel will be bound to.

func InboxSize

func InboxSize(size int) Option

InboxSize specifies the size of the msg inbox channel used for receiving responses.

func NatsOptions

func NatsOptions(options ...nats.Option) Option

NatsOptions allows for passing nats.Option to the nats.Conn that is being dialed.

type Options

type Options struct {
	Group       string
	NatsOptions []nats.Option
	InboxSize   int
}

Options represents a collection of options used for controlling how nats.Conn are created and how the resultant Channel is bound to the nats.Conn.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default options for the Channel.

Jump to

Keyboard shortcuts

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