Skip to content

Commit

Permalink
Set cache control for the dataapi responses (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix authored Apr 19, 2024
1 parent 13cf70c commit 090db84
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions disperser/dataapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dataapi
import (
"context"
"errors"
"fmt"
"math/big"
"net/http"
"os"
Expand Down Expand Up @@ -30,6 +31,21 @@ import (
const (
maxWorkerPoolLimit = 10
maxQueryBatchesLimit = 2

cacheControlParam = "Cache-Control"

// Cache control for responses.
// The time unit is second for max age.
maxOperatorsNonsigningPercentageAge = 10
maxNonSignerAge = 10
maxDeregisteredOperatorAage = 10
maxThroughputAge = 10
maxMetricAage = 10
maxFeedBlobsAge = 10
maxFeedBlobAage = 300 // this is completely static
maxDisperserAvailabilityAge = 3
maxChurnerAvailabilityAge = 3
maxBatcherAvailabilityAge = 3
)

var errNotFound = errors.New("not found")
Expand Down Expand Up @@ -303,6 +319,7 @@ func (s *server) FetchBlobHandler(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchBlob")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxFeedBlobAage))
c.JSON(http.StatusOK, metadata)
}

Expand Down Expand Up @@ -336,6 +353,7 @@ func (s *server) FetchBlobsHandler(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchBlobs")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxFeedBlobsAge))
c.JSON(http.StatusOK, BlobsResponse{
Meta: Meta{
Size: len(metadatas),
Expand Down Expand Up @@ -382,6 +400,7 @@ func (s *server) FetchMetricsHandler(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchMetrics")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxMetricAage))
c.JSON(http.StatusOK, metric)
}

Expand Down Expand Up @@ -422,6 +441,7 @@ func (s *server) FetchMetricsThroughputHandler(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchMetricsTroughput")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxThroughputAge))
c.JSON(http.StatusOK, ths)
}

Expand Down Expand Up @@ -454,6 +474,7 @@ func (s *server) FetchNonSigners(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchNonSigners")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxNonSignerAge))
c.JSON(http.StatusOK, metric)
}

Expand Down Expand Up @@ -501,6 +522,7 @@ func (s *server) FetchOperatorsNonsigningPercentageHandler(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchOperatorsNonsigningPercentageHandler")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxOperatorsNonsigningPercentageAge))
c.JSON(http.StatusOK, metric)
}

Expand Down Expand Up @@ -544,6 +566,7 @@ func (s *server) FetchDeregisteredOperators(c *gin.Context) {
}

s.metrics.IncrementSuccessfulRequestNum("FetchDeregisteredOperators")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxDeregisteredOperatorAage))
c.JSON(http.StatusOK, DeregisteredOperatorsResponse{
Meta: Meta{
Size: len(operatorMetadatas),
Expand Down Expand Up @@ -597,6 +620,7 @@ func (s *server) FetchDisperserServiceAvailability(c *gin.Context) {

}

c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxDisperserAvailabilityAge))
c.JSON(availabilityStatus, ServiceAvailabilityResponse{
Meta: Meta{
Size: len(availabilityStatuses),
Expand Down Expand Up @@ -650,6 +674,7 @@ func (s *server) FetchChurnerServiceAvailability(c *gin.Context) {

}

c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxChurnerAvailabilityAge))
c.JSON(availabilityStatus, ServiceAvailabilityResponse{
Meta: Meta{
Size: len(availabilityStatuses),
Expand Down Expand Up @@ -703,6 +728,7 @@ func (s *server) FetchBatcherAvailability(c *gin.Context) {

}

c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxBatcherAvailabilityAge))
c.JSON(availabilityStatus, ServiceAvailabilityResponse{
Meta: Meta{
Size: len(availabilityStatuses),
Expand Down

0 comments on commit 090db84

Please sign in to comment.