QNConfiguration.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // QNConfiguration.h
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 15/5/21.
  6. // Copyright (c) 2015年 Qiniu. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "QNRecorderDelegate.h"
  10. #import "QNDns.h"
  11. /**
  12. * 断点上传时的分块大小
  13. */
  14. extern const UInt32 kQNBlockSize;
  15. /**
  16. * DNS默认缓存时间
  17. */
  18. extern const UInt32 kQNDefaultDnsCacheTime;
  19. /**
  20. * 转换为用户需要的url
  21. *
  22. * @param url 上传url
  23. *
  24. * @return 根据上传url算出代理url
  25. */
  26. typedef NSString * (^QNUrlConvert)(NSString *url);
  27. typedef NS_ENUM(NSInteger, QNResumeUploadVersion){
  28. QNResumeUploadVersionV1, // 分片v1
  29. QNResumeUploadVersionV2 // 分片v2
  30. };
  31. @class QNConfigurationBuilder;
  32. @class QNZone;
  33. @class QNReportConfig;
  34. /**
  35. * Builder block
  36. *
  37. * @param builder builder实例
  38. */
  39. typedef void (^QNConfigurationBuilderBlock)(QNConfigurationBuilder *builder);
  40. @interface QNConfiguration : NSObject
  41. /**
  42. * 存储区域
  43. */
  44. @property (copy, nonatomic, readonly) QNZone *zone;
  45. /**
  46. * 断点上传时的分片大小
  47. */
  48. @property (readonly) UInt32 chunkSize;
  49. /**
  50. * 如果大于此值就使用断点上传,否则使用form上传
  51. */
  52. @property (readonly) UInt32 putThreshold;
  53. /**
  54. * 上传失败时每个上传域名的重试次数,默认重试1次
  55. */
  56. @property (readonly) UInt32 retryMax;
  57. /**
  58. * 重试前等待时长,默认0.5s
  59. */
  60. @property (readonly) NSTimeInterval retryInterval;
  61. /**
  62. * 单个请求超时时间 单位 秒
  63. * 注:每个文件上传肯能存在多个操作,当每个操作失败时,可能存在多个请求重试。
  64. */
  65. @property (readonly) UInt32 timeoutInterval;
  66. /**
  67. * 是否使用 https,默认为 YES
  68. */
  69. @property (nonatomic, assign, readonly) BOOL useHttps;
  70. /**
  71. * 单个文件是否开启并发分片上传,默认为NO
  72. * 单个文件大小大于4M时,会采用分片上传,每个分片会已单独的请求进行上传操作,多个上传操作可以使用并发,
  73. * 也可以采用串行,采用并发时,可以设置并发的个数(对concurrentTaskCount进行设置)。
  74. */
  75. @property (nonatomic, assign, readonly) BOOL useConcurrentResumeUpload;
  76. /**
  77. * 分片上传版本
  78. */
  79. @property (nonatomic, assign, readonly) QNResumeUploadVersion resumeUploadVersion;
  80. /**
  81. * 并发分片上传的并发任务个数,在concurrentResumeUpload为YES时有效,默认为3个
  82. */
  83. @property (nonatomic, assign, readonly) UInt32 concurrentTaskCount;
  84. /**
  85. * 重试时是否允许使用备用上传域名,默认为YES
  86. */
  87. @property (nonatomic, assign) BOOL allowBackupHost;
  88. /**
  89. * 持久化记录接口,可以实现将记录持久化到文件,数据库等
  90. */
  91. @property (nonatomic, readonly) id<QNRecorderDelegate> recorder;
  92. /**
  93. * 为持久化上传记录,根据上传的key以及文件名 生成持久化的记录key
  94. */
  95. @property (nonatomic, readonly) QNRecorderKeyGenerator recorderKeyGen;
  96. /**
  97. * 上传请求代理配置信息
  98. */
  99. @property (nonatomic, readonly) NSDictionary *proxy;
  100. /**
  101. * 上传URL转换,使url转换为用户需要的url
  102. */
  103. @property (nonatomic, readonly) QNUrlConvert converter;
  104. /**
  105. * 默认配置
  106. */
  107. + (instancetype)defaultConfiguration;
  108. /**
  109. * 使用 QNConfigurationBuilder 进行配置
  110. * @param block 配置block
  111. */
  112. + (instancetype)build:(QNConfigurationBuilderBlock)block;
  113. @end
  114. #define kQNGlobalConfiguration [QNGlobalConfiguration shared]
  115. @interface QNGlobalConfiguration : NSObject
  116. /**
  117. * 是否开启dns预解析 默认开启
  118. */
  119. @property(nonatomic, assign)BOOL isDnsOpen;
  120. /**
  121. * dns 预取失败后 会进行重新预取 dnsRepreHostNum为最多尝试次数
  122. */
  123. @property(nonatomic, assign)UInt32 dnsRepreHostNum;
  124. /**
  125. * dns 预取超时,单位:秒 默认:2
  126. */
  127. @property(nonatomic, assign)int dnsResolveTimeout;
  128. /**
  129. * dns 预取, ip 默认有效时间 单位:秒 默认:120
  130. * 只有在 dns 预取未返回 ttl 时使用
  131. */
  132. @property(nonatomic, assign)UInt32 dnsCacheTime;
  133. /**
  134. * dns预取缓存最大有效时间 单位:秒 默认 1800
  135. * 当 dns 缓存 ip 过期并未刷新时,只要在 dnsCacheMaxTTL 时间内仍有效。
  136. */
  137. @property(nonatomic, assign)UInt32 dnsCacheMaxTTL;
  138. /**
  139. * 自定义DNS解析客户端host
  140. */
  141. @property(nonatomic, strong) id <QNDnsDelegate> dns;
  142. /**
  143. * dns解析结果本地缓存路径
  144. */
  145. @property(nonatomic, copy, readonly)NSString *dnsCacheDir;
  146. /**
  147. * 是否使用 udp 方式进行 Dns 预取,默认开启
  148. */
  149. @property(nonatomic, assign)BOOL udpDnsEnable;
  150. /**
  151. * 使用 udp 进行 Dns 预取时的 server ipv4 数组;当对某个 Host 使用 udp 进行 Dns 预取时,会使用 udpDnsIps 进行并发预取
  152. * 当 udpDnsEnable 开启时,使用 udp 进行 Dns 预取方式才会生效
  153. * 默认:@[@"223.5.5.5", @"114.114.114.114", @"1.1.1.1", @"208.67.222.222"]
  154. */
  155. @property(nonatomic, copy) NSArray <NSString *> *udpDnsIpv4Servers;
  156. /**
  157. * 使用 udp 进行 Dns 预取时的 server ipv6 数组;当对某个 Host 使用 udp 进行 Dns 预取时,会使用 udpDnsIps 进行并发预取
  158. * 当 udpDnsEnable 开启时,使用 udp 进行 Dns 预取方式才会生效
  159. * 默认:nil
  160. */
  161. @property(nonatomic, copy) NSArray <NSString *> *udpDnsIpv6Servers;
  162. /**
  163. * 是否使用 doh 预取,默认开启
  164. */
  165. @property(nonatomic, assign)BOOL dohEnable;
  166. /**
  167. * 使用 doh 预取时的 server 数组;当对某个 Host 使用 Doh 预取时,会使用 dohServers 进行并发预取
  168. * 当 dohEnable 开启时,doh 预取才会生效
  169. * 默认:@[@"https://223.6.6.6/dns-query", @"https://8.8.8.8/dns-query"];
  170. * 注意:如果使用 ip,需保证服务证书与 IP 绑定,避免 sni 问题
  171. */
  172. @property(nonatomic, copy) NSArray <NSString *> *dohIpv4Servers;
  173. /**
  174. * 使用 doh 预取时的 server 数组;当对某个 Host 使用 Doh 预取时,会使用 dohServers 进行并发预取
  175. * 当 dohEnable 开启时,doh 预取才会生效
  176. * 默认:nil
  177. * 注意:如果使用 ip,需保证服务证书与 IP 绑定,避免 sni 问题
  178. */
  179. @property(nonatomic, copy) NSArray <NSString *> *dohIpv6Servers;
  180. /**
  181. * Host全局冻结时间 单位:秒 默认:10 推荐范围:[5 ~ 30]
  182. * 当某个Host的上传失败后并且可能短时间无法恢复,会冻结该Host
  183. */
  184. @property(nonatomic, assign)UInt32 globalHostFrozenTime;
  185. /**
  186. * Host局部冻结时间,只会影响当前上传操作 单位:秒 默认:5*60 推荐范围:[60 ~ 10*60]
  187. * 当某个Host的上传失败后并且短时间可能会恢复,会局部冻结该Host
  188. */
  189. @property(nonatomic, assign)UInt32 partialHostFrozenTime;
  190. /**
  191. * 网络连接状态检测使用的connectCheckURLStrings,网络链接状态检测可能会影响重试机制,启动网络连接状态检测有助于提高上传可用性。
  192. * 当请求的 Response 为网络异常时,并发对 connectCheckURLStrings 中 URLString 进行 HEAD 请求,以此检测当前网络状态的链接状态,其中任意一个 URLString 链接成功则认为当前网络状态链接良好;
  193. * 当 connectCheckURLStrings 为 nil 或者 空数组时则弃用检测功能。
  194. */
  195. @property(nonatomic, copy)NSArray <NSString *> *connectCheckURLStrings;
  196. /**
  197. * 是否开启网络连接状态检测,默认:开启
  198. */
  199. @property(nonatomic, assign)BOOL connectCheckEnable;
  200. /**
  201. * 网络连接状态检测HEAD请求超时,默认:2s
  202. */
  203. @property(nonatomic, assign)NSTimeInterval connectCheckTimeout;
  204. + (instancetype)shared;
  205. @end
  206. @interface QNConfigurationBuilder : NSObject
  207. /**
  208. * 默认上传服务器地址
  209. */
  210. @property (nonatomic, strong) QNZone *zone;
  211. /**
  212. * 断点上传时的分片大小
  213. * 分片 v1 最小为 1024,即 1K,建议用户配置 >= 512K
  214. * 分片 v2 最小为 1024 * 1024,即 1M
  215. */
  216. @property (assign) UInt32 chunkSize;
  217. /**
  218. * 如果大于此值就使用断点上传,否则使用form上传
  219. */
  220. @property (assign) UInt32 putThreshold;
  221. /**
  222. * 上传失败时每个上传域名的重试次数,默认重试1次
  223. */
  224. @property (assign) UInt32 retryMax;
  225. /**
  226. * 重试前等待时长,默认0.5s
  227. */
  228. @property (assign) NSTimeInterval retryInterval;
  229. /**
  230. * 超时时间 单位 秒
  231. */
  232. @property (assign) UInt32 timeoutInterval;
  233. /**
  234. * 是否使用 https,默认为 YES
  235. */
  236. @property (nonatomic, assign) BOOL useHttps;
  237. /**
  238. * 重试时是否允许使用备用上传域名,默认为YES
  239. */
  240. @property (nonatomic, assign) BOOL allowBackupHost;
  241. /**
  242. * 是否开启并发分片上传,默认为NO
  243. */
  244. @property (nonatomic, assign) BOOL useConcurrentResumeUpload;
  245. /**
  246. * 分片上传版本
  247. */
  248. @property (nonatomic, assign) QNResumeUploadVersion resumeUploadVersion;
  249. /**
  250. * 并发分片上传的并发任务个数,在concurrentResumeUpload为YES时有效,默认为3个
  251. */
  252. @property (nonatomic, assign) UInt32 concurrentTaskCount;
  253. /**
  254. * 持久化记录接口,可以实现将记录持久化到文件,数据库等
  255. */
  256. @property (nonatomic, strong) id<QNRecorderDelegate> recorder;
  257. /**
  258. * 为持久化上传记录,根据上传的key以及文件名 生成持久化的记录key
  259. */
  260. @property (nonatomic, strong) QNRecorderKeyGenerator recorderKeyGen;
  261. /**
  262. * 上传请求代理配置信息
  263. */
  264. @property (nonatomic, strong) NSDictionary *proxy;
  265. /**
  266. * 上传URL转换,使url转换为用户需要的url
  267. */
  268. @property (nonatomic, strong) QNUrlConvert converter;
  269. @end