Skip to content

Commit 0ac8c7d

Browse files
committed
update on the docs
1 parent 54ce567 commit 0ac8c7d

File tree

2 files changed

+245
-111
lines changed

2 files changed

+245
-111
lines changed
Lines changed: 122 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Chunk Management
3-
description: Learn how Tusflow handles large file uploads through intelligent chunking
3+
description: Learn how Tusflow optimizes large file uploads through intelligent chunking and parallel processing
44
---
55

66
import { Callout } from 'fumadocs-ui/components/callout'
@@ -9,76 +9,147 @@ import { Tab, Tabs } from 'fumadocs-ui/components/tabs'
99

1010
## Overview
1111

12-
Tusflow implements smart chunk management to handle large file uploads efficiently and reliably.
12+
Tusflow implements advanced chunk management to handle large file uploads efficiently and reliably, leveraging edge computing capabilities and dynamic optimization.
1313

1414
<Callout type="info">
15-
The default chunk size is 5MB, but Tusflow automatically adjusts this based on network conditions and file characteristics.
15+
Tusflow automatically adjusts chunk sizes based on network conditions, file characteristics, and edge worker constraints, with sizes ranging from 5MB to 50MB.
1616
</Callout>
1717

18-
## How It Works
18+
## Key Components
1919

2020
<Steps>
21-
### File Analysis
22-
When a file is selected for upload, Tusflow analyzes its size and type to determine the optimal chunking strategy.
23-
24-
### Chunk Creation
25-
The file is split into manageable chunks, with size determined by:
26-
- Network conditions
27-
- File size
28-
- Memory constraints
29-
- Client capabilities
21+
### Dynamic Chunk Sizing
22+
Optimizes chunk size based on network speed, file size, and edge worker memory constraints.
3023

3124
### Parallel Processing
32-
Multiple chunks are uploaded simultaneously to maximize throughput while maintaining stability.
25+
Uploads multiple chunks simultaneously to maximize throughput while maintaining stability.
3326

34-
### Assembly
35-
Once all chunks are uploaded, they're automatically assembled in the correct order.
36-
</Steps>
27+
### Adaptive Batching
28+
Adjusts the number of concurrent uploads based on network conditions.
3729

30+
### Checksum Verification
31+
Ensures data integrity through MD5, SHA1, SHA256, or SHA512 checksums.
32+
</Steps>
3833

3934
## Features
4035

41-
### Dynamic Sizing
42-
Tusflow automatically adjusts chunk sizes based on:
43-
- Upload speed
44-
- Success rate
45-
- Memory usage
46-
- Network stability
47-
48-
### Parallel Processing
49-
- Multiple chunks upload simultaneously
50-
- Automatic concurrency management
51-
- Progress tracking per chunk
52-
- Bandwidth optimization
53-
54-
### Error Handling
55-
- Automatic retries
36+
### Intelligent Chunk Sizing
37+
- Considers network speed, file size, and edge worker memory limits
38+
- Dynamically adjusts between 5MB and 50MB
39+
- Ensures optimal upload performance and resource utilization
40+
41+
### Parallel Upload Management
42+
- Concurrent chunk uploads (up to 10 simultaneous uploads)
43+
- Adaptive batch sizing based on network conditions
44+
- Progress tracking per chunk with rate-limited updates
45+
46+
### Error Handling and Recovery
47+
- Automatic retries with exponential backoff
5648
- Partial upload recovery
57-
- Checksum verification
58-
- Progress preservation
49+
- Circuit breaker implementation for failure management
5950

60-
## Best Practices
51+
### Performance Optimization
52+
- Exponential moving average for network speed calculation
53+
- Memory-aware chunk processing
54+
- Efficient use of edge worker resources
55+
56+
## How It Works
6157

6258
<Callout type="warning">
63-
While parallel uploads can improve speed, too many concurrent uploads can degrade performance. Start with 3-5 parallel uploads and adjust based on your needs.
59+
Understanding the chunk management process is crucial for optimizing your application's upload performance and reliability.
6460
</Callout>
6561

