github.com/uber/kraken@v0.1.4/lib/torrent/scheduler/dispatch/config.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package dispatch
    15  
    16  import (
    17  	"math"
    18  	"time"
    19  
    20  	"github.com/uber/kraken/lib/torrent/scheduler/dispatch/piecerequest"
    21  	"github.com/uber/kraken/utils/memsize"
    22  	"github.com/uber/kraken/utils/timeutil"
    23  )
    24  
    25  // Config defines the configuration for piece dispatch.
    26  type Config struct {
    27  
    28  	// PieceRequestMinTimeout is the minimum timeout for all piece requests, regardless of
    29  	// size.
    30  	PieceRequestMinTimeout time.Duration `yaml:"piece_request_min_timeout"`
    31  
    32  	// PieceRequestTimeoutPerMb is the duration that will be added to piece request
    33  	// timeouts based on the piece size (in megabytes).
    34  	PieceRequestTimeoutPerMb time.Duration `yaml:"piece_request_timeout_per_mb"`
    35  
    36  	// PieceRequestPolicy is the policy that is used to decide which pieces to request
    37  	// from a peer.
    38  	PieceRequestPolicy string `yaml:"piece_request_policy"`
    39  
    40  	// PipelineLimit limits the total number of requests can be sent to a peer
    41  	// at the same time.
    42  	PipelineLimit int `yaml:"pipeline_limit"`
    43  
    44  	// EndgameThreshold is the number pieces required to complete the torrent
    45  	// before the torrent enters "endgame", where we start overloading piece
    46  	// requests to multiple peers.
    47  	EndgameThreshold int `yaml:"endgame_threshold"`
    48  
    49  	DisableEndgame bool `yaml:"disable_endgame"`
    50  }
    51  
    52  func (c Config) applyDefaults() Config {
    53  	if c.PieceRequestPolicy == "" {
    54  		c.PieceRequestPolicy = piecerequest.DefaultPolicy
    55  	}
    56  	if c.PieceRequestMinTimeout == 0 {
    57  		c.PieceRequestMinTimeout = 4 * time.Second
    58  	}
    59  	if c.PieceRequestTimeoutPerMb == 0 {
    60  		c.PieceRequestTimeoutPerMb = 4 * time.Second
    61  	}
    62  	if c.PipelineLimit == 0 {
    63  		c.PipelineLimit = 3
    64  	}
    65  	if c.EndgameThreshold == 0 {
    66  		c.EndgameThreshold = c.PipelineLimit
    67  	}
    68  	return c
    69  }
    70  
    71  func (c Config) calcPieceRequestTimeout(maxPieceLength int64) time.Duration {
    72  	n := float64(c.PieceRequestTimeoutPerMb) * float64(maxPieceLength) / float64(memsize.MB)
    73  	d := time.Duration(math.Ceil(n))
    74  	return timeutil.MaxDuration(d, c.PieceRequestMinTimeout)
    75  }