NestJS gRPC Cant connect to microservice - node.js

When i trying to call some method from service im getting "Error: 14 UNAVAILABLE: No connection established"
Code in some module:
imports: [
ClientsModule.register([
{
name: 'UsersService',
transport: Transport.GRPC,
options: {
url: 'localhost:50051',
package: 'users',
protoPath: 'path/to/proto',
}
}
])
],
Users microservice:
async function bootstrap() {
const app = await NestFactory.createMicroservice(
UsersModule,
{
transport: Transport.GRPC,
options: {
url: 'localhost:50051',
package: 'users',
protoPath: 'path/to/proto',
}
},
)
}
Full error
[Nest] 3644 - 12.06.2022, 22:11:54 ERROR [ExceptionsHandler] 14 UNAVAILABLE: No connection established
Error: 14 UNAVAILABLE: No connection established
at Object.callErrorFromStatus (C:\Users\123\Desktop\projects\syntx\api-gateway\node_modules\#grpc\grpc-js\src\call.ts:81:24)
at Object.onReceiveStatus (C:\Users\123\Desktop\projects\syntx\api-gateway\node_modules\#grpc\grpc-js\src\client.ts:351:36)
at Object.onReceiveStatus (C:\Users\123\Desktop\projects\syntx\api-gateway\node_modules\#grpc\grpc-js\src\client-interceptors.ts:462:34)
at Object.onReceiveStatus (C:\Users\123\Desktop\projects\syntx\api-gateway\node_modules\#grpc\grpc-js\src\client-interceptors.ts:424:48)
at C:\Users\123\Desktop\projects\syntx\api-gateway\node_modules\#grpc\grpc-js\src\call-stream.ts:330:24
at processTicksAndRejections (node:internal/process/task_queues:78:11)

Related

Why my nestj.js microservice not responding?

I made two microservice with nest.js and I connected these services on gateway. There is no problem while I work with localhost but I can't receive a response from host. This is the error:
Error: Connection closed
at ClientTCP.handleClose (/Users/ali/Desktop/topstudy/topstudy-app/node_modules/#nestjs/microservices/client/client-tcp.js:79:25)
at Socket.<anonymous> (/Users/ali/Desktop/topstudy/topstudy-app/node_modules/#nestjs/microservices/client/client-tcp.js:69:55)
at Socket.emit (node:events:532:35)
at TCP.<anonymous> (node:net:687:12)
And this is my port and host values:
{
imports: [ConfigModule],
inject: [ConfigService],
name: 'SCHOOL_SERVICE',
useFactory: (configService: ConfigService) => {
return {
transport: Transport.TCP,
options: {
host: 'url.herokuapp.com',
port: 80,
},
}
},
}
If I switch this credentials with the local ones works as expected. But I'm sure host credentials are not wrong I get connection closed error after 30 seconds.

Connect SQL Server with Next.js

I am having a problem (ConnectionError) while trying to connect Next.js with SQL Server with mssql, I have enabled TCP/IP, and SQL Server Browser is running.
this is my code:
// db.js
import sql from 'mssql'
// connection configs
const config = {
user: 'test',
password: '1000',
server: '.\sqlexpress',
database: 'DATABASE_NAME',
port: 1433,
options: {
instancename: 'SQLEXPRESS',
trustedconnection: true,
trustServerCertificate: true
},
}
export default async function ExcuteQuery(query, options) {
try {
let pool = await sql.connect(config);
let products = await pool.request().query(query);
return products.recordsets;
}
catch (error) {
console.log(error);
}
}
// api/hello.js
import ExcuteQuery from '../../utils/db';
export default async function handler(req, res) {
console.log(await ExcuteQuery('select * from tbl_category'));
res.status(200).json({})
}
this is the error:
ConnectionError: getaddrinfo ENOTFOUND .
at E:\0 - WEB\pos\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1012:9)
at Object.onceWrapper (node:events:628:26)
at Connection.emit (node:events:513:28)
at Connection.emit (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1040:18)
at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:16
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
code: 'EINSTLOOKUP',
originalError: ConnectionError: getaddrinfo ENOTFOUND .
at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:32
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
code: 'EINSTLOOKUP',
isTransient: undefined
}
}
also these are the settings changed:
I tried setting the server to MY_DESKTOP_NAME\SQLEXPRESS but it didn't work, but after disabling and reenabling all the settings it works now, it was kinda like a glitch in the system

AWS Lambda connecting DocumentDb instance got MongoServerSelectionError: getaddrinfo EAI_AGAIN

Got following error logs when trying to connecting lambda to documentdb.
TART RequestId: 0946e4e9-9364-4af2-9802-7ea2d8b946d5 Version: $LATEST
2021-08-18T16:25:11.958Z 0946e4e9-9364-4af2-9802-7ea2d8b946d5 INFO insideHandler
2021-08-18T16:25:42.296Z 0946e4e9-9364-4af2-9802-7ea2d8b946d5 ERROR MongoServerSelectionError: getaddrinfo EAI_AGAIN sample-dev-01-instance1.abcde.us-east-1.docdb.amazonaws.com
at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/sdam/topology.js:325:38)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'sample-dev-01-instance1.abcde.us-east-1.docdb.amazonaws.com:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
the VPC of lambda is the same as documentdb instance. The nodejs code connects the instance successfully when running in local but has the issue in lambda.
const { MongoClient } = require('mongodb');
exports.handler = async(event, context) => {
console.log("insideHandler");
const uri = 'mongodb://username:password#sample-dev-01-instance1.abcde.us-east-1.docdb.amazonaws.com:27017';
const client = new MongoClient(uri, {
useNewUrlParser: true,
ssl: true,
tlsCAFile: `rds-combined-ca-bundle.pem`, //Specify the DocDB; cert
retryWrites: false
});
try {
await client.connect();
console.log("connect successfully");
return {
statusCode: 200,
body: JSON.stringify({ message: 'connect successfully' })
}
}
catch (e) {
console.error(e);
}
};
enter code here
EAI_AGAIN is a DNS lookup timed out error, means it is a network connectivity error or proxy related error. Can you confirm whether the Lambda function is using the same security group as the DocumentDB cluster?

ioredis - handle connection retry and ECONNRESET

I'm trying to handle redis retry connection if there's any network issue. It looks like it's working but I'm not sure because sometimes I get ECONNRESET.
redisOptions: {
sentinels: [
{ host: process.env.redis_server_1, port: process.env.redis_port_1 },
{ host: process.env.redis_server_2, port: process.env.redis_port_2 },
{ host: process.env.redis_server_3, port: process.env.redis_port_3 },
],
name: 'mymaster'
},
redisOptions object is defined in configuration file.
function updateConfigurations(){
console.log(configuration.redisOptions);
configuration.redisOptions.sentinelRetryStrategy = (times) => {
console.log('Trying to connect to Redis ', times);
return Math.min(times * 100, 10000);
}
// configuration.redisOptions.retryStrategy = (times) => Math.min(times * 50, 2000);
}
I'm testing this by disconnecting from VPN. The moment I disconnected from VPN, below error message is getting printed on console.
[ioredis] Unhandled error event: Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
And then below logging for retry connecting
[ioredis] Unhandled error event: Error: All sentinels are unreachable. Retrying from scratch after 400ms. Last error: Connection is closed.
Is this a correct way to handle retry connecting? How should I handle ECONNRESET?

The nodemailer connection is forcibly closed in Heroku after 5 min

I'm using nodemailer library as an SMTP client to send emails outside. I'm also using the option for connection pooling that keeps socket open in the air for 10 min. After the 10 min of inactivity the socket is closing.
smtpClient = nodemailer.createTransport({... pool:true ...});
This is how it looks like when the connection is closing the regular way after the timeout event:
{ component: 'smtp-connection', sid: 'cHH7rJsPKGc' } Timeout
{ component: 'smtp-pool', err: { Error: Timeout
at SMTPConnection._onTimeout (/app/node_modules/nodemailer/lib/smtp-connection/index.js:847:30)
at TLSSocket.SMTPConnection._onSocketTimeout (/app/node_modules/nodemailer/lib/smtp-connection/index.js:199:44)
at TLSSocket.emit (events.js:314:20)
at TLSSocket.EventEmitter.emit (domain.js:483:12)
at TLSSocket.Socket._onTimeout (net.js:484:8)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
code: 'ETIMEDOUT',
command: 'CONN' }, tnx: 'pool', cid: 1 } Pool Error for #%s: %s
{ component: 'smtp-connection', sid: 'cHH7rJsPKGc', tnx: 'smtp' } Closing connection to the server using "%s"
{ component: 'smtp-pool', tnx: 'connection', cid: 1, action: 'closed' } Connection #%s was closed
When running it on Heroku, it doesn't reach the 10 min but closes after the 4-5 min with an error:
{ component: 'smtp-pool', err: { Error: Unexpected Response: 451 4.7.0 Timeout waiting for client input [xxx.prod.protection.outlook.com]
at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:945:34)
at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
at TLSSocket.emit (events.js:314:20)
at TLSSocket.EventEmitter.emit (domain.js:483:12)
at addChunk (_stream_readable.js:298:12)
at readableAddChunk (_stream_readable.js:273:9)
at TLSSocket.Readable.push (_stream_readable.js:214:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
code: 'EPROTOCOL',
response: '451 4.7.0 Timeout waiting for client input [xxx.prod.protection.outlook.com]',
responseCode: 451,
command: 'CONN' }, tnx: 'pool', cid: 2 } Pool Error for #%s: %s
{ component: 'smtp-pool', tnx: 'connection', cid: 2, action: 'closed' } Connection #%s was closed
{ component: 'smtp-connection', sid: 'Wabdmex9scE', tnx: 'network' } Connection closed
Just to remind, the nodemailer uses Node.js tls.connect() to open a socket:
this._socket = tls.connect(opts, () => {
this._socket.setKeepAlive(true);
this._socket.setTimeout(10 * 60 * 1000);
});
Node.js version: "12.15.0"-"12.19.0"
nodemailer version: "^6.1.1"

Resources