AWS lambda function with runtime python 3.7 does not utilizing connection pool in rds proxy instead it create new connection on every request

本秂侑毒 提交于 2021-02-20 03:42:28

问题


I am using AWS lambda functions for a few months now. so far I have cached the connection in the lambda function memory to reuse. After reading about AWS rds proxy recently, I am trying to utilize the connection pool for the lambda functions(Runtime python3.7). so to test the way rds proxy works with lambda functions, I created two functions with runtime node12 and python3.7, the behaviours was this.

1. node12 function => When I run this function, it pools two connection in the proxy and utilizing them for the future request too.

Expected results: Utilizing the connection in the pool

Actual results: Utilizing the connection in the pool

code

let AWS = require('aws-sdk');
var mysql2 = require('mysql2');

exports.handler = async(event) => {
    let connection;
    const promise = new Promise(function(resolve, reject) {
        let connectionConfig = {
          host: 'rds-proxy-endpoint',
          user: 'xxx',
          database: 'xxx',
          password: 'xxxx'
        };
        connection = mysql2.createConnection(connectionConfig);
        connection.query("select * from user limit 1", function (error, results, fields) {
            if(results){
                let response = {
                    "statusCode": 200,
                    "statusDescription": "200 OK",
                    "isBase64Encoded": false,
                    "headers":{
                        "Content-Type": "text/html"
                    },
                    body: results,
                };
                
                connection.end(function(error, results) {
                      resolve(response);
                });
            }
        });
    });
    return promise;
};

2. python3.7 function => when I run this function, it always creates a new connection in rds and the number of connection in proxy keeps increasing.

Expected results: Utilizing the connection in the pool

Actual results: Always creates a new connection

code

import json
import pymysql

def lambda_handler(event, context):
    connection = pymysql.connect(
        host='rds-proxy-endpoint',
        user='xxxx',
        password='xxx',
        db='xxx',
    )
    with connection.cursor() as cursor:
        query = "SELECT * FROM `user` limit 1"
        cursor.execute(query)
        results = cursor.fetchone()
        connection.commit()
        connection.close()
        return {
            'statusCode': 200,
            'body': json.dumps(results)
        }

can anyone explain why the python function always creating a new connection instead of using the existing connections in rds proxy?

来源:https://stackoverflow.com/questions/64458884/aws-lambda-function-with-runtime-python-3-7-does-not-utilizing-connection-pool-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!