This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: limit the number which download from supernode for one task
Signed-off-by: Starnop <[email protected]>
- Loading branch information
Showing
11 changed files
with
168 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package progress | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/dragonflyoss/Dragonfly/pkg/errortypes" | ||
) | ||
|
||
const ( | ||
// renewInterval is the interval time to check the superload. | ||
renewInterval = 2 * time.Second | ||
|
||
// renewDelayTime if the superload has not been changed after renewDelayTime, it should be renewed. | ||
renewDelayTime = 30 * time.Second | ||
) | ||
|
||
// UpdateSuperLoad returns whether the superLoad will exceed the limit after add the delta. | ||
// If not, the delta will be updated. | ||
func (pm *Manager) UpdateSuperLoad(ctx context.Context, taskID string, delta, limit int32) (updated bool, err error) { | ||
loadState, err := pm.superLoad.getAsSuperLoadState(taskID) | ||
if err != nil { | ||
if !errortypes.IsDataNotFound(err) { | ||
return false, err | ||
} | ||
|
||
// init the value for taskID when not found. | ||
pm.superLoad.add(taskID, newSuperLoadState()) | ||
} | ||
|
||
if loadState.loadValue.Add(delta) > limit && limit > 0 { | ||
loadState.loadValue.Add(-delta) | ||
return false, nil | ||
} | ||
loadState.loadModTime = time.Now() | ||
|
||
return true, nil | ||
} | ||
|
||
func (pm *Manager) startRenewSuperLoad() { | ||
ticker := time.NewTicker(renewInterval) | ||
|
||
for range ticker.C { | ||
pm.renewSuperLoad() | ||
} | ||
} | ||
|
||
func (pm *Manager) renewSuperLoad() { | ||
rangeFunc := func(key, value interface{}) bool { | ||
if v, ok := value.(*superLoadState); ok { | ||
if time.Since(v.loadModTime) > renewDelayTime { | ||
v.loadValue.Set(0) | ||
} | ||
} | ||
return true | ||
} | ||
|
||
pm.superLoad.Range(rangeFunc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters