Skip to content

Commit

Permalink
pkg/linksharing/sharing: make getLocations fail fast if mapper is nil
Browse files Browse the repository at this point in the history
Change-Id: I860b5dd8b5128082efe54e9b0bd290e978fb8f62
  • Loading branch information
amwolff committed Jan 10, 2023
1 parent 49f6ece commit 37230a8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions pkg/linksharing/sharing/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@ type location struct {
func (handler *Handler) getLocations(ctx context.Context, pr *parsedRequest) (locs []location, pieceCount int64, err error) {
defer mon.Task()(&ctx)(&err)

// we explicitly don't want locations to be nil, so it doesn't render as
// null when we plop it into the output javascript.
locations := make([]location, 0)

if handler.mapper == nil { // fast path
return locations, 0, nil
}

ipSummary, err := object.GetObjectIPSummary(ctx, *handler.uplink, pr.access, pr.bucket, pr.realKey)
if err != nil {
return nil, 0, errdata.WithAction(err, "get locations")
}

// we explicitly don't want locations to be nil, so it doesn't
// render as null when we plop it into the output javascript.
locations := make([]location, 0, len(ipSummary.IPPorts))
if handler.mapper != nil {
for _, ip := range ipSummary.IPPorts {
info, err := handler.mapper.GetIPInfos(ctx, string(ip))
if err != nil {
handler.log.Error("failed to get IP info", zap.Error(err))
continue
}

locations = append(locations, location{
Latitude: info.Location.Latitude,
Longitude: info.Location.Longitude,
})
for _, ip := range ipSummary.IPPorts {
info, err := handler.mapper.GetIPInfos(ctx, string(ip))
if err != nil {
handler.log.Error("failed to get IP info", zap.Error(err))
continue
}

locations = append(locations, location{
Latitude: info.Location.Latitude,
Longitude: info.Location.Longitude,
})
}

return locations, ipSummary.PieceCount, nil
Expand Down

0 comments on commit 37230a8

Please sign in to comment.