@@ -29,11 +29,12 @@ public abstract partial class Spider :
29
29
{
30
30
private readonly List < IDataFlow > _dataFlows ;
31
31
private readonly List < IRequestSupplier > _requestSuppliers ;
32
- private readonly SpiderOptions _options ;
33
32
private readonly RequestedQueue _requestedQueue ;
34
33
private AsyncMessageConsumer < byte [ ] > _consumer ;
35
34
private readonly SpiderServices _services ;
36
35
36
+ protected SpiderOptions Options { get ; private set ; }
37
+
37
38
/// <summary>
38
39
/// 爬虫标识
39
40
/// </summary>
@@ -53,8 +54,8 @@ ILogger<Spider> logger
53
54
{
54
55
Logger = logger ;
55
56
_services = services ;
56
- _options = options . Value ;
57
- _requestedQueue = new RequestedQueue ( _options ) ;
57
+ Options = options . Value ;
58
+ _requestedQueue = new RequestedQueue ( Options ) ;
58
59
_requestSuppliers = new List < IRequestSupplier > ( ) ;
59
60
_dataFlows = new List < IDataFlow > ( ) ;
60
61
}
@@ -80,15 +81,15 @@ protected virtual (string Id, string Name) GetIdAndName()
80
81
81
82
protected IDataFlow GetDefaultStorage ( )
82
83
{
83
- if ( string . IsNullOrWhiteSpace ( _options . Storage ) )
84
+ if ( string . IsNullOrWhiteSpace ( Options . Storage ) )
84
85
{
85
86
throw new ArgumentNullException ( $ "Storage is not configured") ;
86
87
}
87
88
88
- var type = Type . GetType ( _options . Storage ) ;
89
+ var type = Type . GetType ( Options . Storage ) ;
89
90
if ( type == null )
90
91
{
91
- throw new SpiderException ( $ "Type of storage { _options . Storage } not found") ;
92
+ throw new SpiderException ( $ "Type of storage { Options . Storage } not found") ;
92
93
}
93
94
94
95
if ( ! typeof ( StorageBase ) . IsAssignableFrom ( type ) && ! typeof ( EntityStorageBase ) . IsAssignableFrom ( type ) )
@@ -103,7 +104,7 @@ protected IDataFlow GetDefaultStorage()
103
104
throw new SpiderException ( $ "Storage { type } didn't implement method CreateFromOptions") ;
104
105
}
105
106
106
- var storage = method . Invoke ( null , new object [ ] { _options } ) ;
107
+ var storage = method . Invoke ( null , new object [ ] { Options } ) ;
107
108
if ( storage == null )
108
109
{
109
110
throw new SpiderException ( "Create default storage failed" ) ;
@@ -186,15 +187,15 @@ protected async Task<int> AddRequestsAsync(IEnumerable<Request> requests)
186
187
187
188
// 1. 请求次数超过限制则跳过,并添加失败记录
188
189
// 2. 默认构造的请求次数为 0, 并且不可用户更改,因此可以保证数据安全性
189
- if ( request . RequestedTimes > _options . RetriedTimes )
190
+ if ( request . RequestedTimes > Options . RetriedTimes )
190
191
{
191
192
await _services . StatisticsClient . IncreaseFailureAsync ( Id ) ;
192
193
continue ;
193
194
}
194
195
195
196
// 1. 默认构造的深度为 0, 并且用户不可更改,可以保证数据安全
196
197
// 2. 当深度超过限制则跳过
197
- if ( _options . Depth > 0 && request . Depth > _options . Depth )
198
+ if ( Options . Depth > 0 && request . Depth > Options . Depth )
198
199
{
199
200
continue ;
200
201
}
@@ -295,7 +296,7 @@ private async Task HandleResponseAsync(Request request, Response response, byte[
295
296
try
296
297
{
297
298
using var scope = _services . ServiceProvider . CreateScope ( ) ;
298
- var context = new DataContext ( scope . ServiceProvider , _options , request , response ) ;
299
+ var context = new DataContext ( scope . ServiceProvider , Options , request , response ) ;
299
300
context . AddData ( Consts . ResponseBytes , responseBytes ) ;
300
301
301
302
foreach ( var dataFlow in _dataFlows )
@@ -318,8 +319,8 @@ private async Task HandleResponseAsync(Request request, Response response, byte[
318
319
319
320
private async Task RunAsync ( CancellationToken stoppingToken )
320
321
{
321
- var tuple = ComputeIntervalAndDequeueBatch ( _options . Speed ) ;
322
- var sleepTimeLimit = _options . EmptySleepTime * 1000 ;
322
+ var tuple = ComputeIntervalAndDequeueBatch ( Options . Speed ) ;
323
+ var sleepTimeLimit = Options . EmptySleepTime * 1000 ;
323
324
324
325
await Task . Factory . StartNew ( async ( ) =>
325
326
{
@@ -337,7 +338,7 @@ await Task.Factory.StartNew(async () =>
337
338
await _services . StatisticsClient . PrintAsync ( Id ) ;
338
339
}
339
340
340
- if ( _requestedQueue . Count > _options . RequestedQueueCount )
341
+ if ( _requestedQueue . Count > Options . RequestedQueueCount )
341
342
{
342
343
if ( pausedTime > sleepTimeLimit )
343
344
{
@@ -434,7 +435,7 @@ private async Task<bool> PublishRequestMessagesAsync(params Request[] requests)
434
435
{
435
436
foreach ( var request in requests )
436
437
{
437
- if ( _options . UseProxy )
438
+ if ( Options . UseProxy )
438
439
{
439
440
var proxy = await _services . ProxyPool . GetAsync ( 70 ) ;
440
441
if ( proxy == null )
0 commit comments