@@ -22,6 +22,12 @@ import { EventEmitter } from "events";
22
22
import { getLogger } from "@cocalc/conat/client" ;
23
23
import { until } from "@cocalc/util/async-utils" ;
24
24
25
+ let DEFAULT_RECONNECT_DELAY = 1500 ;
26
+
27
+ export function setDefaultReconnectDelay ( delay ) {
28
+ DEFAULT_RECONNECT_DELAY = delay ;
29
+ }
30
+
25
31
interface GetAllOpts {
26
32
start_seq ?: number ;
27
33
end_seq ?: number ;
@@ -32,11 +38,8 @@ interface GetAllOpts {
32
38
const logger = getLogger ( "persist:client" ) ;
33
39
34
40
export type ChangefeedEvent = ( SetOperation | DeleteOperation ) [ ] ;
35
-
36
41
export type Changefeed = EventIterator < ChangefeedEvent > ;
37
42
38
- // const paths = new Set<string>();
39
-
40
43
export { type PersistStreamClient } ;
41
44
class PersistStreamClient extends EventEmitter {
42
45
public socket : ConatSocketClient ;
@@ -69,7 +72,6 @@ class PersistStreamClient extends EventEmitter {
69
72
return ;
70
73
}
71
74
this . socket ?. close ( ) ;
72
- // console.log("making a socket connection to ", persistSubject(this.user));
73
75
const subject = persistSubject ( { ...this . user , service : this . service } ) ;
74
76
this . socket = this . client . socket . connect ( subject , {
75
77
desc : `persist: ${ this . storage . path } ` ,
@@ -88,15 +90,14 @@ class PersistStreamClient extends EventEmitter {
88
90
}
89
91
90
92
this . socket . once ( "disconnected" , ( ) => {
91
- // console.log("persist client was disconnected", this.storage.path);
92
93
this . reconnecting = true ;
93
94
this . socket . removeAllListeners ( ) ;
94
- setTimeout ( this . init , 1000 ) ;
95
+ setTimeout ( this . init , DEFAULT_RECONNECT_DELAY ) ;
95
96
} ) ;
96
97
this . socket . once ( "closed" , ( ) => {
97
98
this . reconnecting = true ;
98
99
this . socket . removeAllListeners ( ) ;
99
- setTimeout ( this . init , 1000 ) ;
100
+ setTimeout ( this . init , DEFAULT_RECONNECT_DELAY ) ;
100
101
} ) ;
101
102
102
103
this . socket . on ( "data" , ( updates , headers ) => {
@@ -131,6 +132,22 @@ class PersistStreamClient extends EventEmitter {
131
132
}
132
133
try {
133
134
await this . socket . waitUntilReady ( 15000 ) ;
135
+ if ( this . changefeeds . length == 0 || this . state != "ready" ) {
136
+ return true ;
137
+ }
138
+ const resp = await this . socket . request ( null , {
139
+ headers : {
140
+ cmd : "changefeed" ,
141
+ } ,
142
+ } ) ;
143
+ if ( resp . headers ?. error ) {
144
+ throw new ConatError ( `${ resp . headers ?. error } ` , {
145
+ code : resp . headers ?. code ,
146
+ } ) ;
147
+ }
148
+ if ( this . changefeeds . length == 0 || this . state != "ready" ) {
149
+ return true ;
150
+ }
134
151
const updates = await this . getAll ( {
135
152
start_seq : this . lastSeq ,
136
153
timeout : 15000 ,
@@ -178,7 +195,6 @@ class PersistStreamClient extends EventEmitter {
178
195
close = ( ) => {
179
196
logger . debug ( "close" , this . storage ) ;
180
197
// paths.delete(this.storage.path);
181
- // console.log("persist -- close", this.storage.path, paths);
182
198
this . state = "closed" ;
183
199
this . emit ( "closed" ) ;
184
200
for ( const iter of this . changefeeds ) {
0 commit comments