66-
1. **Chunk Size Selection**
67-
- Consider network conditions
68-
- Balance memory usage
69-
- Account for client capabilities
62+
1. **File Analysis**
63+
- Analyze file size and type
64+
- Calculate initial network speed
65+
66+
2. **Chunk Creation**
67+
- Determine optimal chunk size using `calculateOptimalChunkSize` function
68+
- Consider edge worker memory constraints and network conditions
69+
70+
3. **Parallel Processing**
71+
- Upload chunks in parallel using adaptive batch sizes
72+
- Dynamically adjust concurrency based on network speed
73+
74+
4. **Progress Tracking**
75+
- Update upload progress with rate limiting
76+
- Store metadata in Upstash Redis for resumability
77+
78+
5. **Completion and Verification**
79+
- Verify all parts are uploaded correctly
80+
- Complete multipart upload on S3
81+
82+
## Best Practices
83+
84+
1. **Optimal Chunk Size Selection**
85+
- Let Tusflow handle dynamic sizing
86+
- Monitor and adjust `CHUNK_SIZE` configuration if needed
7087

71-
2. **Error Recovery**
72-
- Implement retry logic
73-
- Handle network failures
74-
- Maintain chunk integrity
88+
2. **Parallel Upload Configuration**
89+
- Start with default `MAX_CONCURRENT` and `BATCH_SIZE` values
90+
- Adjust based on your specific use case and performance requirements
7591

76-
3. **Performance Optimization**
77-
- Use parallel uploads
78-
- Implement caching
79-
- Monitor upload speeds
92+
3. **Error Handling**
93+
- Implement client-side retry logic
94+
- Utilize Tusflow's built-in circuit breaker for failure management
8095

8196
4. **Resource Management**
82-
- Clean up incomplete uploads
83-
- Monitor storage usage
84-
- Handle timeouts properly
97+
- Monitor edge worker memory usage
98+
- Implement proper cleanup for incomplete uploads
99+
100+
## Code Examples
101+
102+
<Tabs items={['Calculate Optimal Chunk Size', 'Upload Chunks', 'Verify Checksum']}>
103+
<Tab value="Calculate Optimal Chunk Size">
104+
```typescript
105+
function calculateOptimalChunkSize(totalSize: number, networkSpeed: number): number {
106+
const { MIN, MAX } = UPLOAD_CONFIG.CHUNK_SIZE;
107+
const memoryBasedMax = Math.floor(WORKER_CONSTRAINTS.CHUNK_MEMORY_LIMIT / WORKER_CONSTRAINTS.NETWORK_OVERHEAD);
108+
const optimalSize = Math.min(networkSpeed * 2, MAX, memoryBasedMax);
109+
const minRequiredSize = Math.ceil(totalSize / UPLOAD_CONFIG.UPLOAD.MAX_PARTS);
110+
return Math.max(Math.min(optimalSize, MAX), MIN, minRequiredSize);
111+
}
112+
```
113+
</Tab>
114+
<Tab value="Upload Chunks">
115+
```typescript
116+
async function uploadChunks(chunk: ArrayBuffer, uploadOffset: number, totalSize: number, cachedMetadata: UploadMetadata, s3Client: S3Client, redis: Redis, bucket: string, uploadId: string) {
117+
// Calculate network speed and optimal chunk size
118+
const optimalChunkSize = calculateOptimalChunkSize(totalSize, cachedMetadata.networkSpeed);
119+
120+
// Process chunks in parallel
121+
const chunks = Math.ceil(chunk.byteLength / optimalChunkSize);
122+
const uploadPromises = [];
123+
124+
for (let i = 0; i < chunks; i++) {
125+
// Upload chunk logic
126+
// ...
127+
uploadPromises.push(uploadPromise);
128+
}
129+
130+
await Promise.all(uploadPromises);
131+
132+
// Update progress
133+
await updateProgress(redis, uploadId, cachedMetadata);
134+
}
135+
```
136+
</Tab>
137+
<Tab value="Verify Checksum">
138+
```typescript
139+
async function verifyChecksum(data: ArrayBuffer, algorithm: string, expectedChecksum: string): Promise<boolean> {
140+
const supportedAlgorithms = ["md5", "sha1", "sha256", "sha512"];
141+
if (!supportedAlgorithms.includes(algorithm.toLowerCase())) {
142+
throw new Error(`Unsupported checksum algorithm: ${algorithm}`);
143+
}
144+
145+
const hash = crypto.createHash(algorithm.toLowerCase());
146+
hash.update(new Uint8Array(data));
147+
const calculatedChecksum = hash.digest("base64");
148+
149+
return calculatedChecksum === expectedChecksum;
150+
}
151+
```
152+
</Tab>
153+
</Tabs>
154+
155+
By leveraging intelligent chunk management, parallel processing, and adaptive optimization, Tusflow provides a powerful and flexible upload solution that can handle large files, network fluctuations, and high concurrency with ease.
Lines changed: 123 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,130 @@
11
---
22
title: Resumable Uploads
3-
description: Resumable uploads allow clients to pause and resume file uploads, making the process more resilient to network issues and providing a better user experience.
3+
description: Learn how Tusflow leverages Upstash Redis, AWS S3 multipart uploads, and the TUS protocol for efficient and reliable resumable file uploads.
44
---
55

