grpc.service.method.ts 13 KB

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