2018-01-11 04:55:33 +03:00
|
|
|
package servicebus
|
2018-01-22 22:30:27 +03:00
|
|
|
|
2018-05-11 20:27:30 +03:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE
|
|
|
|
|
2018-01-22 22:30:27 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2018-02-07 21:29:09 +03:00
|
|
|
"time"
|
2018-02-18 21:20:45 +03:00
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
"github.com/Azure/azure-amqp-common-go"
|
|
|
|
"github.com/Azure/azure-amqp-common-go/log"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
2018-09-29 02:39:47 +03:00
|
|
|
"pack.ag/amqp"
|
2018-01-22 22:30:27 +03:00
|
|
|
)
|
|
|
|
|
2018-01-30 04:37:40 +03:00
|
|
|
type (
|
2018-12-01 00:51:11 +03:00
|
|
|
// Receiver provides connection, session and link handling for a receiving to an entity path
|
|
|
|
Receiver struct {
|
2018-11-28 22:35:06 +03:00
|
|
|
namespace *Namespace
|
|
|
|
connection *amqp.Client
|
|
|
|
session *session
|
|
|
|
receiver *amqp.Receiver
|
|
|
|
entityPath string
|
|
|
|
done func()
|
|
|
|
Name string
|
|
|
|
useSessions bool
|
|
|
|
sessionID *string
|
|
|
|
lastError error
|
|
|
|
mode ReceiveMode
|
|
|
|
prefetch uint32
|
|
|
|
DefaultDisposition DispositionAction
|
2018-12-01 00:51:11 +03:00
|
|
|
Closed bool
|
2018-02-07 21:29:09 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// ReceiverOption provides a structure for configuring receivers
|
|
|
|
ReceiverOption func(receiver *Receiver) error
|
2018-02-07 21:29:09 +03:00
|
|
|
|
|
|
|
// ListenerHandle provides the ability to close or listen to the close of a Receiver
|
2018-12-01 00:51:11 +03:00
|
|
|
ListenerHandle struct {
|
|
|
|
r *Receiver
|
2018-02-07 21:29:09 +03:00
|
|
|
ctx context.Context
|
2018-01-30 04:37:40 +03:00
|
|
|
}
|
|
|
|
)
|
2018-01-22 22:30:27 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// ReceiverWithSession configures a Receiver to use a session
|
|
|
|
func ReceiverWithSession(sessionID *string) ReceiverOption {
|
|
|
|
return func(r *Receiver) error {
|
|
|
|
r.sessionID = sessionID
|
|
|
|
r.useSessions = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiverWithReceiveMode configures a Receiver to use the specified receive mode
|
|
|
|
func ReceiverWithReceiveMode(mode ReceiveMode) ReceiverOption {
|
|
|
|
return func(r *Receiver) error {
|
|
|
|
r.mode = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:05:19 +03:00
|
|
|
// ReceiverWithPrefetchCount configures the receiver to attempt to fetch the number of messages specified by the prefect
|
|
|
|
// at one time.
|
|
|
|
//
|
|
|
|
// The default is 1 message at a time.
|
|
|
|
//
|
|
|
|
// Caution: Using PeekLock, messages have a set lock timeout, which can be renewed. By setting a high prefetch count, a
|
|
|
|
// local queue of messages could build up and cause message locks to expire before the message lands in the handler. If
|
|
|
|
// this happens, the message disposition will fail and will be re-queued and processed again.
|
|
|
|
func ReceiverWithPrefetchCount(prefetch uint32) ReceiverOption {
|
|
|
|
return func(receiver *Receiver) error {
|
|
|
|
receiver.prefetch = prefetch
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// NewReceiver creates a new Service Bus message listener given an AMQP client and an entity path
|
|
|
|
func (ns *Namespace) NewReceiver(ctx context.Context, entityPath string, opts ...ReceiverOption) (*Receiver, error) {
|
|
|
|
span, ctx := ns.startSpanFromContext(ctx, "sb.Hub.NewReceiver")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
receiver := &Receiver{
|
2018-02-07 21:29:09 +03:00
|
|
|
namespace: ns,
|
2018-01-22 22:30:27 +03:00
|
|
|
entityPath: entityPath,
|
2018-06-12 19:02:46 +03:00
|
|
|
mode: PeekLockMode,
|
2018-06-11 23:19:27 +03:00
|
|
|
prefetch: 1,
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(receiver); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := receiver.newSessionAndLink(ctx)
|
2018-02-18 23:40:22 +03:00
|
|
|
return receiver, err
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// Close will close the AMQP session and link of the Receiver
|
|
|
|
func (r *Receiver) Close(ctx context.Context) error {
|
2018-02-18 23:40:22 +03:00
|
|
|
if r.done != nil {
|
|
|
|
r.done()
|
|
|
|
}
|
2018-01-22 22:30:27 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
r.Closed = true
|
2018-11-30 00:24:40 +03:00
|
|
|
err := r.receiver.Close(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = r.session.Close(ctx)
|
|
|
|
_ = r.connection.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.session.Close(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = r.connection.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
return r.connection.Close()
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recover will attempt to close the current session and link, then rebuild them
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) Recover(ctx context.Context) error {
|
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.Recover")
|
2018-08-08 21:32:30 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// we expect the Sender, session or client is in an error state, ignore errors
|
2018-08-08 21:32:30 +03:00
|
|
|
closeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
closeCtx = opentracing.ContextWithSpan(closeCtx, span)
|
|
|
|
defer cancel()
|
|
|
|
_ = r.receiver.Close(closeCtx)
|
|
|
|
_ = r.session.Close(closeCtx)
|
|
|
|
_ = r.connection.Close()
|
2018-02-07 21:29:09 +03:00
|
|
|
return r.newSessionAndLink(ctx)
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// ReceiveOne will receive one message from the link
|
|
|
|
func (r *Receiver) ReceiveOne(ctx context.Context, handler Handler) error {
|
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.ReceiveOne")
|
2018-06-15 20:20:30 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
amqpMsg, err := r.listenForMessage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.For(ctx).Error(err)
|
2018-09-26 04:39:44 +03:00
|
|
|
return err
|
2018-06-15 20:20:30 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 04:39:44 +03:00
|
|
|
r.handleMessage(ctx, amqpMsg, handler)
|
2018-06-15 20:20:30 +03:00
|
|
|
|
2018-09-26 04:39:44 +03:00
|
|
|
return nil
|
2018-06-15 20:20:30 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 02:47:09 +03:00
|
|
|
// Listen start a listener for messages sent to the entity path
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) Listen(ctx context.Context, handler Handler) *ListenerHandle {
|
2018-09-26 04:39:44 +03:00
|
|
|
ctx, done := context.WithCancel(ctx)
|
2018-02-18 23:40:22 +03:00
|
|
|
r.done = done
|
2018-02-07 21:29:09 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.Listen")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-01-23 02:47:09 +03:00
|
|
|
messages := make(chan *amqp.Message)
|
2018-02-18 23:40:22 +03:00
|
|
|
go r.listenForMessages(ctx, messages)
|
|
|
|
go r.handleMessages(ctx, messages, handler)
|
2018-02-07 21:29:09 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
return &ListenerHandle{
|
2018-02-07 21:29:09 +03:00
|
|
|
r: r,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
2018-01-23 02:47:09 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) handleMessages(ctx context.Context, messages chan *amqp.Message, handler Handler) {
|
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.handleMessages")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
2019-03-05 04:48:34 +03:00
|
|
|
for msg := range messages {
|
|
|
|
r.handleMessage(ctx, msg, handler)
|
2018-02-07 21:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 23:40:22 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) handleMessage(ctx context.Context, msg *amqp.Message, handler Handler) {
|
|
|
|
const optName = "sb.Receiver.handleMessage"
|
2018-12-19 22:02:36 +03:00
|
|
|
|
2018-06-15 20:20:30 +03:00
|
|
|
event, err := messageFromAMQPMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
_, ctx := r.startConsumerSpanFromContext(ctx, optName)
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
var span opentracing.Span
|
2018-05-16 21:39:14 +03:00
|
|
|
wireContext, err := extractWireContext(event)
|
2018-02-07 21:29:09 +03:00
|
|
|
if err == nil {
|
2018-06-15 20:20:30 +03:00
|
|
|
span, ctx = r.startConsumerSpanFromWire(ctx, optName, wireContext)
|
2018-02-07 21:29:09 +03:00
|
|
|
} else {
|
2018-06-15 20:20:30 +03:00
|
|
|
span, ctx = r.startConsumerSpanFromContext(ctx, optName)
|
2018-02-07 21:29:09 +03:00
|
|
|
}
|
|
|
|
defer span.Finish()
|
2018-02-18 23:40:22 +03:00
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
id := messageID(msg)
|
|
|
|
span.SetTag("amqp.message-id", id)
|
|
|
|
|
2018-11-28 22:35:06 +03:00
|
|
|
if err := handler.Handle(ctx, event); err != nil {
|
|
|
|
// stop handling messages since the message consumer ran into an unexpected error
|
|
|
|
r.lastError = err
|
|
|
|
r.done()
|
|
|
|
return
|
|
|
|
}
|
2018-06-15 20:20:30 +03:00
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// nothing more to be done. The message was settled when it was accepted by the Receiver
|
2018-06-12 19:02:46 +03:00
|
|
|
if r.mode == ReceiveAndDeleteMode {
|
2018-06-15 20:20:30 +03:00
|
|
|
return
|
2018-06-12 19:02:46 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// nothing more to be done. The Receiver has no default disposition, so the handler is solely responsible for
|
2018-11-28 22:35:06 +03:00
|
|
|
// disposition
|
|
|
|
if r.DefaultDisposition == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// default disposition is set, so try to send the disposition. If the message disposition has already been set, the
|
|
|
|
// underlying AMQP library will ignore the second disposition respecting the disposition of the handler func.
|
|
|
|
if err := r.DefaultDisposition(ctx); err != nil {
|
|
|
|
// if an error is returned by the default disposition, then we must alert the message consumer as we can't
|
|
|
|
// be sure the final message disposition.
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
r.lastError = err
|
|
|
|
r.done()
|
|
|
|
return
|
2018-01-23 02:47:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:39:14 +03:00
|
|
|
func extractWireContext(reader opentracing.TextMapReader) (opentracing.SpanContext, error) {
|
|
|
|
return opentracing.GlobalTracer().Extract(opentracing.TextMap, reader)
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) listenForMessages(ctx context.Context, msgChan chan *amqp.Message) {
|
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.listenForMessages")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
2018-02-18 23:40:22 +03:00
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
for {
|
|
|
|
msg, err := r.listenForMessage(ctx)
|
2018-08-08 21:32:30 +03:00
|
|
|
if err == nil {
|
|
|
|
msgChan <- msg
|
|
|
|
continue
|
2018-02-18 23:40:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 21:32:30 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.For(ctx).Debug("context done")
|
2019-03-05 04:48:34 +03:00
|
|
|
close(msgChan)
|
2018-08-08 21:32:30 +03:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
_, retryErr := common.Retry(10, 10*time.Second, func() (interface{}, error) {
|
2018-12-01 00:51:11 +03:00
|
|
|
sp, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.listenForMessages.tryRecover")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer sp.Finish()
|
|
|
|
|
2018-08-08 21:32:30 +03:00
|
|
|
log.For(ctx).Debug("recovering connection")
|
2018-02-07 21:29:09 +03:00
|
|
|
err := r.Recover(ctx)
|
2018-08-08 21:32:30 +03:00
|
|
|
if err == nil {
|
|
|
|
log.For(ctx).Debug("recovered connection")
|
|
|
|
return nil, nil
|
2018-02-07 21:29:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 21:32:30 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
2018-02-07 21:29:09 +03:00
|
|
|
return nil, common.Retryable(err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if retryErr != nil {
|
2018-08-08 21:32:30 +03:00
|
|
|
log.For(ctx).Debug("retried, but error was unrecoverable")
|
2018-02-07 21:29:09 +03:00
|
|
|
r.lastError = retryErr
|
2018-11-28 22:35:06 +03:00
|
|
|
if err := r.Close(ctx); err != nil {
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
}
|
2019-03-05 04:48:34 +03:00
|
|
|
close(msgChan)
|
2018-02-07 21:29:09 +03:00
|
|
|
return
|
|
|
|
}
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
2018-01-23 02:47:09 +03:00
|
|
|
}
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
func (r *Receiver) listenForMessage(ctx context.Context) (*amqp.Message, error) {
|
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.Receiver.listenForMessage")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
msg, err := r.receiver.Receive(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.For(ctx).Debug(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := messageID(msg)
|
|
|
|
span.SetTag("amqp.message-id", id)
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:51:11 +03:00
|
|
|
// newSessionAndLink will replace the session and link on the Receiver
|
|
|
|
func (r *Receiver) newSessionAndLink(ctx context.Context) error {
|
2018-02-07 21:29:09 +03:00
|
|
|
connection, err := r.namespace.newConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-02 04:20:37 +03:00
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
r.connection = connection
|
2018-02-02 04:20:37 +03:00
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
err = r.namespace.negotiateClaim(ctx, connection, r.entityPath)
|
2018-01-22 22:30:27 +03:00
|
|
|
if err != nil {
|
2018-02-07 21:29:09 +03:00
|
|
|
log.For(ctx).Error(err)
|
2018-01-22 22:30:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
amqpSession, err := connection.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.session, err = newSession(amqpSession)
|
|
|
|
if err != nil {
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-15 20:20:30 +03:00
|
|
|
receiveMode := amqp.ModeSecond
|
|
|
|
if r.mode == ReceiveAndDeleteMode {
|
|
|
|
receiveMode = amqp.ModeFirst
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
opts := []amqp.LinkOption{
|
2018-02-02 05:26:25 +03:00
|
|
|
amqp.LinkSourceAddress(r.entityPath),
|
2018-06-15 20:20:30 +03:00
|
|
|
amqp.LinkReceiverSettle(receiveMode),
|
2018-06-11 23:19:27 +03:00
|
|
|
amqp.LinkCredit(r.prefetch),
|
2018-02-07 21:29:09 +03:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:48:06 +03:00
|
|
|
if r.mode == ReceiveAndDeleteMode {
|
|
|
|
opts = append(opts, amqp.LinkSenderSettle(amqp.ModeSettled))
|
|
|
|
}
|
|
|
|
|
2018-10-18 23:52:36 +03:00
|
|
|
if r.useSessions {
|
2018-11-28 01:57:42 +03:00
|
|
|
const name = "com.microsoft:session-filter"
|
|
|
|
const code = uint64(0x00000137000000C)
|
|
|
|
if r.sessionID == nil {
|
|
|
|
opts = append(opts, amqp.LinkSourceFilter(name, code, nil))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, amqp.LinkSourceFilter(name, code, r.sessionID))
|
|
|
|
}
|
2018-05-09 23:00:45 +03:00
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
|
|
|
|
amqpReceiver, err := amqpSession.NewReceiver(opts...)
|
2018-01-22 22:30:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.receiver = amqpReceiver
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
|
|
|
|
func messageID(msg *amqp.Message) interface{} {
|
|
|
|
var id interface{} = "null"
|
|
|
|
if msg.Properties != nil {
|
|
|
|
id = msg.Properties.MessageID
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
2018-05-05 02:26:03 +03:00
|
|
|
|
|
|
|
// Close will close the listener
|
2018-12-01 00:51:11 +03:00
|
|
|
func (lc *ListenerHandle) Close(ctx context.Context) error {
|
2018-05-05 02:26:03 +03:00
|
|
|
return lc.r.Close(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done will close the channel when the listener has stopped
|
2018-12-01 00:51:11 +03:00
|
|
|
func (lc *ListenerHandle) Done() <-chan struct{} {
|
2018-05-05 02:26:03 +03:00
|
|
|
return lc.ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err will return the last error encountered
|
2018-12-01 00:51:11 +03:00
|
|
|
func (lc *ListenerHandle) Err() error {
|
2018-05-05 02:26:03 +03:00
|
|
|
if lc.r.lastError != nil {
|
|
|
|
return lc.r.lastError
|
|
|
|
}
|
|
|
|
return lc.ctx.Err()
|
|
|
|
}
|