@@ -34,7 +34,7 @@ export class Server implements IServer {
34
34
private lastApplied : number ;
35
35
public readonly log : ILog ;
36
36
public readonly logger : ILogger ;
37
- public readonly rpcService : IRpcService ;
37
+ private readonly rpcService : IRpcService ;
38
38
private rpcServiceDetachers : Set < IDetacher > ;
39
39
private state : IState ;
40
40
public readonly stateMachine : IStateMachine ;
@@ -119,6 +119,17 @@ export class Server implements IServer {
119
119
await this . state . handleRpcMessage ( endpoint , message ) ;
120
120
}
121
121
122
+ public async sendRpcMessage ( endpoint : IEndpoint , message : IRpcMessage ) : Promise < void > {
123
+ // Before responding to an RPC request, the recipient `Server`
124
+ // updates persistent state on stable storage.
125
+ // > *§5. "...(Updated on stable storage before responding)..."*
126
+ // > *§5.6 "...Raft’s RPCs...require the recipient to persist..."*
127
+ if ( isRpcResponse ( message ) ) {
128
+ await this . updatePersistentState ( ) ;
129
+ }
130
+ await this . rpcService . send ( endpoint , message ) ;
131
+ }
132
+
122
133
// When the term is updated, it is not immediately
123
134
// persisted because, as the Raft paper says, the
124
135
// term is part of persistent state that is:
@@ -155,22 +166,13 @@ export class Server implements IServer {
155
166
this . commitIndex = this . lastApplied = 0 ;
156
167
157
168
this . logger . debug ( 'Loading persistent state' ) ;
169
+
158
170
// The current term is...
159
171
// > *§5. "...initialized to zero on first boot..."*
160
172
await this . currentTerm . readIfExistsElseSetAndWrite ( 0 )
161
173
162
174
this . logger . debug ( 'Starting RPC service' ) ;
163
175
164
- this . rpcServiceDetachers . add ( this . rpcService . onBeforeSend ( async ( endpoint , message ) => {
165
- // Before responding to an RPC request, the recipient `Server`
166
- // updates persistent state on stable storage.
167
- // > *§5. "...(Updated on stable storage before responding)..."*
168
- // > *§5.6 "...Raft’s RPCs...require the recipient to persist..."*
169
- if ( isRpcResponse ( message ) ) {
170
- await this . updatePersistentState ( ) ;
171
- }
172
- } ) ) ;
173
-
174
176
this . rpcServiceDetachers . add ( this . rpcService . onReceive ( async ( endpoint , message ) => {
175
177
await this . handleRpcMessage ( endpoint , message ) ;
176
178
} ) ) ;
0 commit comments