6-
## Starting an Upload
6+
import { Callout } from 'fumadocs-ui/components/callout'
7+
import { Steps } from 'fumadocs-ui/components/steps'
8+
import { Tab, Tabs } from 'fumadocs-ui/components/tabs'
79

8-
To initiate a resumable upload:
10+
## Overview
911

10-
```http
11-
POST /files
12-
Host: your-api.example.com
13-
Tus-Resumable: 1.0.0
14-
Upload-Length: 100000000
15-
Content-Type: application/offset+octet-stream
16-
```
12+
Tusflow utilizes a powerful combination of Upstash Redis, AWS S3 multipart uploads, and the TUS protocol to provide a robust and efficient resumable upload solution.
1713

18-
Response:
19-
20-
```http
21-
HTTP/1.1 201 Created
22-
Location: https://your-api.example.com/files/24e533e02ec3bc40c387f1a0e460e216
23-
Tus-Resumable: 1.0.0
24-
```
25-
26-
## Pausing an Upload
27-
28-
The upload can be paused at any time. The client should keep track of the upload URL and the number of bytes uploaded.
29-
30-
## Resuming an Upload
31-
32-
To resume an upload, send a HEAD request to get the current offset:
33-
34-
```http
35-
HEAD /files/24e533e02ec3bc40c387f1a0e460e216
36-
Host: your-api.example.com
37-
Tus-Resumable: 1.0.0
38-
```
39-
40-
Response:
41-
42-
```http
43-
HTTP/1.1 200 OK
44-
Upload-Offset: 50000000
45-
Tus-Resumable: 1.0.0
46-
```
47-
48-
Then continue uploading from the offset:
49-
50-
```http
51-
PATCH /files/24e533e02ec3bc40c387f1a0e460e216
52-
Host: your-api.example.com
53-
Tus-Resumable: 1.0.0
54-
Upload-Offset: 50000000
55-
Content-Type: application/offset+octet-stream
56-
Content-Length: 10000000
57-
58-
[BINARY DATA]
59-
```
60-
61-
## Error Handling
62-
63-
If a connection is lost during upload:
64-
65-
1. Client can retrieve the last successful offset using HEAD request
66-
2. Resume upload from that offset using PATCH request
67-
3. No data needs to be re-uploaded
14+
<Callout type="info">
15+
Resumable uploads allow clients to pause and resume file uploads, making the process more resilient to network issues and providing a better user experience.
16+
</Callout>
17+
18+
## Key Components
19+
20+
<Steps>
21+
### Upstash Redis
22+
A serverless Redis database used for storing upload metadata and managing upload state.
23+
24+
### AWS S3 Multipart Uploads
25+
Enables large file uploads to be split into smaller parts, uploaded independently, and then reassembled.
26+
27+
### TUS Protocol
28+
An open protocol for resumable file uploads, ensuring consistency and interoperability.
29+
</Steps>
30+
31+
## Features
32+
33+
### Upstash Redis Integration
34+
- Serverless and globally distributed
35+
- Low-latency access to upload metadata
36+
- Automatic scaling and high availability
37+
38+
### AWS S3 Multipart Upload
39+
- Support for large file uploads (up to 5TB)
40+
- Parallel upload of file parts
41+
- Ability to pause and resume uploads
42+
43+
### TUS Protocol Implementation
44+
- Standardized resumable upload process
45+
- Cross-platform compatibility
46+
- Built-in error handling and recovery
47+
48+
## How It Works
49+
50+
<Callout type="warning">
51+
Understanding the upload process is crucial for optimizing your application's performance and reliability.
52+
</Callout>
53+
54+
1. **Initiate Upload**
55+
- Client sends a POST request with file metadata
56+
- Server creates an entry in Upstash Redis and initiates S3 multipart upload
57+
58+
2. **Upload Chunks**
59+
- Client sends file chunks using PATCH requests
60+
- Server uploads parts to S3 and updates progress in Upstash Redis
61+
62+
3. **Resume Upload**
63+
- Client can retrieve upload offset using HEAD request
64+
- Server fetches current state from Upstash Redis
65+
66+
4. **Complete Upload**
67+
- After all chunks are uploaded, server completes S3 multipart upload
68+
- Upload metadata is cleaned up from Upstash Redis
69+
70+
## Best Practices
71+
72+
1. **Optimal Chunk Size**
73+
- Balance between network efficiency and memory usage
74+
- Typically between 5MB to 15MB per chunk
75+
76+
2. **Error Handling**
77+
- Implement exponential backoff for retries
78+
- Handle network disconnections gracefully
79+
80+
3. **Progress Tracking**
81+
- Utilize Upstash Redis for real-time progress updates
82+
- Implement client-side progress bars for better UX
83+
84+
## Code Examples
85+
86+
<Tabs items={['Initiate Upload', 'Upload Chunk', 'Resume Upload']}>
87+
<Tab value="Initiate Upload">
88+
```http
89+
POST /files HTTP/1.1
90+
Host: api.tusflow.com
91+
Tus-Resumable: 1.0.0
92+
Upload-Length: 100000000
93+
Upload-Metadata: filename d29ya2Zsb3cuanBn,mimetype aW1hZ2UvanBlZw==
94+
95+
HTTP/1.1 201 Created
96+
Location: https://<your-workers-endpoint>/files/24e533e02ec3bc40c387f1a0e460e216
97+
Tus-Resumable: 1.0.0
98+
```
99+
</Tab>
100+
<Tab value="Upload Chunk">
101+
```http
102+
PATCH /files/24e533e02ec3bc40c387f1a0e460e216 HTTP/1.1
103+
Host: api.tusflow.com
104+
Tus-Resumable: 1.0.0
105+
Upload-Offset: 0
106+
Content-Type: application/offset+octet-stream
107+
Content-Length: 5242880
108+
109+
[BINARY CHUNK DATA]
110+
111+
HTTP/1.1 204 No Content
112+
Tus-Resumable: 1.0.0
113+
Upload-Offset: 5242880
114+
```
115+
</Tab>
116+
<Tab value="Resume Upload">
117+
```http
118+
HEAD /files/24e533e02ec3bc40c387f1a0e460e216 HTTP/1.1
119+
Host: api.tusflow.com
120+
Tus-Resumable: 1.0.0
121+
122+
HTTP/1.1 200 OK
123+
Upload-Offset: 5242880
124+
Upload-Length: 100000000
125+
Tus-Resumable: 1.0.0
126+
```
127+
</Tab>
128+
</Tabs>
129+
130+
By leveraging Upstash Redis, AWS S3 multipart uploads, and the TUS protocol, Tusflow provides a powerful and flexible resumable upload solution that can handle large files, network interruptions, and high concurrency with ease.

0 commit comments

Comments
 (0)