github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/pubsub_extensions.go (about)

     1  package cluster
     2  
     3  import (
     4  	"github.com/asynkron/protoactor-go/actor"
     5  )
     6  
     7  // Publisher creates a new PubSub publisher that publishes messages directly to the TopicActor
     8  func (c *Cluster) Publisher() Publisher {
     9  	return NewPublisher(c)
    10  }
    11  
    12  // BatchingProducer create a new PubSub batching producer for specified topic, that publishes directly to the topic actor
    13  func (c *Cluster) BatchingProducer(topic string, opts ...BatchingProducerConfigOption) *BatchingProducer {
    14  	return NewBatchingProducer(c.Publisher(), topic, opts...)
    15  }
    16  
    17  // SubscribeByPid subscribes to a PubSub topic by subscriber PID
    18  func (c *Cluster) SubscribeByPid(topic string, pid *actor.PID, opts ...GrainCallOption) (*SubscribeResponse, error) {
    19  	res, err := c.Request(topic, TopicActorKind, &SubscribeRequest{
    20  		Subscriber: &SubscriberIdentity{Identity: &SubscriberIdentity_Pid{Pid: pid}},
    21  	}, opts...)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return res.(*SubscribeResponse), err
    26  }
    27  
    28  // SubscribeByClusterIdentity subscribes to a PubSub topic by cluster identity
    29  func (c *Cluster) SubscribeByClusterIdentity(topic string, identity *ClusterIdentity, opts ...GrainCallOption) (*SubscribeResponse, error) {
    30  	res, err := c.Request(topic, TopicActorKind, &SubscribeRequest{
    31  		Subscriber: &SubscriberIdentity{Identity: &SubscriberIdentity_ClusterIdentity{ClusterIdentity: identity}},
    32  	}, opts...)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return res.(*SubscribeResponse), err
    37  }
    38  
    39  // SubscribeWithReceive subscribe to a PubSub topic by providing a Receive function, that will be used to spawn a subscriber actor
    40  func (c *Cluster) SubscribeWithReceive(topic string, receive actor.ReceiveFunc, opts ...GrainCallOption) (*SubscribeResponse, error) {
    41  	props := actor.PropsFromFunc(receive)
    42  	pid := c.ActorSystem.Root.Spawn(props)
    43  	return c.SubscribeByPid(topic, pid, opts...)
    44  }
    45  
    46  // UnsubscribeByPid unsubscribes from a PubSub topic by subscriber PID
    47  func (c *Cluster) UnsubscribeByPid(topic string, pid *actor.PID, opts ...GrainCallOption) (*UnsubscribeResponse, error) {
    48  	res, err := c.Request(topic, TopicActorKind, &UnsubscribeRequest{
    49  		Subscriber: &SubscriberIdentity{Identity: &SubscriberIdentity_Pid{Pid: pid}},
    50  	}, opts...)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return res.(*UnsubscribeResponse), err
    55  }
    56  
    57  // UnsubscribeByClusterIdentity unsubscribes from a PubSub topic by cluster identity
    58  func (c *Cluster) UnsubscribeByClusterIdentity(topic string, identity *ClusterIdentity, opts ...GrainCallOption) (*UnsubscribeResponse, error) {
    59  	res, err := c.Request(topic, TopicActorKind, &UnsubscribeRequest{
    60  		Subscriber: &SubscriberIdentity{Identity: &SubscriberIdentity_ClusterIdentity{ClusterIdentity: identity}},
    61  	}, opts...)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return res.(*UnsubscribeResponse), err
    66  }
    67  
    68  // UnsubscribeByIdentityAndKind unsubscribes from a PubSub topic by cluster identity
    69  func (c *Cluster) UnsubscribeByIdentityAndKind(topic string, identity string, kind string, opts ...GrainCallOption) (*UnsubscribeResponse, error) {
    70  	res, err := c.Request(topic, TopicActorKind, &UnsubscribeRequest{
    71  		Subscriber: &SubscriberIdentity{Identity: &SubscriberIdentity_ClusterIdentity{ClusterIdentity: NewClusterIdentity(identity, kind)}},
    72  	}, opts...)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	return res.(*UnsubscribeResponse), err
    77  }