/
utilities.go
146 lines (118 loc) · 3.09 KB
/
utilities.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package namesys
import (
"context"
"fmt"
"strings"
"github.com/gocnpan/boxo/path"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
type resolver interface {
resolveOnceAsync(context.Context, path.Path, ResolveOptions) <-chan AsyncResult
}
// resolve is a helper for implementing Resolver.ResolveN using resolveOnce.
func resolve(ctx context.Context, r resolver, p path.Path, options ResolveOptions) (result Result, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
err = ErrResolveFailed
resCh := resolveAsync(ctx, r, p, options)
for res := range resCh {
result.Path, result.TTL, result.LastMod, err = res.Path, res.TTL, res.LastMod, res.Err
if err != nil {
break
}
}
return result, err
}
func resolveAsync(ctx context.Context, r resolver, p path.Path, options ResolveOptions) <-chan AsyncResult {
ctx, span := startSpan(ctx, "ResolveAsync")
defer span.End()
resCh := r.resolveOnceAsync(ctx, p, options)
depth := options.Depth
outCh := make(chan AsyncResult, 1)
go func() {
defer close(outCh)
ctx, span := startSpan(ctx, "ResolveAsync.Worker")
defer span.End()
var subCh <-chan AsyncResult
var cancelSub context.CancelFunc
defer func() {
if cancelSub != nil {
cancelSub()
}
}()
for {
select {
case res, ok := <-resCh:
if !ok {
resCh = nil
break
}
if res.Err != nil {
emitResult(ctx, outCh, res)
return
}
log.Debugf("resolved %s to %s", p.String(), res.Path.String())
if !res.Path.Mutable() {
emitResult(ctx, outCh, res)
break
}
if depth == 1 {
res.Err = ErrResolveRecursion
emitResult(ctx, outCh, res)
break
}
subOpts := options
if subOpts.Depth > 1 {
subOpts.Depth--
}
var subCtx context.Context
if cancelSub != nil {
// Cancel previous recursive resolve since it won't be used anyways
cancelSub()
}
subCtx, cancelSub = context.WithCancel(ctx)
_ = cancelSub
subCh = resolveAsync(subCtx, r, res.Path, subOpts)
case res, ok := <-subCh:
if !ok {
subCh = nil
break
}
// We don't bother returning here in case of context timeout as there is
// no good reason to do that, and we may still be able to emit a result
emitResult(ctx, outCh, res)
case <-ctx.Done():
return
}
if resCh == nil && subCh == nil {
return
}
}
}()
return outCh
}
func emitResult(ctx context.Context, outCh chan<- AsyncResult, r AsyncResult) {
select {
case outCh <- r:
case <-ctx.Done():
}
}
func joinPaths(resolvedBase, unresolvedPath path.Path) (path.Path, error) {
if resolvedBase == nil {
return nil, nil
}
segments := unresolvedPath.Segments()[2:]
if strings.HasSuffix(unresolvedPath.String(), "/") {
segments = append(segments, "")
}
// simple optimization
if len(segments) == 0 {
return resolvedBase, nil
}
return path.Join(resolvedBase, segments...)
}
var tracer = otel.Tracer("boxo/namesys")
func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return tracer.Start(ctx, fmt.Sprintf("Namesys.%s", name))
}