-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
89 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
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,63 @@ | ||
package autopilot | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
rhpv2 "go.sia.tech/core/rhp/v2" | ||
rhpv3 "go.sia.tech/core/rhp/v3" | ||
"go.sia.tech/core/types" | ||
"go.sia.tech/renterd/api" | ||
"go.sia.tech/renterd/hostdb" | ||
"go.sia.tech/renterd/object" | ||
"lukechampine.com/frand" | ||
) | ||
|
||
type Worker interface { | ||
Account(ctx context.Context, hostKey types.PublicKey) (rhpv3.Account, error) | ||
Contracts(ctx context.Context, hostTimeout time.Duration) (api.ContractsResponse, error) | ||
ID(ctx context.Context) (string, error) | ||
MigrateSlab(ctx context.Context, s object.Slab, set string) (api.MigrateSlabResponse, error) | ||
|
||
RHPBroadcast(ctx context.Context, fcid types.FileContractID) (err error) | ||
RHPForm(ctx context.Context, endHeight uint64, hk types.PublicKey, hostIP string, renterAddress types.Address, renterFunds types.Currency, hostCollateral types.Currency) (rhpv2.ContractRevision, []types.Transaction, error) | ||
RHPFund(ctx context.Context, contractID types.FileContractID, hostKey types.PublicKey, hostIP, siamuxAddr string, balance types.Currency) (err error) | ||
RHPPriceTable(ctx context.Context, hostKey types.PublicKey, siamuxAddr string, timeout time.Duration) (hostdb.HostPriceTable, error) | ||
RHPPruneContract(ctx context.Context, fcid types.FileContractID, timeout time.Duration) (prunable, pruned, remaining uint64, err error) | ||
RHPRenew(ctx context.Context, fcid types.FileContractID, endHeight uint64, hk types.PublicKey, hostIP string, hostAddress, renterAddress types.Address, renterFunds, newCollateral types.Currency, windowSize uint64) (rhpv2.ContractRevision, []types.Transaction, error) | ||
RHPScan(ctx context.Context, hostKey types.PublicKey, hostIP string, timeout time.Duration) (api.RHPScanResponse, error) | ||
RHPSync(ctx context.Context, contractID types.FileContractID, hostKey types.PublicKey, hostIP, siamuxAddr string) (err error) | ||
} | ||
|
||
// workerPool contains all workers known to the autopilot. Users can call | ||
// withWorker to execute a function with a worker of the pool or withWorkers to | ||
// sequentially run a function on all workers. Due to the RWMutex this will | ||
// never block during normal operations. However, during an update of the | ||
// workerpool, this allows us to guarantee that all workers have finished their | ||
// tasks by calling acquiring an exclusive lock on the pool before updating it. | ||
// That way the caller who updated the pool can rely on the autopilot not using | ||
// a worker that was removed during the update after the update operation | ||
// returns. | ||
type workerPool struct { | ||
mu sync.RWMutex | ||
workers []Worker | ||
} | ||
|
||
func newWorkerPool(workers []Worker) *workerPool { | ||
return &workerPool{ | ||
workers: workers, | ||
} | ||
} | ||
|
||
func (wp *workerPool) withWorker(workerFunc func(Worker)) { | ||
wp.mu.RLock() | ||
defer wp.mu.RUnlock() | ||
workerFunc(wp.workers[frand.Intn(len(wp.workers))]) | ||
} | ||
|
||
func (wp *workerPool) withWorkers(workerFunc func([]Worker)) { | ||
wp.mu.RLock() | ||
defer wp.mu.RUnlock() | ||
workerFunc(wp.workers) | ||
} |