File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { PubSub } from "@google-cloud/pubsub" ;
2
2
import { PubSubContactsMessage } from "./pubsub-contacts-message.model" ;
3
+ import { ServerError } from "./server-error.model" ;
3
4
5
+ const PUBLISH_TIMEOUT = 10_000 ;
4
6
export class PubSubClient {
5
7
private client : PubSub ;
6
8
private topicName : string ;
@@ -15,9 +17,27 @@ export class PubSubClient {
15
17
throw new Error ( "No pubsub topic name provided." ) ;
16
18
}
17
19
20
+ const timeout = ( ms : number ) =>
21
+ new Promise ( ( _ , reject ) =>
22
+ setTimeout (
23
+ ( ) =>
24
+ reject (
25
+ new ServerError (
26
+ 500 ,
27
+ "Could not publish message in time, did you forget to run gcloud auth application-default login?"
28
+ )
29
+ ) ,
30
+ ms
31
+ )
32
+ ) ;
33
+
18
34
const json = JSON . stringify ( message ) ;
19
35
const dataBuffer = Buffer . from ( json ) ;
20
36
const topic = this . client . topic ( this . topicName ) ;
21
- await topic . publishMessage ( { data : dataBuffer } ) ;
37
+
38
+ await Promise . race ( [
39
+ topic . publishMessage ( { data : dataBuffer } ) ,
40
+ timeout ( PUBLISH_TIMEOUT ) ,
41
+ ] ) ;
22
42
}
23
43
}
You can’t perform that action at this time.
0 commit comments