-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve /health, refactor error handling
fixes #1
- Loading branch information
1 parent
86af520
commit 52deb6e
Showing
9 changed files
with
304 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package controllers | ||
|
||
import ( | ||
"blob-service/services" | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type HealthController struct { | ||
blobsService *services.BlobsService | ||
} | ||
|
||
func NewHealthController(blobsService *services.BlobsService) *HealthController { | ||
return &HealthController{blobsService: blobsService} | ||
} | ||
|
||
type HealthResponse struct { | ||
Status string `json:"status"` | ||
Detail string `json:"detail,omitempty"` | ||
Head uint64 `json:"head,omitempty"` | ||
} | ||
|
||
// Health | ||
// @Summary Returns health status of this API. | ||
// @Tags health | ||
// @Produce json | ||
// @Success 200 {object} HealthResponse | ||
// @Failure 500 {object} response.ApiErrorResponse | ||
// @Router /health [get] | ||
func (hc *HealthController) Health(c *gin.Context) { | ||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second) | ||
defer cancel() | ||
|
||
response := &HealthResponse{Status: "ok"} | ||
slotNum, err := hc.blobsService.GetSlotNumber(ctx, "head") | ||
if err != nil { | ||
response.Status = "error" | ||
response.Detail = err.Error() | ||
} else { | ||
response.Head = slotNum | ||
} | ||
|
||
c.JSON(http.StatusOK, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package internal | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/eosnationftw/eosn-base-api/helper" | ||
"github.com/eosnationftw/eosn-base-api/log" | ||
"github.com/eosnationftw/eosn-base-api/response" | ||
"github.com/friendsofgo/errors" | ||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
ErrSinkTimeout = errors.New("timeout when trying to reach the sink service") | ||
ErrSlotNotFound = errors.New("slot not found") | ||
ErrInvalidSlot = errors.New("invalid slot") | ||
ErrInvalidIndex = errors.New("invalid index") | ||
) | ||
|
||
const ( | ||
NOT_FOUND_SLOT = "slot_not_found" | ||
INVALID_SLOT = "invalid_slot" | ||
INVALID_INDEX = "invalid_index" | ||
SINK_TIMEOUT = "sink_timeout" | ||
) | ||
|
||
func WriteErrorResponse(c *gin.Context, err error) { | ||
|
||
// 404 NOT FOUND | ||
if errors.Is(err, ErrSlotNotFound) { | ||
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorNotFound(NOT_FOUND_SLOT), err) | ||
return | ||
} | ||
|
||
// 400 BAD REQUEST | ||
if errors.Is(err, ErrInvalidSlot) { | ||
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorBadRequest(INVALID_SLOT), err) | ||
return | ||
} | ||
if errors.Is(err, ErrInvalidIndex) { | ||
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorBadRequest(INVALID_INDEX), err) | ||
return | ||
} | ||
|
||
// 504 GATEWAY TIMEOUT | ||
if errors.Is(err, ErrSinkTimeout) { | ||
helper.ReportPublicErrorAndAbort(c, response.NewApiError(http.StatusGatewayTimeout, SINK_TIMEOUT), err) | ||
return | ||
} | ||
|
||
log.Error("unknown error received, writing internal_server_error instead", zap.Error(err)) | ||
helper.ReportPrivateErrorAndAbort(c, response.InternalServerError, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package services | ||
|
||
import ( | ||
"blob-service/internal" | ||
pbbl "blob-service/pb/pinax/ethereum/blobs/v1" | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"strconv" | ||
|
||
pbkv "github.com/streamingfast/substreams-sink-kv/pb/substreams/sink/kv/v1" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
type BlobsService struct { | ||
sinkClient pbkv.KvClient | ||
} | ||
|
||
func NewBlobsService(sinkClient pbkv.KvClient) *BlobsService { | ||
return &BlobsService{sinkClient: sinkClient} | ||
} | ||
|
||
func (bc *BlobsService) GetSlotNumber(ctx context.Context, block_id string) (uint64, error) { | ||
if block_id == "head" { | ||
resp, err := bc.sinkClient.Get(ctx, &pbkv.GetRequest{Key: "head"}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return binary.BigEndian.Uint64(resp.GetValue()), nil | ||
} | ||
|
||
if len(block_id) > 2 && block_id[:2] == "0x" { | ||
resp, err := bc.sinkClient.Get(ctx, &pbkv.GetRequest{Key: "block_root:" + block_id}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return binary.BigEndian.Uint64(resp.GetValue()), nil | ||
} | ||
|
||
slot, err := strconv.ParseUint(block_id, 10, 64) | ||
if err != nil { | ||
return 0, internal.ErrInvalidSlot | ||
} | ||
|
||
return slot, nil | ||
} | ||
|
||
func (bs *BlobsService) GetSlotByBlockId(ctx context.Context, blockId string) (*pbbl.Slot, error) { | ||
|
||
slotNum, err := bs.GetSlotNumber(ctx, blockId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := bs.sinkClient.Get(ctx, &pbkv.GetRequest{Key: fmt.Sprintf("slot:%d", slotNum)}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
slot := &pbbl.Slot{} | ||
err = proto.Unmarshal(resp.GetValue(), slot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return slot, nil | ||
} |
Oops, something went wrong.