@@ -12,14 +12,14 @@ use universalpubsub::{NextOutput, Subscriber};
12
12
13
13
use crate :: {
14
14
error:: { WorkflowError , WorkflowResult } ,
15
- message:: { Message , NatsMessage , NatsMessageWrapper } ,
15
+ message:: { Message , PubsubMessage , PubsubMessageWrapper } ,
16
16
utils:: { self , tags:: AsTags } ,
17
17
} ;
18
18
19
19
#[ derive( Clone ) ]
20
20
pub struct MessageCtx {
21
- /// The connection used to communicate with NATS .
22
- nats : UpsPool ,
21
+ /// The connection used to communicate with pubsub .
22
+ pubsub : UpsPool ,
23
23
24
24
ray_id : Id ,
25
25
@@ -35,7 +35,7 @@ impl MessageCtx {
35
35
ray_id : Id ,
36
36
) -> WorkflowResult < Self > {
37
37
Ok ( MessageCtx {
38
- nats : pools. ups ( ) . map_err ( WorkflowError :: PoolsGeneric ) ?,
38
+ pubsub : pools. ups ( ) . map_err ( WorkflowError :: PoolsGeneric ) ?,
39
39
ray_id,
40
40
config : config. clone ( ) ,
41
41
} )
@@ -44,7 +44,7 @@ impl MessageCtx {
44
44
45
45
// MARK: Publishing messages
46
46
impl MessageCtx {
47
- /// Publishes a message to NATS and to a durable message stream if a topic is
47
+ /// Publishes a message to pubsub and to a durable message stream if a topic is
48
48
/// set.
49
49
///
50
50
/// Use `subscribe` to consume these messages ephemerally and `tail` to read
@@ -94,7 +94,7 @@ impl MessageCtx {
94
94
where
95
95
M : Message ,
96
96
{
97
- let nats_subject = M :: nats_subject ( ) ;
97
+ let subject = M :: subject ( ) ;
98
98
let duration_since_epoch = std:: time:: SystemTime :: now ( )
99
99
. duration_since ( std:: time:: UNIX_EPOCH )
100
100
. unwrap_or_else ( |err| unreachable ! ( "time is broken: {}" , err) ) ;
@@ -109,7 +109,7 @@ impl MessageCtx {
109
109
110
110
// Serialize message
111
111
let req_id = Id :: new_v1 ( self . config . dc_label ( ) ) ;
112
- let message = NatsMessageWrapper {
112
+ let message = PubsubMessageWrapper {
113
113
req_id,
114
114
ray_id : self . ray_id ,
115
115
tags : tags. as_tags ( ) ?,
@@ -119,7 +119,7 @@ impl MessageCtx {
119
119
let message_buf = serde_json:: to_vec ( & message) . map_err ( WorkflowError :: SerializeMessage ) ?;
120
120
121
121
tracing:: debug!(
122
- %nats_subject ,
122
+ %subject ,
123
123
body_bytes = ?body_buf_len,
124
124
message_bytes = ?message_buf. len( ) ,
125
125
"publish message"
@@ -128,15 +128,15 @@ impl MessageCtx {
128
128
// It's important to write to the stream as fast as possible in order to
129
129
// ensure messages are handled quickly.
130
130
let message_buf = Arc :: new ( message_buf) ;
131
- self . message_publish_nats :: < M > ( & nats_subject , message_buf)
131
+ self . message_publish_pubsub :: < M > ( & subject , message_buf)
132
132
. await ;
133
133
134
134
Ok ( ( ) )
135
135
}
136
136
137
- /// Publishes the message to NATS .
137
+ /// Publishes the message to pubsub .
138
138
#[ tracing:: instrument( level = "debug" , skip_all) ]
139
- async fn message_publish_nats < M > ( & self , nats_subject : & str , message_buf : Arc < Vec < u8 > > )
139
+ async fn message_publish_pubsub < M > ( & self , subject : & str , message_buf : Arc < Vec < u8 > > )
140
140
where
141
141
M : Message ,
142
142
{
@@ -146,19 +146,19 @@ impl MessageCtx {
146
146
// Ignore for infinite backoff
147
147
backoff. tick ( ) . await ;
148
148
149
- let nats_subject = nats_subject . to_owned ( ) ;
149
+ let subject = subject . to_owned ( ) ;
150
150
151
151
tracing:: trace!(
152
- %nats_subject ,
152
+ %subject ,
153
153
message_len = message_buf. len( ) ,
154
- "publishing message to nats "
154
+ "publishing message to pubsub "
155
155
) ;
156
- if let Err ( err) = self . nats . publish ( & nats_subject , & ( * message_buf) ) . await {
156
+ if let Err ( err) = self . pubsub . publish ( & subject , & ( * message_buf) ) . await {
157
157
tracing:: warn!( ?err, "publish message failed, trying again" ) ;
158
158
continue ;
159
159
}
160
160
161
- tracing:: debug!( "publish nats message succeeded" ) ;
161
+ tracing:: debug!( "publish pubsub message succeeded" ) ;
162
162
break ;
163
163
}
164
164
}
@@ -172,21 +172,21 @@ impl MessageCtx {
172
172
173
173
// MARK: Subscriptions
174
174
impl MessageCtx {
175
- /// Listens for gasoline messages globally on NATS .
175
+ /// Listens for gasoline messages globally on pubsub .
176
176
#[ tracing:: instrument( skip_all, fields( message = M :: NAME ) ) ]
177
177
pub async fn subscribe < M > ( & self , tags : impl AsTags ) -> WorkflowResult < SubscriptionHandle < M > >
178
178
where
179
179
M : Message ,
180
180
{
181
181
self . subscribe_opt :: < M > ( SubscribeOpts {
182
182
tags : tags. as_tags ( ) ?,
183
- flush_nats : true ,
183
+ flush : true ,
184
184
} )
185
185
. in_current_span ( )
186
186
. await
187
187
}
188
188
189
- /// Listens for gasoline messages globally on NATS .
189
+ /// Listens for gasoline messages globally on pubsub .
190
190
#[ tracing:: instrument( skip_all, fields( message = M :: NAME ) ) ]
191
191
pub async fn subscribe_opt < M > (
192
192
& self ,
@@ -195,32 +195,32 @@ impl MessageCtx {
195
195
where
196
196
M : Message ,
197
197
{
198
- let nats_subject = M :: nats_subject ( ) ;
198
+ let subject = M :: subject ( ) ;
199
199
200
200
// Create subscription and flush immediately.
201
- tracing:: debug!( %nats_subject , tags = ?opts. tags, "creating subscription" ) ;
201
+ tracing:: debug!( %subject , tags = ?opts. tags, "creating subscription" ) ;
202
202
let subscription = self
203
- . nats
204
- . subscribe ( & nats_subject )
203
+ . pubsub
204
+ . subscribe ( & subject )
205
205
. await
206
206
. map_err ( |x| WorkflowError :: CreateSubscription ( x. into ( ) ) ) ?;
207
- if opts. flush_nats {
208
- self . nats
207
+ if opts. flush {
208
+ self . pubsub
209
209
. flush ( )
210
210
. await
211
- . map_err ( |x| WorkflowError :: FlushNats ( x. into ( ) ) ) ?;
211
+ . map_err ( |x| WorkflowError :: FlushPubsub ( x. into ( ) ) ) ?;
212
212
}
213
213
214
214
// Return handle
215
- let subscription = SubscriptionHandle :: new ( nats_subject , subscription, opts. tags . clone ( ) ) ;
215
+ let subscription = SubscriptionHandle :: new ( subject , subscription, opts. tags . clone ( ) ) ;
216
216
Ok ( subscription)
217
217
}
218
218
}
219
219
220
220
#[ derive( Debug ) ]
221
221
pub struct SubscribeOpts {
222
222
pub tags : serde_json:: Value ,
223
- pub flush_nats : bool ,
223
+ pub flush : bool ,
224
224
}
225
225
226
226
/// Used to receive messages from other contexts.
@@ -291,15 +291,15 @@ where
291
291
///
292
292
/// This future can be safely dropped.
293
293
#[ tracing:: instrument( name="message_next" , skip_all, fields( message = M :: NAME ) ) ]
294
- pub async fn next ( & mut self ) -> WorkflowResult < NatsMessage < M > > {
294
+ pub async fn next ( & mut self ) -> WorkflowResult < PubsubMessage < M > > {
295
295
tracing:: debug!( "waiting for message" ) ;
296
296
297
297
loop {
298
298
// Poll the subscription.
299
299
//
300
300
// Use blocking threads instead of `try_next`, since I'm not sure
301
301
// try_next works as intended.
302
- let nats_message = match self . subscription . next ( ) . await {
302
+ let message = match self . subscription . next ( ) . await {
303
303
Ok ( NextOutput :: Message ( msg) ) => msg,
304
304
Ok ( NextOutput :: Unsubscribed ) => {
305
305
tracing:: debug!( "unsubscribed" ) ;
@@ -311,11 +311,11 @@ where
311
311
}
312
312
} ;
313
313
314
- let message_wrapper = NatsMessage :: < M > :: deserialize_wrapper ( & nats_message . payload ) ?;
314
+ let message_wrapper = PubsubMessage :: < M > :: deserialize_wrapper ( & message . payload ) ?;
315
315
316
316
// Check if the subscription tags match a subset of the message tags
317
317
if utils:: is_value_subset ( & self . tags , & message_wrapper. tags ) {
318
- let message = NatsMessage :: < M > :: deserialize_from_wrapper ( message_wrapper) ?;
318
+ let message = PubsubMessage :: < M > :: deserialize_from_wrapper ( message_wrapper) ?;
319
319
tracing:: debug!( ?message, "received message" ) ;
320
320
321
321
return Ok ( message) ;
@@ -326,7 +326,7 @@ where
326
326
}
327
327
328
328
/// Converts the subscription in to a stream.
329
- pub fn into_stream ( self ) -> impl futures_util:: Stream < Item = WorkflowResult < NatsMessage < M > > > {
329
+ pub fn into_stream ( self ) -> impl futures_util:: Stream < Item = WorkflowResult < PubsubMessage < M > > > {
330
330
futures_util:: stream:: try_unfold ( self , |mut sub| {
331
331
async move {
332
332
let message = sub. next ( ) . await ?;
0 commit comments