Home > Back-end >  what is best way that grpc reconnect to other server with stateful data?
what is best way that grpc reconnect to other server with stateful data?

Time:11-02

i have 2 server that is using grpc.

my detail server logic is described like down.

Server structure (----> is grpc connection)

Client ----> ServerA ----> ServerB

data stream like below.

  1. client send stream data. ex (DEF data send)
  2. ServerA stores data. (ABC is previous data and DEF is new data, so total Data is ABCDEF)
  3. ServerA send newest data to ServerB from client(DEF)

In this situation, when some event occurs, I need to disconnect from ServerB and connect another server.

for example,

The schematic diagram when an event occurs is as follows.

Client ----> ServerA --X(Stream Stop)--> ServerB
                      
               ㅣ--(reconnect to other server using grpc)--> ServerB'

I want to send the old data streams sequentially.

even server is chainging ServerA is received data from Client(EX data is GHI)

so, When ServerA's connection change is completed.

data flow like below

  1. Server A send previous total data to ServerB' (ABCDEF)
  2. Server A is receiving newest data. may be this data`ll be received when server change is not compeleted (GHI)
  3. newest data(GHI) is send to ServerB'

and i want this flowchart to work correctly.

could you tell me any good idea??

thank you.

CodePudding user response:

i find answer. the answer is reactive X.

for example,

ReplaySubject subject = ReplaySubject.create();
subject.subscribe(..anysub function);

...
void pushData(int data) {
    if(server change) {
        subject data supress
        subject clear // caused when replay subscribe clear
        ReplaySubject newSubject = ReplaySubject.create();        
        newSubject.onNext(suppress data);
        newSubject.onNext(data);
        subject = newSubject;
    } else {
        subject.onNext(data);
    }
    //when server change event trigger

}

that is my best way.

  • Related