Skip to content

Commit

Permalink
feat: added swarm-cache header to chunks get handler (#4486)
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill authored Nov 30, 2023
1 parent 65a45c1 commit 22cd4a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,11 @@ paths:
$ref: "SwarmCommon.yaml#/components/schemas/SwarmReference"
required: true
description: Swarm address of chunk
- in: header
schema:
$ref: "SwarmCommon.yaml#/components/parameters/SwarmCache"
name: swarm-cache
required: false
responses:
"200":
description: Retrieved chunk content
Expand Down
14 changes: 13 additions & 1 deletion pkg/api/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (s *Service) chunkGetHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_chunk_by_address").Build()
loggerV1 := logger.V(1).Build()

headers := struct {
Cache *bool `map:"Swarm-Cache"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
return
}
cache := true
if headers.Cache != nil {
cache = *headers.Cache
}

paths := struct {
Address swarm.Address `map:"address,resolve" validate:"required"`
}{}
Expand All @@ -158,7 +170,7 @@ func (s *Service) chunkGetHandler(w http.ResponseWriter, r *http.Request) {
return
}

chunk, err := s.storer.Download(true).Get(r.Context(), paths.Address)
chunk, err := s.storer.Download(cache).Get(r.Context(), paths.Address)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
loggerV1.Debug("chunk not found", "address", paths.Address)
Expand Down

0 comments on commit 22cd4a6

Please sign in to comment.