MeWrite Docs

Network: Connection timed out

ネットワーク接続がタイムアウトした場合のエラー

概要

ネットワーク接続を確立しようとしたが、指定された時間内にサーバーからの応答がなかった場合に発生するエラーです。

エラーメッセージ

Error: connect ETIMEDOUT 192.168.1.1:443

または

curl: (28) Connection timed out after 30000 milliseconds

原因

  1. サーバーダウン: 接続先サーバーが停止している
  2. ファイアウォール: ポートがブロックされている
  3. ネットワーク遅延: ネットワークが非常に遅い
  4. IPアドレス誤り: 接続先のIPが間違っている

解決策

1. 接続テスト

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ping で疎通確認
ping -c 4 192.168.1.1

# telnet でポート確認
telnet 192.168.1.1 443

# nc (netcat) でポート確認
nc -zv 192.168.1.1 443

# curl でタイムアウト設定
curl --connect-timeout 10 --max-time 30 https://example.com

2. Node.js でのタイムアウト設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const axios = require('axios');

// タイムアウト設定
const response = await axios.get('https://api.example.com', {
  timeout: 5000  // 5秒
});

// fetch の場合
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);

try {
  const response = await fetch('https://api.example.com', {
    signal: controller.signal
  });
} catch (error) {
  if (error.name === 'AbortError') {
    console.log('Request timed out');
  }
} finally {
  clearTimeout(timeoutId);
}

3. Python でのタイムアウト設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import requests

# タイムアウト設定
try:
    response = requests.get('https://api.example.com', timeout=5)
except requests.Timeout:
    print('Request timed out')

# 接続タイムアウトと読み取りタイムアウトを分ける
response = requests.get('https://api.example.com', timeout=(3, 10))
# (接続: 3秒, 読み取り: 10秒)

4. ファイアウォールの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# iptables のルール確認(Linux)
sudo iptables -L -n

# UFW の状態確認(Ubuntu)
sudo ufw status

# Windows Firewall の確認
netsh advfirewall show allprofiles

# セキュリティグループの確認(AWS)
aws ec2 describe-security-groups --group-ids sg-xxxxxxxx

5. リトライ戦略

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const axios = require('axios');
const axiosRetry = require('axios-retry');

// 自動リトライ設定
axiosRetry(axios, {
  retries: 3,
  retryDelay: (retryCount) => {
    return retryCount * 1000;  // 1秒、2秒、3秒
  },
  retryCondition: (error) => {
    return error.code === 'ETIMEDOUT' || error.code === 'ECONNABORTED';
  }
});

6. コネクションプーリング

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const https = require('https');
const axios = require('axios');

const agent = new https.Agent({
  keepAlive: true,
  maxSockets: 50,
  timeout: 60000
});

const client = axios.create({
  httpsAgent: agent,
  timeout: 10000
});

7. ヘルスチェック

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
async function healthCheck(url) {
  const start = Date.now();
  try {
    await axios.get(url, { timeout: 5000 });
    return {
      status: 'healthy',
      latency: Date.now() - start
    };
  } catch (error) {
    return {
      status: 'unhealthy',
      error: error.code || error.message
    };
  }
}

// 定期的にチェック
setInterval(() => {
  healthCheck('https://api.example.com/health')
    .then(console.log);
}, 30000);

8. DNS キャッシュ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const { Resolver } = require('dns');
const resolver = new Resolver();
resolver.setServers(['8.8.8.8']);

// DNSルックアップをキャッシュ
const dnsCache = new Map();

async function getCachedIP(hostname) {
  if (dnsCache.has(hostname)) {
    return dnsCache.get(hostname);
  }

  return new Promise((resolve, reject) => {
    resolver.resolve4(hostname, (err, addresses) => {
      if (err) return reject(err);
      dnsCache.set(hostname, addresses[0]);
      setTimeout(() => dnsCache.delete(hostname), 300000);  // 5分キャッシュ
      resolve(addresses[0]);
    });
  });
}

9. プロキシ設定

1
2
3
4
5
6
7
8
9
const HttpsProxyAgent = require('https-proxy-agent');
const axios = require('axios');

const agent = new HttpsProxyAgent('http://proxy.example.com:8080');

const response = await axios.get('https://api.example.com', {
  httpsAgent: agent,
  timeout: 10000
});

10. ネットワーク診断

1
2
3
4
5
6
7
8
# MTR で詳細なルート分析
mtr api.example.com

# TCPダンプでパケットキャプチャ
sudo tcpdump -i eth0 host api.example.com

# netstat で接続状態確認
netstat -an | grep ESTABLISHED

よくある間違い

  • タイムアウト値が短すぎる(モバイル回線を考慮していない)
  • コネクションプールの枯渇
  • DNSルックアップ時間を考慮していない
  • ロードバランサーのヘルスチェック設定

Network の他のエラー

最終更新: 2025-12-09