-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into pj/usable-hosts
- Loading branch information
Showing
19 changed files
with
226 additions
and
229 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,30 @@ | ||
package host | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"time" | ||
|
||
rhpv2 "go.sia.tech/core/rhp/v2" | ||
"go.sia.tech/core/types" | ||
"go.sia.tech/renterd/api" | ||
) | ||
|
||
type ( | ||
Host interface { | ||
PublicKey() types.PublicKey | ||
|
||
DownloadSector(ctx context.Context, w io.Writer, root types.Hash256, offset, length uint32, overpay bool) error | ||
UploadSector(ctx context.Context, sectorRoot types.Hash256, sector *[rhpv2.SectorSize]byte, rev types.FileContractRevision) error | ||
|
||
PriceTable(ctx context.Context, rev *types.FileContractRevision) (api.HostPriceTable, types.Currency, error) | ||
FetchRevision(ctx context.Context, fetchTimeout time.Duration) (types.FileContractRevision, error) | ||
|
||
FundAccount(ctx context.Context, balance types.Currency, rev *types.FileContractRevision) error | ||
SyncAccount(ctx context.Context, rev *types.FileContractRevision) error | ||
} | ||
|
||
HostManager interface { | ||
Host(hk types.PublicKey, fcid types.FileContractID, siamuxAddr string) Host | ||
} | ||
) |
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,100 @@ | ||
package locking | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.sia.tech/core/types" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ContractLocker interface { | ||
AcquireContract(ctx context.Context, fcid types.FileContractID, priority int, d time.Duration) (lockID uint64, err error) | ||
KeepaliveContract(ctx context.Context, fcid types.FileContractID, lockID uint64, d time.Duration) (err error) | ||
ReleaseContract(ctx context.Context, fcid types.FileContractID, lockID uint64) (err error) | ||
} | ||
|
||
type ContractLock struct { | ||
lockID uint64 | ||
fcid types.FileContractID | ||
d time.Duration | ||
locker ContractLocker | ||
logger *zap.SugaredLogger | ||
|
||
keepaliveLoopCtx context.Context | ||
keepaliveLoopCtxCancel context.CancelFunc | ||
stopWG sync.WaitGroup | ||
} | ||
|
||
func (cl *ContractLock) keepaliveLoop() { | ||
// Create ticker for 20% of the lock duration. | ||
start := time.Now() | ||
var lastUpdate time.Time | ||
tickDuration := cl.d / 5 | ||
t := time.NewTicker(tickDuration) | ||
|
||
// Cleanup | ||
defer func() { | ||
t.Stop() | ||
select { | ||
case <-t.C: | ||
default: | ||
} | ||
}() | ||
|
||
// Loop until stopped. | ||
for { | ||
select { | ||
case <-cl.keepaliveLoopCtx.Done(): | ||
return // released | ||
case <-t.C: | ||
} | ||
if err := cl.locker.KeepaliveContract(cl.keepaliveLoopCtx, cl.fcid, cl.lockID, cl.d); err != nil && !errors.Is(err, context.Canceled) { | ||
cl.logger.Errorw(fmt.Sprintf("failed to send keepalive: %v", err), | ||
"contract", cl.fcid, | ||
"lockID", cl.lockID, | ||
"loopStart", start, | ||
"timeSinceLastUpdate", time.Since(lastUpdate), | ||
"tickDuration", tickDuration) | ||
return | ||
} | ||
lastUpdate = time.Now() | ||
} | ||
} | ||
|
||
func (cl *ContractLock) Release(ctx context.Context) error { | ||
// Stop background loop. | ||
cl.keepaliveLoopCtxCancel() | ||
cl.stopWG.Wait() | ||
|
||
// Release the contract. | ||
return cl.locker.ReleaseContract(ctx, cl.fcid, cl.lockID) | ||
} | ||
|
||
func NewContractLock(ctx context.Context, fcid types.FileContractID, priority int, locker ContractLocker, logger *zap.SugaredLogger) (*ContractLock, error) { | ||
duration := 30 * time.Second | ||
lockID, err := locker.AcquireContract(ctx, fcid, priority, duration) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cl := &ContractLock{ | ||
lockID: lockID, | ||
fcid: fcid, | ||
d: duration, | ||
locker: locker, | ||
logger: logger, | ||
|
||
keepaliveLoopCtx: ctx, | ||
keepaliveLoopCtxCancel: cancel, | ||
} | ||
cl.stopWG.Add(1) | ||
go func() { | ||
cl.keepaliveLoop() | ||
cl.stopWG.Done() | ||
}() | ||
return cl, nil | ||
} |
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
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
Oops, something went wrong.