rpc

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderContextKey  serviceKey = "headers"
	SubjectContextKey serviceKey = "subject"
)

Variables

This section is empty.

Functions

func GetHeaders

func GetHeaders(ctx context.Context) (nats.Header, bool)

func GetSubject

func GetSubject(ctx context.Context) (service.Subject, bool)

Types

type ClientConn

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

ClientConn implements grpc.ClientConnInterface for NATS

func NewClientConn

func NewClientConn(ctx context.Context, sub Publisher, remotePrefix string, vars map[string]string) *ClientConn

NewClientConn returns a client connector that functions as a layer between Protobuf Auto-generated gRPC client code and Data Layer allowing making rpc calls to a remote publisher.

It automatically derives the correct subjects based on the Protobuf schema, however, you may need to specify the remote prefix that the publisher is configured with. Also, you may need to pass vars. The subjects derived if contain any tokens such as `{id}`, will be substituted by the string stored under key "id". Otherwise such subject token will be replaced with `*`.

Naturally such subjects are invalid for a rpc call, so you must always supply variables for a client. Consuming pure streams will work just fine.

func (*ClientConn) Invoke

func (c *ClientConn) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error

func (*ClientConn) NewStream

func (c *ClientConn) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)

type MockPublisher

type MockPublisher struct {
	mock.Mock
}

MockPublisher is an autogenerated mock type for the Publisher type

func NewMockPublisher

func NewMockPublisher(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockPublisher

NewMockPublisher creates a new instance of MockPublisher. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockPublisher) EXPECT

func (_m *MockPublisher) EXPECT() *MockPublisher_Expecter

func (*MockPublisher) PublishTo

func (_m *MockPublisher) PublishTo(msg protoreflect.ProtoMessage, tokens ...string) error

PublishTo provides a mock function with given fields: msg, tokens

func (*MockPublisher) RequestFrom

RequestFrom provides a mock function with given fields: ctx, msg, resp, tokens

func (*MockPublisher) Serve

func (_m *MockPublisher) Serve(handler service.ServiceHandler, suffixes ...string) (*nats.Subscription, error)

Serve provides a mock function with given fields: handler, suffixes

func (*MockPublisher) Subject

func (_m *MockPublisher) Subject(suffixes ...string) string

Subject provides a mock function with given fields: suffixes

func (*MockPublisher) SubscribeTo

func (_m *MockPublisher) SubscribeTo(handler service.MessageHandler, tokens ...string) (*nats.Subscription, error)

SubscribeTo provides a mock function with given fields: handler, tokens

func (*MockPublisher) Unmarshal

func (_m *MockPublisher) Unmarshal(nmsg service.Message, msg protoreflect.ProtoMessage) (nats.Header, error)

Unmarshal provides a mock function with given fields: nmsg, msg

type MockPublisher_Expecter

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

func (*MockPublisher_Expecter) PublishTo

func (_e *MockPublisher_Expecter) PublishTo(msg interface{}, tokens ...interface{}) *MockPublisher_PublishTo_Call

PublishTo is a helper method to define mock.On call

  • msg protoreflect.ProtoMessage
  • tokens ...string

func (*MockPublisher_Expecter) RequestFrom

func (_e *MockPublisher_Expecter) RequestFrom(ctx interface{}, msg interface{}, resp interface{}, tokens ...interface{}) *MockPublisher_RequestFrom_Call

RequestFrom is a helper method to define mock.On call

  • ctx context.Context
  • msg protoreflect.ProtoMessage
  • resp protoreflect.ProtoMessage
  • tokens ...string

func (*MockPublisher_Expecter) Serve

func (_e *MockPublisher_Expecter) Serve(handler interface{}, suffixes ...interface{}) *MockPublisher_Serve_Call

Serve is a helper method to define mock.On call

  • handler service.ServiceHandler
  • suffixes ...string

func (*MockPublisher_Expecter) Subject

func (_e *MockPublisher_Expecter) Subject(suffixes ...interface{}) *MockPublisher_Subject_Call

Subject is a helper method to define mock.On call

  • suffixes ...string

func (*MockPublisher_Expecter) SubscribeTo

func (_e *MockPublisher_Expecter) SubscribeTo(handler interface{}, tokens ...interface{}) *MockPublisher_SubscribeTo_Call

SubscribeTo is a helper method to define mock.On call

  • handler service.MessageHandler
  • tokens ...string

func (*MockPublisher_Expecter) Unmarshal

