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, ConnectionState } 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. }
  49. }
  50. // To be migrated into a service in the immediate future
  51. private async createGrpcInstance(
  52. serverUrl: string,
  53. grpcType: GrpcConnectionType,
  54. connectionAttribute: ConnectionAttribute,
  55. ) {
  56. let statusControl: Subject<ConnectionState> = connectionAttribute.connectionStatus
  57. let consecutiveResolutions = 0;
  58. let lastResolutionTime = Date.now();
  59. let yellowErrorEmission: boolean = false
  60. let redErrorEmission: boolean = false
  61. while (true) {
  62. try {
  63. let recreatePromise = new Promise((resolve) => {
  64. if (grpcType.instanceType == 'server') {
  65. this.createServerStreamingServer(serverUrl, connectionAttribute).then(() => {
  66. resolve('recreate')
  67. })
  68. }
  69. if (grpcType.instanceType == 'client') {
  70. this.createServerStreamingClient(serverUrl, connectionAttribute).then(() => {
  71. resolve('recreate')
  72. })
  73. }
  74. })
  75. await recreatePromise
  76. // If connection resolves (indicating failure), increment the count
  77. consecutiveResolutions++;
  78. // console.log(`Reconnection Attempt: ${consecutiveResolutions}`)
  79. if (redErrorEmission == false) {
  80. redErrorEmission = true
  81. console.error(`Connection failed ${consecutiveResolutions} times. Stopping connection attempts.`);
  82. let error: ConnectionState = {
  83. status: 'BUFFER',
  84. reason: `Server is not responding...`
  85. }
  86. statusControl.next(error)
  87. }
  88. } catch (error) {
  89. // Connection did not resolve, reset the count
  90. consecutiveResolutions = 0;
  91. console.error('Connection attempt failed:', error);
  92. }
  93. // Check for a pause of more than 2 seconds since the last resolution attempt
  94. const currentTime = Date.now();
  95. const timeSinceLastResolution = currentTime - lastResolutionTime;
  96. if (timeSinceLastResolution > 2000) {
  97. consecutiveResolutions = 0;
  98. yellowErrorEmission = false
  99. redErrorEmission = false
  100. }
  101. // Update the last resolution time
  102. lastResolutionTime = currentTime;
  103. await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second before the next attempt
  104. // timeout generate message to trigger this reconnection
  105. }
  106. }
  107. // Create Server Instance to stream all application Outgoing messages
  108. public async createServerStreamingServer(
  109. serverUrl: string,
  110. connectionAttribute: ConnectionAttribute
  111. ): Promise<any> { // '0.0.0.0:3001'
  112. return new Promise((resolve, reject) => {
  113. try {
  114. if (!this.server) {
  115. this.server = new grpc.Server();
  116. }
  117. this.server.addService(message_proto.Message.service, {
  118. HandleMessage: (call) => {
  119. this.callRequestsFromRemote.push(call)
  120. let clientInfo = JSON.parse(call.request.message)
  121. this.generateAdditionalAttributes(connectionAttribute, clientInfo)
  122. console.log(`Initializing stream. Opening Channel... Confirmation from ${call.request.id}`)
  123. if (connectionAttribute.outGoing.MessageToBePublished) {
  124. let subscription: Subscription = connectionAttribute.outGoing.MessageToBePublished.subscribe({
  125. next: (response: Message) => {
  126. // console.log(`Sending from GRPC server: ${(response.message as MessageLog).appData.msgId} `)
  127. this.messageToBeSendOver = response
  128. let message = {
  129. id: response.id,
  130. message: JSON.stringify(response.message)
  131. }
  132. call.write(message)
  133. },
  134. error: err => {
  135. console.error(err)
  136. subscription.unsubscribe()
  137. resolve('')
  138. },
  139. complete: () => {
  140. console.log(`Stream response completed for ${call.request.id}`)
  141. subscription.unsubscribe()
  142. resolve('')
  143. }
  144. })
  145. }
  146. console.log(connectionAttribute)
  147. let report: ConnectionState = {
  148. status: 'DIRECT_PUBLISH'
  149. }
  150. connectionAttribute.connectionStatus.next(report)
  151. },
  152. Check: (_, callback) => {
  153. // for now it is just sending the status message over to tell the client it is alive
  154. // For simplicity, always return "SERVING" as status
  155. callback(null, { status: 'SERVING' });
  156. },
  157. });
  158. // Bind and start the server
  159. this.server.bindAsync(serverUrl, grpc.ServerCredentials.createInsecure(), () => {
  160. console.log(`gRPC server is running on ${serverUrl}`);
  161. this.server.start();
  162. });
  163. }
  164. catch (error) {
  165. resolve(error)
  166. }
  167. })
  168. }
  169. // Send a request over to the other server to open a channel for this server to emit/stream messages over
  170. public async createServerStreamingClient(
  171. server: string,
  172. connectionAttribute: ConnectionAttribute
  173. ): Promise<string> {
  174. return new Promise(async (resolve, reject) => {
  175. const client = new message_proto.Message(server, grpc.credentials.createInsecure());
  176. let outGoingInfo: any = {
  177. channelID: uuidv4(),
  178. publisherID: uuidv4(),
  179. subscriberID: uuidv4()
  180. }
  181. this.generateAdditionalAttributes(connectionAttribute, {}, outGoingInfo)
  182. let call = client.HandleMessage({ id: server, message: JSON.stringify(outGoingInfo) })
  183. console.log(`Sending request to ${server} to open response channel...`)
  184. call.on('status', (status: Status) => {
  185. if (status == grpc.status.OK) { // only returns a status when there's error. Otherwise it just waits
  186. console.log(`Message trasmission operation is successful`)
  187. // RPC completed successfully
  188. } if (status == grpc.status.UNAVAILABLE) {
  189. let report: ConnectionState = {
  190. status: 'BUFFER',
  191. reason: `Server doesn't seem to be alive. Error returned.`,
  192. payload: this.messageToBeSendOver ?? `There's no message at the moment...`
  193. }
  194. connectionAttribute.connectionStatus.next(report)
  195. resolve('No connection established. Server is not responding..')
  196. }
  197. });
  198. call.on('data', (data: any) => {
  199. let response: Message = {
  200. id: data.id,
  201. message: JSON.parse(data.message)
  202. }
  203. if (connectionAttribute.inComing.MessageToBeReceived) {
  204. connectionAttribute.inComing.MessageToBeReceived.next(response)
  205. }
  206. });
  207. call.on('error', (err) => {
  208. console.error(err)
  209. resolve('')
  210. });
  211. })
  212. }
  213. // THis is no longer necesarry after the introduction of connection Attribute. But it is still useful for checking for the other side's health
  214. public async checkConnectionHealth(client: any, statusControl: Subject<ReportStatus>, alreadyHealthCheck: boolean): Promise<boolean> {
  215. return new Promise((resolve, reject) => {
  216. client.Check({}, (error, response) => {
  217. if (response) {
  218. console.log(`GRPC Health check status: ${response.status} Server Connected`);
  219. // Intepret the response status and implement code logic or handler
  220. resolve(response.status)
  221. } else {
  222. if (alreadyHealthCheck == false) console.error(`Health check failed: ${error}`);
  223. reject(false)
  224. }
  225. })
  226. })
  227. }
  228. }
  229. // https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md