HTTP 연결 풀링 사용

컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.

HTTP 연결 풀을 사용하여 HTTP 연결을 재사용하는 방법을 보여줍니다.

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플


// Package http provides a set of HTTP Cloud Functions samples.
package http

import (

var urlString = ""

// client is used to make HTTP requests with a 10 second timeout.
// http.Clients should be reused instead of created as needed.
var client = &http.Client{
	Timeout: 10 * time.Second,

// MakeRequest is an example of making an HTTP request. MakeRequest uses a
// single http.Client for all requests to take advantage of connection
// pooling and caching. See
func MakeRequest(w http.ResponseWriter, r *http.Request) {
	resp, err := client.Get(urlString)
	if err != nil {
		http.Error(w, "Error making request", http.StatusInternalServerError)
	if resp.StatusCode != http.StatusOK {
		msg := fmt.Sprintf("Bad StatusCode: %d", resp.StatusCode)
		http.Error(w, msg, http.StatusInternalServerError)
	fmt.Fprintf(w, "ok")


import java.time.Duration;

public class SendHttpRequest implements HttpFunction {

  // Create a client with some reasonable defaults. This client can be reused for multiple requests.
  // ( also pools connections automatically by default.)
  private static HttpClient client =

  public void service(HttpRequest request, HttpResponse response)
      throws IOException, InterruptedException {
    // Create a GET sendHttpRequest to ""
    String url = "";
    var getRequest =;

    // Send the sendHttpRequest using the client
    var getResponse = client.send(getRequest, BodyHandlers.ofString());

    // Write the results to the output:
    var writer = new PrintWriter(response.getWriter());
    writer.printf("Received code '%s' from url '%s'.", getResponse.statusCode(), url);


const fetch = require('node-fetch');

const http = require('http');
const https = require('https');

const httpAgent = new http.Agent({keepAlive: true});
const httpsAgent = new https.Agent({keepAlive: true});

 * HTTP Cloud Function that caches an HTTP agent to pool HTTP connections.
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
exports.connectionPooling = async (req, res) => {
  try {
    // TODO(optional): replace this with your own URL.
    const url = '';

    // Select the appropriate agent to use based on the URL.
    const agent = url.includes('https') ? httpsAgent : httpAgent;

    const {data} = await fetch(url, {agent});
    const text = await data.text();

    res.status(200).send(`Data: ${text}`);
  } catch (err) {
    res.status(500).send(`Error: ${err.message}`);


import requests

# Create a global HTTP session (which provides connection pooling)
session = requests.Session()

def connection_pooling(request):
    HTTP Cloud Function that uses a connection pool to make HTTP requests.
        request (flask.Request): The request object.
        The response text, or any set of values that can be turned into a
        Response object using `make_response`

    # The URL to send the request to
    url = ''

    # Process the request
    response = session.get(url)
    return 'Success!'

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.