forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout_wrapper.go
40 lines (32 loc) · 1.11 KB
/
timeout_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
)
type ReaderTimeoutWrapper struct {
t time.Duration
DataAvailabilityServiceReader
}
type TimeoutWrapper struct {
ReaderTimeoutWrapper
}
func NewReaderTimeoutWrapper(dataAvailabilityServiceReader DataAvailabilityServiceReader, t time.Duration) DataAvailabilityServiceReader {
return &ReaderTimeoutWrapper{
t: t,
DataAvailabilityServiceReader: dataAvailabilityServiceReader,
}
}
func (w *ReaderTimeoutWrapper) GetByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
deadlineCtx, cancel := context.WithDeadline(ctx, time.Now().Add(w.t))
// For GetByHash we want fast cancellation of all goroutines started by
// the aggregator as soon as one returns.
defer cancel()
return w.DataAvailabilityServiceReader.GetByHash(deadlineCtx, hash)
}
func (w *ReaderTimeoutWrapper) String() string {
return fmt.Sprintf("ReaderTimeoutWrapper{%v}", w.DataAvailabilityServiceReader)
}