|
@ -12,6 +12,7 @@ import ( |
|
|
|
|
|
|
|
|
type Subscriber struct { |
|
|
type Subscriber struct { |
|
|
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient |
|
|
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient |
|
|
|
|
|
subscriberCancels []context.CancelFunc |
|
|
subscriberId string |
|
|
subscriberId string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -21,6 +22,7 @@ func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, |
|
|
PartitionCount: 4, |
|
|
PartitionCount: 4, |
|
|
} |
|
|
} |
|
|
subscriberClients := make([]messaging_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount) |
|
|
subscriberClients := make([]messaging_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount) |
|
|
|
|
|
subscriberCancels := make([]context.CancelFunc, topicConfiguration.PartitionCount) |
|
|
|
|
|
|
|
|
for i := 0; i < int(topicConfiguration.PartitionCount); i++ { |
|
|
for i := 0; i < int(topicConfiguration.PartitionCount); i++ { |
|
|
if partitionId>=0 && i != partitionId { |
|
|
if partitionId>=0 && i != partitionId { |
|
@ -35,21 +37,24 @@ func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
client, err := setupSubscriberClient(grpcClientConn, tp, subscriberId, startTime) |
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
|
client, err := setupSubscriberClient(ctx, grpcClientConn, tp, subscriberId, startTime) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
subscriberClients[i] = client |
|
|
subscriberClients[i] = client |
|
|
|
|
|
subscriberCancels[i] = cancel |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return &Subscriber{ |
|
|
return &Subscriber{ |
|
|
subscriberClients: subscriberClients, |
|
|
subscriberClients: subscriberClients, |
|
|
|
|
|
subscriberCancels: subscriberCancels, |
|
|
subscriberId: subscriberId, |
|
|
subscriberId: subscriberId, |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func setupSubscriberClient(grpcConnection *grpc.ClientConn, tp broker.TopicPartition, subscriberId string, startTime time.Time) (stream messaging_pb.SeaweedMessaging_SubscribeClient, err error) { |
|
|
|
|
|
stream, err = messaging_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(context.Background()) |
|
|
|
|
|
|
|
|
func setupSubscriberClient(ctx context.Context, grpcConnection *grpc.ClientConn, tp broker.TopicPartition, subscriberId string, startTime time.Time) (stream messaging_pb.SeaweedMessaging_SubscribeClient, err error) { |
|
|
|
|
|
stream, err = messaging_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(ctx) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -98,3 +103,11 @@ func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Subscriber) Shutdown() { |
|
|
|
|
|
for i := 0; i < len(s.subscriberClients); i++ { |
|
|
|
|
|
if s.subscriberCancels[i] != nil { |
|
|
|
|
|
s.subscriberCancels[i]() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |