...

Source file src/solace.dev/go/messaging/pkg/solace/resource/destination.go

Documentation: solace.dev/go/messaging/pkg/solace/resource

     1  // pubsubplus-go-client
     2  //
     3  // Copyright 2021-2024 Solace Corporation. All rights reserved.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  // Package resource contains types and factory functions for various
    18  // broker resources such as topics and queues.
    19  package resource
    20  
    21  import "fmt"
    22  
    23  // Destination represents a message destination on a broker.
    24  // Some examples of destinations include queues and topics.
    25  // Destination implementations can be retrieved by the various helper functions,
    26  // such as TopicOf, QueueDurableExclusive, and TopicSubscriptionOf.
    27  type Destination interface {
    28  	// GetName retrieves the name of the destination
    29  	GetName() string
    30  }
    31  
    32  // Subscription represents the valid subscriptions that can be specified to receivers.
    33  // Valid subscriptions include *resource.TopicSubscription.
    34  type Subscription interface {
    35  	Destination
    36  	// GetSubscriptionType will return the type of the subscription as a string
    37  	GetSubscriptionType() string
    38  }
    39  
    40  // Topic is an implementation of destination representing a topic
    41  // that can be published to.
    42  type Topic struct {
    43  	name string
    44  }
    45  
    46  // TopicOf creates a new topic with the specified name.
    47  // Topic name must not be empty.
    48  func TopicOf(expression string) *Topic {
    49  	return &Topic{expression}
    50  }
    51  
    52  // GetName returns the name of the topic. Implements the Destination interface.
    53  func (t *Topic) GetName() string {
    54  	return t.name
    55  }
    56  
    57  // String implements fmt.Stringer
    58  func (t *Topic) String() string {
    59  	return fmt.Sprintf("Topic: %s", t.GetName())
    60  }
    61  
    62  // ShareName is an interface for identifiers that are associated
    63  // with a shared subscription.
    64  // See https://docs.solace.com/PubSub-Basics/Direct-Messages.htm#Shared in the Solace documentation.
    65  type ShareName struct {
    66  	name string
    67  }
    68  
    69  // ShareNameOf returns a new share name with the provided name.
    70  // Valid share names are not empty and do not contain special
    71  // characters '>' or '*'. Returns a new ShareName with the given string.
    72  func ShareNameOf(name string) *ShareName {
    73  	return &ShareName{name}
    74  }
    75  
    76  // GetName returns the share name. Implements the Destination interface.
    77  func (sn *ShareName) GetName() string {
    78  	return sn.name
    79  }
    80  
    81  // String implements fmt.Stringer
    82  func (sn *ShareName) String() string {
    83  	return fmt.Sprintf("ShareName: %s", sn.GetName())
    84  }
    85  
    86  // TopicSubscription is a subscription to a topic often used for receivers.
    87  type TopicSubscription struct {
    88  	topic string
    89  }
    90  
    91  // GetName returns the topic subscription expression. Implements the Destination interface.
    92  func (t *TopicSubscription) GetName() string {
    93  	return t.topic
    94  }
    95  
    96  // GetSubscriptionType returns the type of the topic subscription as a string
    97  func (t *TopicSubscription) GetSubscriptionType() string {
    98  	return fmt.Sprintf("%T", t)
    99  }
   100  
   101  // String implements fmt.Stringer
   102  func (t *TopicSubscription) String() string {
   103  	return fmt.Sprintf("TopicSubscription: %s", t.GetName())
   104  }
   105  
   106  // TopicSubscriptionOf creates a TopicSubscription of the specified topic string.
   107  func TopicSubscriptionOf(topic string) *TopicSubscription {
   108  	return &TopicSubscription{topic}
   109  }
   110  
   111  // Queue represents a queue used for guaranteed messaging receivers.
   112  type Queue struct {
   113  	name                           string
   114  	exclusivelyAccessible, durable bool
   115  }
   116  
   117  // GetName returns the name of the queue. Implements the Destination interface.
   118  func (q *Queue) GetName() string {
   119  	return q.name
   120  }
   121  
   122  // IsExclusivelyAccessible determines if Queue supports exclusive or shared-access mode.
   123  // Returns true if the Queue can serve only one consumer at any one time, false if the
   124  // Queue can serve multiple consumers with each consumer serviced in a round-robin fashion.
   125  func (q *Queue) IsExclusivelyAccessible() bool {
   126  	return q.exclusivelyAccessible
   127  }
   128  
   129  // IsDurable determines if the Queue is durable. Durable queues are privisioned objects on
   130  // the broker that have a lifespan that is independent of any one client session.
   131  func (q *Queue) IsDurable() bool {
   132  	return q.durable
   133  }
   134  
   135  func (q *Queue) String() string {
   136  	return fmt.Sprintf("Queue: %s, exclusive: %t, durable: %t", q.GetName(), q.IsExclusivelyAccessible(), q.IsDurable())
   137  }
   138  
   139  // QueueDurableExclusive creates a new durable, exclusive queue with the specified name.
   140  func QueueDurableExclusive(queueName string) *Queue {
   141  	return &Queue{
   142  		name:                  queueName,
   143  		exclusivelyAccessible: true,
   144  		durable:               true,
   145  	}
   146  }
   147  
   148  // QueueDurableNonExclusive creates a durable, non-exclusive queue with the specified name.
   149  func QueueDurableNonExclusive(queueName string) *Queue {
   150  	return &Queue{
   151  		name:                  queueName,
   152  		exclusivelyAccessible: false,
   153  		durable:               true,
   154  	}
   155  }
   156  
   157  // QueueNonDurableExclusive creates an exclusive, non-durable queue with the specified name.
   158  func QueueNonDurableExclusive(queueName string) *Queue {
   159  	return &Queue{
   160  		name:                  queueName,
   161  		exclusivelyAccessible: true,
   162  		durable:               false,
   163  	}
   164  }
   165  
   166  // QueueNonDurableExclusiveAnonymous creates an anonymous, exclusive, and non-durable queue.
   167  func QueueNonDurableExclusiveAnonymous() *Queue {
   168  	return &Queue{
   169  		name:                  "",
   170  		exclusivelyAccessible: true,
   171  		durable:               false,
   172  	}
   173  }
   174