mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-31 14:13:09 +02:00
refactor(metrics): optimize memory allocation in period entries
- Replace heap allocation with stack-allocated array in Entries.Get() method. - Also refactor uptime module to use value types instead of pointer types.
This commit is contained in:
@@ -58,10 +58,14 @@ func (e *Entries[T]) Get() []T {
|
||||
if e.count < maxEntries {
|
||||
return e.entries[:e.count]
|
||||
}
|
||||
res := make([]T, maxEntries)
|
||||
copy(res, e.entries[e.index:])
|
||||
copy(res[maxEntries-e.index:], e.entries[:e.index])
|
||||
return res
|
||||
var res [maxEntries]T
|
||||
if e.index >= e.count {
|
||||
copy(res[:], e.entries[:e.count])
|
||||
} else {
|
||||
copy(res[:], e.entries[e.index:])
|
||||
copy(res[e.count-e.index:], e.entries[:e.index])
|
||||
}
|
||||
return res[:]
|
||||
}
|
||||
|
||||
func (e *Entries[T]) MarshalJSON() ([]byte, error) {
|
||||
|
||||
@@ -17,8 +17,8 @@ import (
|
||||
|
||||
type (
|
||||
StatusByAlias struct {
|
||||
Map map[string]*routes.HealthInfo `json:"statuses"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Map map[string]routes.HealthInfo `json:"statuses"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
} // @name RouteStatusesByAlias
|
||||
Status struct {
|
||||
Status types.HealthStatus `json:"status" swaggertype:"string" enums:"healthy,unhealthy,unknown,napping,starting"`
|
||||
@@ -42,8 +42,8 @@ type (
|
||||
|
||||
var Poller = period.NewPoller("uptime", getStatuses, aggregateStatuses)
|
||||
|
||||
func getStatuses(ctx context.Context, _ *StatusByAlias) (*StatusByAlias, error) {
|
||||
return &StatusByAlias{
|
||||
func getStatuses(ctx context.Context, _ StatusByAlias) (StatusByAlias, error) {
|
||||
return StatusByAlias{
|
||||
Map: routes.GetHealthInfo(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
@@ -57,7 +57,7 @@ func (s *Status) MarshalJSON() ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
func aggregateStatuses(entries []*StatusByAlias, query url.Values) (int, Aggregated) {
|
||||
func aggregateStatuses(entries []StatusByAlias, query url.Values) (int, Aggregated) {
|
||||
limit := metricsutils.QueryInt(query, "limit", 0)
|
||||
offset := metricsutils.QueryInt(query, "offset", 0)
|
||||
keyword := query.Get("keyword")
|
||||
|
||||
Reference in New Issue
Block a user