Handling network timeouts
This topic describes how to handle network timeouts that may occur when you use the OSS SDK.
Symptoms
A network timeout is a common issue when you use the OSS SDK. A ConnectionTimeOut error may occur during an upload, which can negatively affect the user experience.
Troubleshoot the issue
This issue can be difficult to reproduce. The following sections describe possible causes of and solutions for network timeouts that occur when you use the OSS SDK.
- Network environment
Analyze the network path:
Mobile phone/PC --- Carrier network --- OSS server
Your device may be at an edge node of the carrier network. This can lead to a low request success rate. You can use CDN edge nodes for acceleration to reduce the dependency of your device on the carrier network. The accelerated network path is as follows:
Mobile phone/PC -- Nearest CDN edge node -- Carrier network -- OSS server
If the issue persists and a ConnectionTimeOut error still occurs, analyze the network configuration.
- Network configuration
The following code provides a detailed timeout error message:
"ConnectionTimeoutError&errormsg=Failed to upload some parts with error: ConnectionTimeoutError: Connect timeout for 60000ms, PUT https://***.oss-cn-hangzhou.aliyuncs.com/***/***/***.mp4?partNumber=2&uploadId=*** -2 (connected: false, keepalive socket: false)headers: {} part_num: 2The error message indicates the following:
- The connection is dropped because the client does not receive a response from the server within 60 seconds.
- An analysis of CDN logs shows that the timeout occurs because the connection is dropped before a part is uploaded.
- If you upload a large file in a poor network environment, the client may not receive a timely response from the OSS server.
Based on this analysis, you can use the following solutions to resolve this issue:
- Use resumable multipart upload. Set the size of each part to a maximum of 1 MB.
- Add a retransmission mechanism to ensure that a part can be re-uploaded if the initial upload fails.
- Increase the timeout period.
// Sample code for resumable multipart upload in the JavaScript SDK let retryCount = 0; let retryCountMax = 3; ... const uploadFile = function uploadFile(client) { if (!uploadFileClient || Object.keys(uploadFileClient).length === 0) { uploadFileClient = client; } ... console.log(`${file.name} => ${key}`); const options = { progress, partSize: 1000 * 1024, // Set the part size. timeout: 120000, // Set the timeout period. }; if (currentCheckpoint) { options.checkpoint = currentCheckpoint; } return uploadFileClient.multipartUpload(key, file, options).then((res) => { console.log('upload success: %j', res); currentCheckpoint = null; uploadFileClient = null; }).catch((err) => { if (uploadFileClient && uploadFileClient.isCancel()) { console.log('stop-upload!'); } else { console.error(err); //retry if (retryCount < retryCountMax){ retryCount++; console.error("retryCount : " + retryCount); uploadFile(''); } } }); };
Summary
If you access data in OSS through a standard OSS domain name, such as oss-cn-hangzhou.aliyuncs.com, the access request is sent over the carrier network. Because of complex network conditions, such as a poor or unstable network, a ConnectionTimeOut error may occur during an upload. You can try the following methods to resolve this issue:
- Use resumable multipart upload. Set the part size to a value between 100 KB and 1 MB. Note The OSS server does not accept parts that are smaller than 100 KB.
- Add a retransmission mechanism to ensure that a part can be re-uploaded if the initial upload fails. Note This mechanism is enabled by default in the Android and iOS SDKs and does not require additional configuration.
- Increase the timeout period.
- Use the CDN full-site acceleration service to improve the performance of OSS data transfers.