@@ -34,27 +34,46 @@ func (azureBlob *AzureBlob) getBlob(writer http.ResponseWriter, request *http.Re
34
34
},
35
35
)
36
36
if err != nil {
37
- fail (writer , request , http .StatusInternalServerError , "failed to generate cache download URL " ,
37
+ fail (writer , request , http .StatusInternalServerError , "failed to generate cache download URLs " ,
38
38
"key" , key , "err" , err )
39
39
40
40
return
41
41
}
42
42
43
- if len (generateCacheDownloadURLResponse .Urls ) != 1 {
43
+ if len (generateCacheDownloadURLResponse .Urls ) == 0 {
44
44
fail (writer , request , http .StatusInternalServerError , fmt .Sprintf ("failed to generate" +
45
- " cache download URL : expected 1 URL, got %d" , len ( generateCacheDownloadURLResponse . Urls ) ))
45
+ " cache download URLs : expected at least 1 URL, got 0" ))
46
46
47
47
return
48
48
}
49
49
50
50
// Proxy cache entry download
51
- req , err := http .NewRequestWithContext (request .Context (), http .MethodGet ,
52
- generateCacheDownloadURLResponse .Urls [0 ], nil )
51
+ for i , url := range generateCacheDownloadURLResponse .Urls {
52
+ isLastIteration := i == len (generateCacheDownloadURLResponse .Urls )- 1
53
+
54
+ if azureBlob .proxyCacheEntryDownload (writer , request , key , url , isLastIteration ) {
55
+ break
56
+ }
57
+ }
58
+ }
59
+
60
+ func (azureBlob * AzureBlob ) proxyCacheEntryDownload (
61
+ writer http.ResponseWriter ,
62
+ request * http.Request ,
63
+ key string ,
64
+ url string ,
65
+ isLastIteration bool ,
66
+ ) bool {
67
+ req , err := http .NewRequestWithContext (request .Context (), http .MethodGet , url , nil )
53
68
if err != nil {
69
+ if ! isLastIteration {
70
+ return false
71
+ }
72
+
54
73
fail (writer , request , http .StatusInternalServerError , "failed to create request to proxy" +
55
74
" cache entry download" , "key" , key , "err" , err )
56
75
57
- return
76
+ return true
58
77
}
59
78
60
79
// Support HTTP range requests
@@ -67,24 +86,37 @@ func (azureBlob *AzureBlob) getBlob(writer http.ResponseWriter, request *http.Re
67
86
68
87
resp , err := azureBlob .potentiallyCachingHTTPClient .Do (req )
69
88
if err != nil {
89
+ if ! isLastIteration {
90
+ return false
91
+ }
92
+
70
93
fail (writer , request , http .StatusInternalServerError , "failed to perform request to proxy" +
71
94
" cache entry download" , "key" , key , "err" , err )
72
95
73
- return
96
+ return true
74
97
}
98
+ defer resp .Body .Close ()
75
99
76
100
switch resp .StatusCode {
77
101
case http .StatusOK , http .StatusPartialContent :
78
102
// Proceed with proxying
79
103
case http .StatusNotFound :
104
+ if ! isLastIteration {
105
+ return false
106
+ }
107
+
80
108
writer .WriteHeader (http .StatusNotFound )
81
109
82
- return
110
+ return true
83
111
default :
112
+ if ! isLastIteration {
113
+ return false
114
+ }
115
+
84
116
fail (writer , request , http .StatusInternalServerError , fmt .Sprintf ("failed to perform request to proxy" +
85
117
" cache entry download, got unexpected HTTP %d" , resp .StatusCode ), "key" , key )
86
118
87
- return
119
+ return true
88
120
}
89
121
90
122
// Chacha doesn't support Range requests yet, and not disclosing Content-Length
@@ -108,9 +140,11 @@ func (azureBlob *AzureBlob) getBlob(writer http.ResponseWriter, request *http.Re
108
140
})
109
141
bytesRead , err := io .CopyBuffer (writer , progressReader , largeBuffer )
110
142
if err != nil {
111
- proxyingDuration := time .Since (startProxyingAt )
112
143
fail (writer , request , http .StatusInternalServerError , "failed to proxy cache entry download" ,
113
- "err" , err , "duration" , proxyingDuration , "read" , bytesRead , "key" , key )
114
- return
144
+ "err" , err , "duration" , time .Since (startProxyingAt ), "read" , bytesRead , "key" , key )
145
+
146
+ return true
115
147
}
148
+
149
+ return true
116
150
}
0 commit comments