forked from ipfs-inactive/bifrost-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockstore.go
99 lines (81 loc) · 2.68 KB
/
blockstore.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/filecoin-saturn/caboose"
blockstore "github.com/ipfs/boxo/blockstore"
exchange "github.com/ipfs/boxo/exchange"
"github.com/ipfs/boxo/gateway"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.uber.org/zap/zapcore"
)
var errNotImplemented = errors.New("not implemented")
const GetBlockTimeout = time.Second * 60
func newExchange(bs blockstore.Blockstore) (exchange.Interface, error) {
return &exchangeBsWrapper{bstore: bs}, nil
}
type exchangeBsWrapper struct {
bstore blockstore.Blockstore
}
func (e *exchangeBsWrapper) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
ctx, cancel := context.WithTimeout(ctx, GetBlockTimeout)
defer cancel()
if goLog.Level().Enabled(zapcore.DebugLevel) {
goLog.Debugw("block requested from remote blockstore", "cid", c.String())
}
blk, err := e.bstore.Get(ctx, c)
if err != nil {
return nil, gatewayError(err)
}
return blk, nil
}
func (e *exchangeBsWrapper) GetBlocks(ctx context.Context, cids []cid.Cid) (<-chan blocks.Block, error) {
out := make(chan blocks.Block)
go func() {
defer close(out)
for _, c := range cids {
blk, err := e.GetBlock(ctx, c)
if err != nil {
return
}
out <- blk
}
}()
return out, nil
}
func (e *exchangeBsWrapper) NotifyNewBlocks(ctx context.Context, blks ...blocks.Block) error {
return nil
}
func (e *exchangeBsWrapper) Close() error {
return nil
}
// gatewayError translates underlying blockstore error into one that gateway code will return as HTTP 502 or 504
// it also makes sure Retry-After hint from remote blockstore will be passed to HTTP client, if present.
func gatewayError(err error) error {
if errors.Is(err, &gateway.ErrorResponse{}) ||
errors.Is(err, &gateway.ErrorRetryAfter{}) {
// already correct error
return err
}
// All timeouts should produce 504 Gateway Timeout
if errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, caboose.ErrSaturnTimeout) ||
// Unfortunately this is not an exported type so we have to check for the content.
strings.Contains(err.Error(), "Client.Timeout exceeded") {
return fmt.Errorf("%w: %s", gateway.ErrGatewayTimeout, err.Error())
}
// (Saturn) errors that support the RetryAfter interface need to be converted
// to the correct gateway error, such that the HTTP header is set.
for v := err; v != nil; v = errors.Unwrap(v) {
if r, ok := v.(interface{ RetryAfter() time.Duration }); ok {
return gateway.NewErrorRetryAfter(err, r.RetryAfter())
}
}
// everything else returns 502 Bad Gateway
return fmt.Errorf("%w: %s", gateway.ErrBadGateway, err.Error())
}
var _ exchange.Interface = (*exchangeBsWrapper)(nil)