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
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-02-18 21:20:45 +03:00
|
|
|
|
2018-01-22 22:30:27 +03:00
|
|
|
"pack.ag/amqp"
|
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-01-22 22:30:27 +03:00
|
|
|
)
|
|
|
|
|
2018-01-30 04:37:40 +03:00
|
|
|
// receiver provides session and link handling for a receiving entity path
|
|
|
|
type (
|
|
|
|
receiver struct {
|
2018-02-07 21:29:09 +03:00
|
|
|
namespace *Namespace
|
|
|
|
connection *amqp.Client
|
|
|
|
session *session
|
|
|
|
receiver *amqp.Receiver
|
|
|
|
entityPath string
|
|
|
|
done func()
|
|
|
|
Name string
|
|
|
|
requiredSessionID *string
|
|
|
|
lastError error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiverOptions provides a structure for configuring receivers
|
|
|
|
ReceiverOptions func(receiver *receiver) error
|
|
|
|
|
|
|
|
// ListenerHandle provides the ability to close or listen to the close of a Receiver
|
|
|
|
ListenerHandle struct {
|
|
|
|
r *receiver
|
|
|
|
ctx context.Context
|
2018-01-30 04:37:40 +03:00
|
|
|
}
|
|
|
|
)
|
2018-01-22 22:30:27 +03:00
|
|
|
|
2018-01-30 04:37:40 +03:00
|
|
|
// newReceiver creates a new Service Bus message listener given an AMQP client and an entity path
|
2018-02-07 21:29:09 +03:00
|
|
|
func (ns *Namespace) newReceiver(ctx context.Context, entityPath string, opts ...ReceiverOptions) (*receiver, error) {
|
2018-05-11 20:27:30 +03:00
|
|
|
span, ctx := ns.startSpanFromContext(ctx, "sb.Hub.newReceiver")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-01-30 04:37:40 +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-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
|
|
|
}
|
|
|
|
|
|
|
|
// Close will close the AMQP session and link of the receiver
|
2018-02-07 21:29:09 +03:00
|
|
|
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-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-02-07 21:29:09 +03:00
|
|
|
func (r *receiver) Recover(ctx context.Context) error {
|
|
|
|
_ = r.Close(ctx) // we expect the receiver is in an error state
|
|
|
|
return r.newSessionAndLink(ctx)
|
2018-01-22 22:30:27 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 02:47:09 +03:00
|
|
|
// Listen start a listener for messages sent to the entity path
|
2018-02-07 21:29:09 +03:00
|
|
|
func (r *receiver) Listen(handler Handler) *ListenerHandle {
|
2018-02-18 23:40:22 +03:00
|
|
|
ctx, done := context.WithCancel(context.Background())
|
|
|
|
r.done = done
|
2018-02-07 21:29:09 +03:00
|
|
|
|
2018-05-11 20:27:30 +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
|
|
|
|
|
|
|
return &ListenerHandle{
|
|
|
|
r: r,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
2018-01-23 02:47:09 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 23:40:22 +03:00
|
|
|
func (r *receiver) handleMessages(ctx context.Context, messages chan *amqp.Message, handler Handler) {
|
2018-05-11 20:27:30 +03:00
|
|
|
span, ctx := r.startConsumerSpanFromContext(ctx, "sb.receiver.handleMessages")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer span.Finish()
|
2018-01-23 02:47:09 +03:00
|
|
|
for {
|
|
|
|
select {
|
2018-02-18 23:40:22 +03:00
|
|
|
case <-ctx.Done():
|
2018-01-23 02:47:09 +03:00
|
|
|
return
|
|
|
|
case msg := <-messages:
|
2018-02-07 21:29:09 +03:00
|
|
|
r.handleMessage(ctx, msg, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-18 23:40:22 +03:00
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
func (r *receiver) handleMessage(ctx context.Context, msg *amqp.Message, handler Handler) {
|
2018-06-01 20:12:10 +03:00
|
|
|
event := messageFromAMQPMessage(msg)
|
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-05-11 20:27:30 +03:00
|
|
|
span, ctx = r.startConsumerSpanFromWire(ctx, "sb.receiver.handleMessage", wireContext)
|
2018-02-07 21:29:09 +03:00
|
|
|
} else {
|
2018-05-11 20:27:30 +03:00
|
|
|
span, ctx = r.startConsumerSpanFromContext(ctx, "sb.receiver.handleMessage")
|
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)
|
|
|
|
|
|
|
|
err = handler(ctx, event)
|
|
|
|
if err != nil {
|
2018-05-16 21:39:14 +03:00
|
|
|
msg.Modify(true, false, nil)
|
|
|
|
log.For(ctx).Error(fmt.Errorf("message modify(true, false, nil): id: %v", id))
|
2018-02-07 21:29:09 +03:00
|
|
|
return
|
2018-01-23 02:47:09 +03:00
|
|
|
}
|
2018-02-07 21:29:09 +03:00
|
|
|
msg.Accept()
|
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-02-18 23:40:22 +03:00
|
|
|
func (r *receiver) listenForMessages(ctx context.Context, msgChan chan *amqp.Message) {
|
2018-05-11 20:27:30 +03:00
|
|
|
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)
|
|
|
|
if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {
|
2018-02-18 23:40:22 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:29:09 +03:00
|
|
|
if err != nil {
|
|
|
|
_, retryErr := common.Retry(5, 10*time.Second, func() (interface{}, error) {
|
2018-05-11 20:27:30 +03:00
|
|
|
sp, ctx := r.startConsumerSpanFromContext(ctx, "sb.receiver.listenForMessages.tryRecover")
|
2018-02-07 21:29:09 +03:00
|
|
|
defer sp.Finish()
|
|
|
|
|
|
|
|
err := r.Recover(ctx)
|
|
|
|
if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.For(ctx).Error(err)
|
|
|
|
return nil, common.Retryable(err.Error())
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if retryErr != nil {
|
|
|
|
r.lastError = retryErr
|
|
|
|
r.Close(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
2018-02-18 23:40:22 +03:00
|
|
|
}
|
2018-01-23 02:47:09 +03:00
|
|
|
select {
|
2018-02-18 23:40:22 +03:00
|
|
|
case msgChan <- msg:
|
|
|
|
case <-ctx.Done():
|
2018-01-23 02:47: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-02-07 21:29:09 +03:00
|
|
|
func (r *receiver) listenForMessage(ctx context.Context) (*amqp.Message, error) {
|
2018-05-11 20:27:30 +03:00
|
|
|
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-01-22 22:30:27 +03:00
|
|
|
// newSessionAndLink will replace the session and link on the receiver
|
2018-02-07 21:29:09 +03:00
|
|
|
func (r *receiver) newSessionAndLink(ctx context.Context) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []amqp.LinkOption{
|
2018-02-02 05:26:25 +03:00
|
|
|
amqp.LinkSourceAddress(r.entityPath),
|
2018-02-07 21:29:09 +03:00
|
|
|
amqp.LinkCredit(100),
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:00:45 +03:00
|
|
|
if r.requiredSessionID != nil {
|
|
|
|
opts = append(opts, amqp.LinkSessionFilter(*r.requiredSessionID))
|
|
|
|
r.session.SessionID = *r.requiredSessionID
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// ReceiverWithSession configures a receiver to use a session
|
|
|
|
func ReceiverWithSession(sessionID string) ReceiverOptions {
|
|
|
|
return func(r *receiver) error {
|
|
|
|
r.requiredSessionID = &sessionID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func (lc *ListenerHandle) Close(ctx context.Context) error {
|
|
|
|
return lc.r.Close(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done will close the channel when the listener has stopped
|
|
|
|
func (lc *ListenerHandle) Done() <-chan struct{} {
|
|
|
|
return lc.ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err will return the last error encountered
|
|
|
|
func (lc *ListenerHandle) Err() error {
|
|
|
|
if lc.r.lastError != nil {
|
|
|
|
return lc.r.lastError
|
|
|
|
}
|
|
|
|
return lc.ctx.Err()
|
|
|
|
}
|