-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhancement and add metric (#5)
* enhancement and add metric * add comment * quota calculation
- Loading branch information
1 parent
e4f3997
commit ec7329d
Showing
17 changed files
with
231 additions
and
77 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
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
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
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,63 @@ | ||
package external | ||
|
||
import ( | ||
"context" | ||
"encoding/xml" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type SPClient struct { | ||
hc *http.Client | ||
host string | ||
} | ||
|
||
func NewSPClient(host string) (*SPClient, error) { | ||
transport := &http.Transport{ | ||
DisableCompression: true, | ||
MaxIdleConnsPerHost: 1000, | ||
MaxConnsPerHost: 1000, | ||
IdleConnTimeout: 90 * time.Second, | ||
} | ||
client := &http.Client{ | ||
Timeout: 10 * time.Minute, | ||
Transport: transport, | ||
} | ||
return &SPClient{hc: client, host: host}, nil | ||
} | ||
|
||
func (c *SPClient) GetBucketReadQuota(ctx context.Context, bucketName string) (QuotaInfo, error) { | ||
year, month, _ := time.Now().Date() | ||
var date string | ||
if int(month) < 10 { | ||
date = strconv.Itoa(year) + "-" + "0" + strconv.Itoa(int(month)) | ||
} else { | ||
date = strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) | ||
} | ||
var urlStr string | ||
parts := strings.Split(c.host, "//") | ||
urlStr = parts[0] + "//" + bucketName + "." + parts[1] + "/" | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil) | ||
if err != nil { | ||
return QuotaInfo{}, err | ||
} | ||
// set query parameters | ||
q := req.URL.Query() | ||
q.Add("read-quota", "") | ||
q.Add("year-month", date) | ||
req.URL.RawQuery = q.Encode() | ||
resp, err := c.hc.Do(req) | ||
if err != nil { | ||
return QuotaInfo{}, err | ||
} | ||
defer resp.Body.Close() | ||
QuotaResult := QuotaInfo{} | ||
err = xml.NewDecoder(resp.Body).Decode(&QuotaResult) | ||
if err != nil { | ||
return QuotaInfo{}, err | ||
} | ||
return QuotaResult, nil | ||
} |
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,19 @@ | ||
package external | ||
|
||
import ( | ||
"encoding/xml" | ||
) | ||
|
||
// QuotaInfo indicates the quota info of bucket | ||
type QuotaInfo struct { | ||
XMLName xml.Name `xml:"GetReadQuotaResult"` | ||
Version string `xml:"version,attr"` | ||
BucketName string `xml:"BucketName"` | ||
BucketID string `xml:"BucketID"` // BucketID defines the bucket read quota value on chain | ||
ReadQuotaSize uint64 `xml:"ReadQuotaSize"` // ReadQuotaSize defines the bucket read quota value on chain | ||
SPFreeReadQuotaSize uint64 `xml:"SPFreeReadQuotaSize"` // SPFreeReadQuotaSize defines the free quota of this month | ||
ReadConsumedSize uint64 `xml:"ReadConsumedSize"` // ReadConsumedSize defines the consumed total read quota of this month | ||
FreeConsumedSize uint64 `xml:"FreeConsumedSize"` // FreeConsumedSize defines the consumed free quota | ||
MonthlyFreeQuota uint64 `xml:"MonthlyFreeQuota"` // MonthlyFreeQuota defines the consumed monthly free quota | ||
MonthlyFreeConsumedSize uint64 `xml:"MonthlyQuotaConsumedSize"` // MonthlyFreeConsumedSize defines the consumed monthly free quota | ||
} |
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
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
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,26 @@ | ||
package syncer | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/bnb-chain/blob-hub/logging" | ||
"github.com/bnb-chain/blob-hub/metrics" | ||
) | ||
|
||
func (s *BlobSyncer) monitorQuota() { | ||
if s.spClient == nil { | ||
return | ||
} | ||
monitorTicket := time.NewTicker(MonitorQuotaInterval) | ||
for range monitorTicket.C { | ||
quota, err := s.spClient.GetBucketReadQuota(context.Background(), s.getBucketName()) | ||
if err != nil { | ||
logging.Logger.Errorf("failed to get bucket info from SP, err=%s", err.Error()) | ||
continue | ||
} | ||
remaining := quota.ReadQuotaSize + quota.MonthlyFreeQuota + quota.SPFreeReadQuotaSize - quota.ReadConsumedSize - quota.MonthlyFreeConsumedSize - quota.FreeConsumedSize | ||
metrics.BucketRemainingQuotaGauge.Set(float64(remaining)) | ||
logging.Logger.Infof("remaining quota in bytes is %d", remaining) | ||
} | ||
} |
Oops, something went wrong.