feat: custom json marshaling implementation, replace json and yaml library (#89)

* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic

* fix: yaml unmarshal panic

* feat: custom json marshaler implementation

* chore: fix import and err marshal handling

---------

Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
Yuzerion
2025-04-16 15:02:11 +08:00
committed by GitHub
parent 57292f0fe8
commit 80bc018a7f
65 changed files with 1749 additions and 205 deletions

View File

@@ -0,0 +1,22 @@
package uptime
import (
"fmt"
"github.com/yusing/go-proxy/internal/watcher/health"
)
type Status struct {
Status health.Status
Latency int64
Timestamp int64
}
type RouteStatuses map[string][]*Status
func (s *Status) MarshalJSONTo(buf []byte) []byte {
return fmt.Appendf(buf,
`{"status":"%s","latency":"%d","timestamp":"%d"}`,
s.Status, s.Latency, s.Timestamp,
)
}

View File

@@ -2,7 +2,6 @@ package uptime
import (
"context"
"encoding/json"
"net/url"
"sort"
"time"
@@ -13,20 +12,15 @@ import (
"github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/route/routes/routequery"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/pkg/json"
)
type (
StatusByAlias struct {
Map map[string]*routequery.HealthInfoRaw `json:"statuses"`
Timestamp int64 `json:"timestamp"`
Map json.Map[*routequery.HealthInfoRaw] `json:"statuses"`
Timestamp int64 `json:"timestamp"`
}
Status struct {
Status health.Status `json:"status"`
Latency int64 `json:"latency"`
Timestamp int64 `json:"timestamp"`
}
RouteStatuses map[string][]*Status
Aggregated []map[string]any
Aggregated = json.MapSlice[any]
)
var Poller = period.NewPoller("uptime", getStatuses, aggregateStatuses)
@@ -124,7 +118,3 @@ func (rs RouteStatuses) aggregate(limit int, offset int) Aggregated {
}
return result
}
func (result Aggregated) MarshalJSON() ([]byte, error) {
return json.Marshal([]map[string]any(result))
}

View File

@@ -0,0 +1,10 @@
package uptime
import (
"net/url"
"testing"
)
func TestPoller(t *testing.T) {
Poller.Test(t, url.Values{"limit": []string{"1"}})
}