Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
package handler

import (
"context"
"net/http"
"os"
"runtime/pprof"

"0chain.net/blobbercore/config"
"0chain.net/blobbercore/constants"
"0chain.net/blobbercore/datastore"
"0chain.net/blobbercore/stats"
"0chain.net/core/common"
"context"
"net/http"
"os"
"runtime/pprof"
"time"

. "0chain.net/core/logging"
"go.uber.org/zap"
Expand Down Expand Up @@ -202,6 +202,7 @@ func CommitHandler(ctx context.Context, r *http.Request) (interface{}, error) {
}

func ReferencePathHandler(ctx context.Context, r *http.Request) (interface{}, error) {
ctx, _ = context.WithTimeout(ctx, time.Second*10)
ctx = setupHandlerContext(ctx, r)

response, err := storageHandler.GetReferencePath(ctx, r)
Expand Down
42 changes: 34 additions & 8 deletions code/go/0chain.net/blobbercore/handler/storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,40 +459,63 @@ func (fsh *StorageHandler) ListEntities(ctx context.Context, r *http.Request) (*
}

func (fsh *StorageHandler) GetReferencePath(ctx context.Context, r *http.Request) (*ReferencePathResult, error) {
resCh := make(chan *ReferencePathResult)
errCh := make(chan error)
go fsh.getReferencePath(ctx, r, resCh, errCh)

for {
select {
case <-ctx.Done():
return nil, common.NewError("timeout", "timeout reached")
case result := <-resCh:
return result, nil
case err := <-errCh:
return nil, err
}
}
}

func (fsh *StorageHandler) getReferencePath(ctx context.Context, r *http.Request, resCh chan<- *ReferencePathResult, errCh chan<- error) {
if r.Method == "POST" {
return nil, common.NewError("invalid_method", "Invalid method used. Use GET instead")
errCh <- common.NewError("invalid_method", "Invalid method used. Use GET instead")
return
}
allocationTx := ctx.Value(constants.ALLOCATION_CONTEXT_KEY).(string)
allocationObj, err := fsh.verifyAllocation(ctx, allocationTx, false)

if err != nil {
return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error())
errCh <- common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error())
return
}
allocationID := allocationObj.ID

clientID := ctx.Value(constants.CLIENT_CONTEXT_KEY).(string)
if len(clientID) == 0 {
return nil, common.NewError("invalid_operation", "Please pass clientID in the header")
errCh <- common.NewError("invalid_operation", "Please pass clientID in the header")
return
}

var paths []string
pathsString := r.FormValue("paths")
if len(pathsString) == 0 {
path := r.FormValue("path")
if len(path) == 0 {
return nil, common.NewError("invalid_parameters", "Invalid path")
errCh <- common.NewError("invalid_parameters", "Invalid path")
return
}
paths = append(paths, path)
} else {
err = json.Unmarshal([]byte(pathsString), &paths)
if err != nil {
return nil, common.NewError("invalid_parameters", "Invalid path array json")
errCh <- common.NewError("invalid_parameters", "Invalid path array json")
return
}
}

rootRef, err := reference.GetReferencePathFromPaths(ctx, allocationID, paths)
if err != nil {
return nil, err
errCh <- err
return
}

refPath := &ReferencePath{ref: rootRef}
Expand All @@ -518,15 +541,18 @@ func (fsh *StorageHandler) GetReferencePath(ctx context.Context, r *http.Request
} else {
latestWM, err = writemarker.GetWriteMarkerEntity(ctx, allocationObj.AllocationRoot)
if err != nil {
return nil, common.NewError("latest_write_marker_read_error", "Error reading the latest write marker for allocation."+err.Error())
errCh <- common.NewError("latest_write_marker_read_error", "Error reading the latest write marker for allocation."+err.Error())
return
}
}
var refPathResult ReferencePathResult
refPathResult.ReferencePath = refPath
if latestWM != nil {
refPathResult.LatestWM = &latestWM.WM
}
return &refPathResult, nil

resCh <- &refPathResult
return
}

func (fsh *StorageHandler) GetObjectPath(ctx context.Context, r *http.Request) (*ObjectPathResult, error) {
Expand Down