Skip to content

Commit 4607435

Browse files
authored
feat: add option to cancel idle peer task (#3810)
Signed-off-by: Jim Ma <[email protected]>
1 parent 8ccd368 commit 4607435

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

client/config/peerhost.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ type DownloadOption struct {
278278
Concurrent *ConcurrentOption `mapstructure:"concurrent" yaml:"concurrent"`
279279
SyncPieceViaHTTPS bool `mapstructure:"syncPieceViaHTTPS" yaml:"syncPieceViaHTTPS"`
280280
SplitRunningTasks bool `mapstructure:"splitRunningTasks" yaml:"splitRunningTasks"`
281+
CancelIdlePeerTask bool `mapstructure:"cancelIdlePeerTask" yaml:"cancelIdlePeerTask"`
281282
// resource clients option
282283
ResourceClients ResourceClientsOption `mapstructure:"resourceClients" yaml:"resourceClients"`
283284

client/daemon/daemon.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,15 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
255255

256256
peerTaskManagerOption := &peer.TaskManagerOption{
257257
TaskOption: peer.TaskOption{
258-
PeerHost: host,
259-
SchedulerOption: opt.Scheduler,
260-
PieceManager: pieceManager,
261-
StorageManager: storageManager,
262-
WatchdogTimeout: opt.Download.WatchdogTimeout,
263-
CalculateDigest: opt.Download.CalculateDigest,
264-
GRPCCredentials: rpc.NewInsecureCredentials(),
265-
GRPCDialTimeout: opt.Download.GRPCDialTimeout,
258+
PeerHost: host,
259+
SchedulerOption: opt.Scheduler,
260+
PieceManager: pieceManager,
261+
StorageManager: storageManager,
262+
WatchdogTimeout: opt.Download.WatchdogTimeout,
263+
CalculateDigest: opt.Download.CalculateDigest,
264+
GRPCCredentials: rpc.NewInsecureCredentials(),
265+
GRPCDialTimeout: opt.Download.GRPCDialTimeout,
266+
CancelIdlePeerTask: opt.Download.CancelIdlePeerTask,
266267
},
267268
SchedulerClient: schedulerClient,
268269
PerPeerRateLimit: opt.Download.PerPeerRateLimit.Limit,

client/daemon/peer/peertask_conductor.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ type TaskOption struct {
173173
GRPCCredentials credentials.TransportCredentials
174174
GRPCDialTimeout time.Duration
175175
// WatchdogTimeout > 0 indicates to start watch dog for every single peer task
176-
WatchdogTimeout time.Duration
176+
WatchdogTimeout time.Duration
177+
CancelIdlePeerTask bool
177178
}
178179

179180
func (ptm *peerTaskManager) newPeerTaskConductor(
@@ -183,9 +184,12 @@ func (ptm *peerTaskManager) newPeerTaskConductor(
183184
parent *peerTaskConductor,
184185
rg *nethttp.Range,
185186
seed bool) *peerTaskConductor {
186-
// use a new context with span info
187-
ctx = trace.ContextWithSpan(context.Background(), trace.SpanFromContext(ctx))
188-
ctx, span := tracer.Start(ctx, config.SpanPeerTask, trace.WithSpanKind(trace.SpanKindClient))
187+
var span trace.Span
188+
if !ptm.TaskOption.CancelIdlePeerTask {
189+
// use a new context to avoid cancel idle peer task
190+
ctx = trace.ContextWithSpan(context.Background(), trace.SpanFromContext(ctx))
191+
}
192+
ctx, span = tracer.Start(ctx, config.SpanPeerTask, trace.WithSpanKind(trace.SpanKindClient))
189193
span.SetAttributes(config.AttributePeerHost.String(ptm.PeerHost.Id))
190194
span.SetAttributes(semconv.NetHostIPKey.String(ptm.PeerHost.Ip))
191195
span.SetAttributes(config.AttributePeerID.String(request.PeerId))

0 commit comments

Comments
 (0)