mirror of
https://github.com/mayflower/docker-ls.git
synced 2025-11-28 00:01:09 +03:00
Tag listing, fix potential deadlock if the response does not respect the page size.
This commit is contained in:
108
lib/api_paginated_request.go
Normal file
108
lib/api_paginated_request.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type validatable interface {
|
||||
validate() error
|
||||
}
|
||||
|
||||
type paginatedRequestResponse interface {
|
||||
setLastError(error)
|
||||
close()
|
||||
}
|
||||
|
||||
type paginatedRequestContext interface {
|
||||
path() string
|
||||
validateApiResponse(response *http.Response, initialRequest bool) error
|
||||
processPartialResponse(response paginatedRequestResponse, apiResponse interface{})
|
||||
createResponse(api *registryApi) paginatedRequestResponse
|
||||
createJsonResponse() validatable
|
||||
}
|
||||
|
||||
func (r *registryApi) executePaginatedRequest(ctx paginatedRequestContext, url *url.URL, initialRequest bool) (response *http.Response, close bool, err error) {
|
||||
response, err = r.connector.Get(url)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
close = response.Close
|
||||
err = ctx.validateApiResponse(response, initialRequest)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *registryApi) iteratePaginatedRequest(
|
||||
ctx paginatedRequestContext,
|
||||
lastApiResponse *http.Response,
|
||||
response paginatedRequestResponse,
|
||||
) (
|
||||
apiResponse *http.Response,
|
||||
more bool,
|
||||
err error,
|
||||
) {
|
||||
requestUrl, err := r.paginatedRequestEndpointUrl(ctx.path(), lastApiResponse)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
apiResponse, needsClose, err := r.executePaginatedRequest(ctx, requestUrl, lastApiResponse == nil)
|
||||
|
||||
if needsClose {
|
||||
defer apiResponse.Body.Close()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
more = apiResponse.Header.Get("link") != ""
|
||||
|
||||
jsonResponse := ctx.createJsonResponse()
|
||||
decoder := json.NewDecoder(apiResponse.Body)
|
||||
err = decoder.Decode(&jsonResponse)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = jsonResponse.validate()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.processPartialResponse(response, jsonResponse)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *registryApi) paginatedRequest(ctx paginatedRequestContext) (response paginatedRequestResponse) {
|
||||
response = ctx.createResponse(r)
|
||||
|
||||
go func() {
|
||||
var apiResponse *http.Response
|
||||
var err error
|
||||
more := true
|
||||
|
||||
for more {
|
||||
apiResponse, more, err = r.iteratePaginatedRequest(ctx, apiResponse, response)
|
||||
|
||||
if err != nil {
|
||||
response.setLastError(err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
response.close()
|
||||
}()
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user