mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
feat(json): improve JSON performance with bytedance/sonic
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package period
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
type Entries[T any] struct {
|
||||
@@ -64,7 +65,7 @@ func (e *Entries[T]) Get() []T {
|
||||
}
|
||||
|
||||
func (e *Entries[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
return sonic.Marshal(map[string]any{
|
||||
"entries": e.Get(),
|
||||
"interval": e.interval,
|
||||
})
|
||||
@@ -75,7 +76,7 @@ func (e *Entries[T]) UnmarshalJSON(data []byte) error {
|
||||
Entries []T `json:"entries"`
|
||||
Interval time.Duration `json:"interval"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &v); err != nil {
|
||||
if err := sonic.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(v.Entries) == 0 {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/internal/utils/atomic"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
@@ -73,7 +74,7 @@ func (p *Poller[T, AggregateT]) load() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(entries, &p.period); err != nil {
|
||||
if err := sonic.Unmarshal(entries, &p.period); err != nil {
|
||||
return err
|
||||
}
|
||||
// Validate and fix intervals after loading to ensure data integrity.
|
||||
@@ -83,7 +84,7 @@ func (p *Poller[T, AggregateT]) load() error {
|
||||
|
||||
func (p *Poller[T, AggregateT]) save() error {
|
||||
initDataDirOnce.Do(initDataDir)
|
||||
entries, err := json.Marshal(p.period)
|
||||
entries, err := sonic.Marshal(p.period)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package uptime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
config "github.com/yusing/godoxy/internal/config/types"
|
||||
"github.com/yusing/godoxy/internal/metrics/period"
|
||||
@@ -50,7 +50,7 @@ func getStatuses(ctx context.Context, _ *StatusByAlias) (*StatusByAlias, error)
|
||||
}
|
||||
|
||||
func (s *Status) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
return sonic.Marshal(map[string]any{
|
||||
"status": s.Status.String(),
|
||||
"latency": s.Latency,
|
||||
"timestamp": s.Timestamp,
|
||||
@@ -163,5 +163,5 @@ func (rs RouteStatuses) aggregate(limit int, offset int) Aggregated {
|
||||
}
|
||||
|
||||
func (result Aggregated) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal([]RouteAggregate(result))
|
||||
return sonic.Marshal([]RouteAggregate(result))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user