| 85 | // 文件系统限速器,防止并发请求过多达到服务器限制 |
| 86 | // 也防止上传/下载带宽占用过多超时导致失败/数据不全的问题 |
| 87 | export default class LimiterFileSystem implements FileSystem { |
| 88 | private fs: FileSystem; |
| 89 | |
| 90 | private limiter: RateLimiter; |
| 91 | |
| 92 | constructor(fs: FileSystem, limiter?: RateLimiter) { |
| 93 | this.fs = fs; |
| 94 | this.limiter = limiter || new RateLimiter(); |
| 95 | } |
| 96 | |
| 97 | verify(): Promise<void> { |
| 98 | return this.limiter.execute(() => this.fs.verify(), "verify"); |
| 99 | } |
| 100 | |
| 101 | async open(file: FileInfo): Promise<FileReader> { |
| 102 | return this.limiter.execute(async () => { |
| 103 | const reader = await this.fs.open(file); |
| 104 | return { |
| 105 | read: (type) => this.limiter.execute(() => reader.read(type), "read"), |
| 106 | }; |
| 107 | }, "open"); |
| 108 | } |
| 109 | |
| 110 | async openDir(path: string): Promise<FileSystem> { |
| 111 | return this.limiter.execute(async () => { |
| 112 | const fs = await this.fs.openDir(path); |
| 113 | return new LimiterFileSystem(fs, this.limiter); |
| 114 | }, "openDir"); |
| 115 | } |
| 116 | |
| 117 | async create(path: string, opts?: FileCreateOptions): Promise<FileWriter> { |
| 118 | return this.limiter.execute(async () => { |
| 119 | const writer = await this.fs.create(path, opts); |
| 120 | return { |
| 121 | write: (content) => this.limiter.execute(() => writer.write(content), "write"), |
| 122 | }; |
| 123 | }, "create"); |
| 124 | } |
| 125 | |
| 126 | createDir(dir: string, opts?: FileCreateOptions): Promise<void> { |
| 127 | return this.limiter.execute(() => this.fs.createDir(dir, opts), "createDir"); |
| 128 | } |
| 129 | |
| 130 | delete(path: string): Promise<void> { |
| 131 | return this.limiter.execute(() => this.fs.delete(path), "delete"); |
| 132 | } |
| 133 | |
| 134 | list(): Promise<FileInfo[]> { |
| 135 | return this.limiter.execute(() => this.fs.list(), "list"); |
| 136 | } |
| 137 | |
| 138 | getDirUrl(): Promise<string> { |
| 139 | return this.limiter.execute(() => this.fs.getDirUrl(), "getDirUrl"); |
| 140 | } |
| 141 | } |
nothing calls this directly
no outgoing calls
no test coverage detected