从 AWS Lambda 连接到 MongoDB Atlas 花费的时间太长

Connecting to MongoDB Atlas from AWS Lambda takes too long

冷启动大约需要。 10 秒,这对我来说是无法接受的。如果 lambda 重新使用连接,持续时间将减少到 30 毫秒。有什么方法可以改善初始连接时间吗?

最耗时的部分(6秒)

2020-08-04T20:39:45.004+02:00 信息:com.mongodb.client.internal.MongoClientDelegate 没有从集群描述中选择服务器 ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{, type=UNKNOWN, state=CONNECTING}, ServerDescription{, type=UNKNOWN, state=CONNECTING},ServerDescription{,type=UNKNOWN,state=CONNECTING}]}。超时前等待 30000 毫秒

2020-08-04T20:39:51.302+02:00 2020 年 8 月 4 日 6:39:51 下午 com.mongodb.diagnostics.logging.JULLogger 日志

import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDBConnectionLambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    // declare MongoClient as an instance variable to ensure connection pooling
    private MongoClient mongoClient = null;

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {

        MongoDatabase database = getDBConnection(context);

        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        Document doc = database.getCollection(System.getenv("MONGO_DB_COLLECTION"))
                               .find()
                               .first();

        response.setBody(doc == null ? null : doc.toJson());

        response.setStatusCode(200);
        return response;
    }

    private MongoDatabase getDBConnection(Context context) {

        if (mongoClient == null) {

            context.getLogger().log("Initializing new MongoDB connection");
            mongoClient = MongoClients.create(System.getenv("MONGO_DB_URI"));
            return mongoClient.getDatabase(System.getenv("MONGO_DB_NAME"));
        }

        context.getLogger().log("Reusing existing MongoDB connection");
        return mongoClient.getDatabase(System.getenv("MONGO_DB_NAME"));
    }

}

我通过在处理程序方法之外触发连接,设法将初始连接时间缩短了 2 秒。剩余时间可能是语言选择的结果。

import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

import org.bson.Document;

public class MongoDBConnectionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    // declare MongoClient as an instance variable to ensure connection pooling
    private final MongoClient mongoClient = MongoClients.create(System.getenv("MONGO_DB_URI"));

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {

        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        Document doc = mongoClient.getDatabase(System.getenv("MONGO_DB_NAME"))
                                  .getCollection(System.getenv("MONGO_DB_COLLECTION"))
                                  .find()
                                  .first();

        response.setBody(doc == null ? null : doc.toJson());

        response.setStatusCode(200);
        return response;
    }

}