func (_e *MockPublisher_Expecter) Unmarshal(nmsg interface{}, msg interface{}) *MockPublisher_Unmarshal_Call

Unmarshal is a helper method to define mock.On call

  • nmsg service.Message
  • msg protoreflect.ProtoMessage

type MockPublisher_PublishTo_Call

type MockPublisher_PublishTo_Call struct {
	*mock.Call
}

MockPublisher_PublishTo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'PublishTo'

func (*MockPublisher_PublishTo_Call) Return

func (*MockPublisher_PublishTo_Call) Run

func (*MockPublisher_PublishTo_Call) RunAndReturn

type MockPublisher_RequestFrom_Call

type MockPublisher_RequestFrom_Call struct {
	*mock.Call
}

MockPublisher_RequestFrom_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RequestFrom'

func (*MockPublisher_RequestFrom_Call) Return

func (*MockPublisher_RequestFrom_Call) Run

type MockPublisher_Serve_Call

type MockPublisher_Serve_Call struct {
	*mock.Call
}

MockPublisher_Serve_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Serve'

func (*MockPublisher_Serve_Call) Return

func (*MockPublisher_Serve_Call) Run

func (_c *MockPublisher_Serve_Call) Run(run func(handler service.ServiceHandler, suffixes ...string)) *MockPublisher_Serve_Call

func (*MockPublisher_Serve_Call) RunAndReturn

type MockPublisher_Subject_Call

type MockPublisher_Subject_Call struct {
	*mock.Call
}

MockPublisher_Subject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Subject'

func (*MockPublisher_Subject_Call) Return

func (*MockPublisher_Subject_Call) Run

func (_c *MockPublisher_Subject_Call) Run(run func(suffixes ...string)) *MockPublisher_Subject_Call

func (*MockPublisher_Subject_Call) RunAndReturn

func (_c *MockPublisher_Subject_Call) RunAndReturn(run func(...string) string) *MockPublisher_Subject_Call

type MockPublisher_SubscribeTo_Call

type MockPublisher_SubscribeTo_Call struct {
	*mock.Call
}

MockPublisher_SubscribeTo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SubscribeTo'

func (*MockPublisher_SubscribeTo_Call) Return

func (*MockPublisher_SubscribeTo_Call) Run

func (*MockPublisher_SubscribeTo_Call) RunAndReturn

type MockPublisher_Unmarshal_Call

type MockPublisher_Unmarshal_Call struct {
	*mock.Call
}

MockPublisher_Unmarshal_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Unmarshal'

func (*MockPublisher_Unmarshal_Call) Return

func (*MockPublisher_Unmarshal_Call) Run

func (*MockPublisher_Unmarshal_Call) RunAndReturn

type Publisher

type Publisher interface {
	Serve(handler service.ServiceHandler, suffixes ...string) (*nats.Subscription, error)
	RequestFrom(ctx context.Context, msg proto.Message, resp proto.Message, tokens ...string) (service.Message, error)
	SubscribeTo(handler service.MessageHandler, tokens ...string) (*nats.Subscription, error)
	PublishTo(msg proto.Message, tokens ...string) error
	PublishToRpc(msg proto.Message, replyTo string, tokens ...string) error
	RpcInbox(suffixes ...string) string
	Unmarshal(nmsg service.Message, msg protoreflect.ProtoMessage) (nats.Header, error)
	Subject(suffixes ...string) string
}

Publisher interface from your NATS wrapper

type ServiceRegistrar

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

ServiceRegistrar implements grpc.ServiceRegistrar for NATS

func NewServiceRegistrar

func NewServiceRegistrar(group *errgroup.Group, pub Publisher) *ServiceRegistrar

func (*ServiceRegistrar) RegisterService

func (s *ServiceRegistrar) RegisterService(desc *grpc.ServiceDesc, impl interface{})

RegisterService is called by the autogenerated Protobuf service code.

func (*ServiceRegistrar) Start

func (s *ServiceRegistrar) Start(ctx context.Context, vars map[string]string) error

Start begins processing service events, subscribes to rpc subjects, and calls pure-stream methods on a service.

It automatically derives the correct subjects based on the Protobuf schema, however, you may need to pass vars. The subjects derived if contain any tokens such as `{id}` will be substituted by the string stored under key "id" under a specific service key. Otherwise such subject token will be replaced with `*`.

You can also specify the full name of the service and the variable using fully qualified name of the service if more than one service shares the same variable.

Jump to

Keyboard shortcuts

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