grpc.service.method.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import * as grpc from '@grpc/grpc-js';
  2. import { Subject, Subscription } from "rxjs";
  3. import { ReportStatus, ColorCode, Message, MessageLog, ConnectionAttribute, ConnectionRequest, GrpcConnectionType } from "../interfaces/general.interface";
  4. import { Status } from '@grpc/grpc-js/build/src/constants';
  5. import { v4 as uuidv4 } from 'uuid'
  6. import { message_proto } from './protos/server.proto'
  7. import { ServerWritableStreamImpl } from '@grpc/grpc-js/build/src/server-call';
  8. export class GrpcServiceMethod {
  9. private server: grpc.Server | any
  10. private messageToBeSendOver: Message | any
  11. private callRequestsFromRemote: ServerWritableStreamImpl<any, ResponseType>[] = []
  12. public async create(request: ConnectionRequest, connectionAttribute: ConnectionAttribute): Promise<any> {
  13. // Assuming currently only one client
  14. this.createGrpcInstance(request.server.serverUrl, { instanceType: 'server' }, connectionAttribute)
  15. this.createGrpcInstance(request.client.targetServer, { instanceType: 'client' }, connectionAttribute)
  16. }
  17. // For testing only
  18. public async shutDownServer(): Promise<string> {
  19. return new Promise((resolve, reject) => {
  20. console.log(`Shutting down servers...`)
  21. if (this.server) {
  22. this.callRequestsFromRemote[0].destroy()
  23. this.callRequestsFromRemote[0].end()
  24. this.server.forceShutdown()
  25. let message: string = `Server shut down successfully!`
  26. resolve(message)
  27. }
  28. if (!this.server) {
  29. let errorMsg: string = `There's no active server here`
  30. reject(errorMsg)
  31. }
  32. })
  33. }
  34. private async generateAdditionalAttributes(connectionAttribute: ConnectionAttribute, clientInfo?: any, localInfo?: any) {
  35. if (clientInfo) {
  36. connectionAttribute.inComing.StreamID = clientInfo.channelID
  37. connectionAttribute.inComing.PublisherID = clientInfo.publisherID
  38. connectionAttribute.inComing.SubscriberID = clientInfo.subscriberID
  39. }
  40. if (localInfo) {
  41. connectionAttribute.outGoing.StreamID = localInfo.channelID
  42. connectionAttribute.outGoing.PublisherID = localInfo.publisherID
  43. connectionAttribute.outGoing.SubscriberID = localInfo.subscriberID
  44. }
  45. if (connectionAttribute.outGoing.StreamID && connectionAttribute.inComing.StreamID) {
  46. connectionAttribute.ConnectionID.local = connectionAttribute.outGoing.StreamID + connectionAttribute.inComing.StreamID
  47. connectionAttribute.ConnectionID.remote = connectionAttribute.inComing.StreamID + connectionAttribute.outGoing.StreamID
  48. let report: ReportStatus = {
  49. code: ColorCode.GREEN,
  50. message: `ConnectionID acquired. Informing Restranmission to release Messages...`,
  51. }
  52. connectionAttribute.connectionStatus.next(report)
  53. // console.log(connectionAttribute)
  54. }
  55. }
  56. // To be migrated into a service in the immediate future
  57. private async createGrpcInstance(
  58. serverUrl: string,
  59. grpcType: GrpcConnectionType,
  60. connectionAttribute: ConnectionAttribute,
  61. ) {
  62. let statusControl: Subject<ReportStatus> = connectionAttribute.connectionStatus
  63. let consecutiveResolutions = 0;
  64. let lastResolutionTime = Date.now();
  65. let yellowErrorEmission: boolean = false
  66. let redErrorEmission: boolean = false
  67. while (true) {
  68. try {
  69. let recreatePromise = new Promise((resolve) => {
  70. if (grpcType.instanceType == 'server') {
  71. this.createServerStreamingServer(serverUrl, connectionAttribute).then(() => {
  72. resolve('recreate')
  73. })
  74. }
  75. if (grpcType.instanceType == 'client') {
  76. this.createServerStreamingClient(serverUrl, connectionAttribute).then(() => {
  77. resolve('recreate')
  78. })
  79. }
  80. })
  81. await recreatePromise
  82. // If connection resolves (indicating failure), increment the count
  83. consecutiveResolutions++;
  84. // console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
  85. if (redErrorEmission == false) {
  86. redErrorEmission = true
  87. // console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
  88. let error: ReportStatus = {
  89. code: ColorCode.YELLOW,
  90. message: 'Server is not responding. Proceed to buffer.',
  91. }
  92. statusControl.next(error)
  93. }
  94. } catch (error) {
  95. // Connection did not resolve, reset the count
  96. consecutiveResolutions = 0;
  97. console.error('Connection attempt failed:', error);
  98. }
  99. // Check for a pause of more than 3 seconds since the last resolution attempt
  100. const currentTime = Date.now();
  101. const timeSinceLastResolution = currentTime - lastResolutionTime;
  102. if (timeSinceLastResolution > 2000) {
  103. consecutiveResolutions = 0;
  104. yellowErrorEmission = false
  105. redErrorEmission = false
  106. }
  107. // Update the last resolution time
  108. lastResolutionTime = currentTime;
  109. await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second before the next attempt
  110. // timeout generate message to trigger this reconnection
  111. }
  112. }
  113. // Create Server Instance to stream all application Outgoing messages
  114. public async createServerStreamingServer(
  115. serverUrl: string,
  116. connectionAttribute: ConnectionAttribute
  117. ): Promise<any> { // '0.0.0.0:3001'
  118. return new Promise((resolve, reject) => {
  119. try {
  120. if (!this.server) {
  121. this.server = new grpc.Server();
  122. }
  123. this.server.addService(message_proto.Message.service, {
  124. HandleMessage: (call) => {
  125. this.callRequestsFromRemote.push(call)
  126. let clientInfo = JSON.parse(call.request.message)
  127. this.generateAdditionalAttributes(connectionAttribute, clientInfo)
  128. console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
  129. if (connectionAttribute.outGoing.MessageToBePublished) {
  130. let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
  131. next: (response: Message) => {
  132. console.log(`Sending from GRPC server: ${(response.message as MessageLog).appData.msgId} `)
  133. this.messageToBeSendOver = response
  134. let message = {
  135. id: response.id,
  136. message: JSON.stringify(response.message)
  137. }
  138. call.write(message)
  139. },
  140. error: err => {
  141. console.error(err)
  142. subscription.unsubscribe()
  143. resolve('')
  144. },
  145. complete: () => {
  146. console.log(`Stream response completed for ${call.request.id}`)
  147. subscription.unsubscribe()
  148. resolve('')
  149. }
  150. })
  151. }
  152. },
  153. Check: (_, callback) => {
  154. // for now it is just sending the status message over to tell the client it is alive
  155. // For simplicity, always return "SERVING" as status
  156. callback(null, { status: 'SERVING' });
  157. },
  158. });
  159. // Bind and start the server
  160. this.server.bindAsync(serverUrl, grpc.ServerCredentials.createInsecure(), () => {
  161. console.log(`gRPC server is running on ${serverUrl}`);
  162. this.server.start();
  163. });
  164. }
  165. catch (error) {
  166. resolve(error)
  167. }
  168. })
  169. }
  170. // Send a request over to the other server to open a channel for this server to emit/stream messages over
  171. public async createServerStreamingClient(
  172. server: string,
  173. connectionAttribute: ConnectionAttribute
  174. ): Promise<string> {
  175. return new Promise(async (resolve, reject) => {
  176. const client = new message_proto.Message(server, grpc.credentials.createInsecure());
  177. let outGoingInfo: any = {
  178. channelID: uuidv4(),
  179. publisherID: uuidv4(),
  180. subscriberID: uuidv4()
  181. }
  182. this.generateAdditionalAttributes(connectionAttribute, {}, outGoingInfo)
  183. // connectionAttribute.ConnectionID = connectionAttribute.outGoing.ChannelID + (connectionAttribute.inComing.ChannelID ?? 'undefined')
  184. let call = client.HandleMessage({ id: server, message: JSON.stringify(outGoingInfo) })
  185. console.log(`Sending request to ${server} to open response channel...`)
  186. call.on('status', (status: Status) => {
  187. if (status == grpc.status.OK) { // only returns a status when there's error. Otherwise it just waits
  188. console.log(`Message trasmission operation is successful`)
  189. // RPC completed successfully
  190. } if (status == grpc.status.UNAVAILABLE) {
  191. let report: ReportStatus = {
  192. code: ColorCode.YELLOW,
  193. message: `Server doesn't seem to be alive. Error returned.`,
  194. payload: this.messageToBeSendOver ?? `There's no message at the moment...`
  195. }
  196. connectionAttribute.connectionStatus.next(report)
  197. resolve('No connection established. Server is not responding..')
  198. }
  199. });
  200. call.on('data', (data: any) => {
  201. let response: Message = {
  202. id: data.id,
  203. message: JSON.parse(data.message)
  204. }
  205. if (connectionAttribute.inComing.MessageToBeReceived) {
  206. connectionAttribute.inComing.MessageToBeReceived.next(response)
  207. }
  208. // console.log(`Received ${(response.message as MessageLog).appData.msgId}`)
  209. });
  210. call.on('error', (err) => {
  211. resolve('')
  212. });
  213. })
  214. }
  215. // THis is no longer necesarry after the introduction of connection Attribute. But it is still useful for checking for the other side's health
  216. public async checkConnectionHealth(client: any, statusControl: Subject<ReportStatus>, alreadyHealthCheck: boolean): Promise<boolean> {
  217. return new Promise((resolve, reject) => {
  218. client.Check({}, (error, response) => {
  219. if (response) {
  220. console.log(`GRPC Health check status: ${response.status} Server Connected`);
  221. // Intepret the response status and implement code logic or handler
  222. resolve(response.status)
  223. } else {
  224. if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
  225. reject(false)
  226. }
  227. })
  228. })
  229. }
  230. }
  231. // https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md