chore(ur): move structs to reduce dependency chain

lib/ur brings in a lot of dependencies we don't need in e.g.
stcrashreceiver, who only needs the small failure reporting structs.
Make those part of the lean `contract` package instead.

Signed-off-by: Jakob Borg <jakob@kastelo.net>
This commit is contained in:
Jakob Borg
2026-05-23 09:13:47 +02:00
parent 4404b4dfb4
commit 9152d7fb2f
4 changed files with 32 additions and 31 deletions
+3 -3
View File
@@ -30,7 +30,7 @@ import (
raven "github.com/getsentry/raven-go" raven "github.com/getsentry/raven-go"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/ur" "github.com/syncthing/syncthing/lib/ur/contract"
) )
const maxRequestSize = 1 << 20 // 1 MiB const maxRequestSize = 1 << 20 // 1 MiB
@@ -131,7 +131,7 @@ func handleFailureFn(dsn, failureDir string, ignore *ignorePatterns) func(w http
return return
} }
var reports []ur.FailureReport var reports []contract.FailureReport
err = json.Unmarshal(bs, &reports) err = json.Unmarshal(bs, &reports)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
@@ -179,7 +179,7 @@ func handleFailureFn(dsn, failureDir string, ignore *ignorePatterns) func(w http
} }
} }
func saveFailureWithGoroutines(data ur.FailureData, failureDir string) (string, error) { func saveFailureWithGoroutines(data contract.FailureData, failureDir string) (string, error) {
bs := make([]byte, len(data.Description)+len(data.Goroutines)) bs := make([]byte, len(data.Description)+len(data.Goroutines))
copy(bs, data.Description) copy(bs, data.Description)
copy(bs[len(data.Description):], data.Goroutines) copy(bs[len(data.Description):], data.Goroutines)
+2 -2
View File
@@ -20,7 +20,7 @@ import (
"github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/svcutil" "github.com/syncthing/syncthing/lib/svcutil"
"github.com/syncthing/syncthing/lib/ur" "github.com/syncthing/syncthing/lib/ur/contract"
) )
type indexHandler struct { type indexHandler struct {
@@ -470,7 +470,7 @@ func (s *indexHandler) logSequenceAnomaly(msg string, extra map[string]any) {
extraStrs[k] = fmt.Sprint(v) extraStrs[k] = fmt.Sprint(v)
} }
s.evLogger.Log(events.Failure, ur.FailureData{ s.evLogger.Log(events.Failure, contract.FailureData{
Description: msg, Description: msg,
Extra: extraStrs, Extra: extraStrs,
}) })
+13
View File
@@ -282,3 +282,16 @@ func clear(v interface{}, since int) error {
} }
return nil return nil
} }
type FailureReport struct {
FailureData
Count int
Version string
}
type FailureData struct {
Description string
Goroutines string
Extra map[string]string
}
+14 -26
View File
@@ -23,6 +23,7 @@ import (
"github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/svcutil" "github.com/syncthing/syncthing/lib/svcutil"
"github.com/syncthing/syncthing/lib/tlsutil" "github.com/syncthing/syncthing/lib/tlsutil"
"github.com/syncthing/syncthing/lib/ur/contract"
"github.com/thejerf/suture/v4" "github.com/thejerf/suture/v4"
) )
@@ -39,23 +40,10 @@ var (
invalidEventDataType = "failure event data is not a string" invalidEventDataType = "failure event data is not a string"
) )
type FailureReport struct { func FailureDataWithGoroutines(description string) contract.FailureData {
FailureData
Count int
Version string
}
type FailureData struct {
Description string
Goroutines string
Extra map[string]string
}
func FailureDataWithGoroutines(description string) FailureData {
var buf strings.Builder var buf strings.Builder
pprof.Lookup("goroutine").WriteTo(&buf, 1) pprof.Lookup("goroutine").WriteTo(&buf, 1)
return FailureData{ return contract.FailureData{
Description: description, Description: description,
Goroutines: buf.String(), Goroutines: buf.String(),
Extra: make(map[string]string), Extra: make(map[string]string),
@@ -86,7 +74,7 @@ type failureHandler struct {
type failureStat struct { type failureStat struct {
first, last time.Time first, last time.Time
count int count int
data FailureData data contract.FailureData
} }
func (h *failureHandler) Serve(ctx context.Context) error { func (h *failureHandler) Serve(ctx context.Context) error {
@@ -105,24 +93,24 @@ func (h *failureHandler) Serve(ctx context.Context) error {
if !ok { if !ok {
// Just to be safe - shouldn't ever happen, as // Just to be safe - shouldn't ever happen, as
// evChan is set to nil when unsubscribing. // evChan is set to nil when unsubscribing.
h.addReport(FailureData{Description: evChanClosed}, time.Now()) h.addReport(contract.FailureData{Description: evChanClosed}, time.Now())
evChan = nil evChan = nil
continue continue
} }
var data FailureData var data contract.FailureData
switch d := e.Data.(type) { switch d := e.Data.(type) {
case string: case string:
data.Description = d data.Description = d
case FailureData: case contract.FailureData:
data = d data = d
default: default:
// Same here, shouldn't ever happen. // Same here, shouldn't ever happen.
h.addReport(FailureData{Description: invalidEventDataType}, time.Now()) h.addReport(contract.FailureData{Description: invalidEventDataType}, time.Now())
continue continue
} }
h.addReport(data, e.Time) h.addReport(data, e.Time)
case <-timer.C: case <-timer.C:
reports := make([]FailureReport, 0, len(h.buf)) reports := make([]contract.FailureReport, 0, len(h.buf))
now := time.Now() now := time.Now()
for descr, stat := range h.buf { for descr, stat := range h.buf {
if now.Sub(stat.last) > minDelay || now.Sub(stat.first) > maxDelay { if now.Sub(stat.last) > minDelay || now.Sub(stat.first) > maxDelay {
@@ -152,7 +140,7 @@ func (h *failureHandler) Serve(ctx context.Context) error {
if sub != nil { if sub != nil {
sub.Unsubscribe() sub.Unsubscribe()
if len(h.buf) > 0 { if len(h.buf) > 0 {
reports := make([]FailureReport, 0, len(h.buf)) reports := make([]contract.FailureReport, 0, len(h.buf))
for _, stat := range h.buf { for _, stat := range h.buf {
reports = append(reports, newFailureReport(stat)) reports = append(reports, newFailureReport(stat))
} }
@@ -179,7 +167,7 @@ func (h *failureHandler) applyOpts(opts config.OptionsConfiguration, sub events.
return url, nil, nil return url, nil, nil
} }
func (h *failureHandler) addReport(data FailureData, evTime time.Time) { func (h *failureHandler) addReport(data contract.FailureData, evTime time.Time) {
if stat, ok := h.buf[data.Description]; ok { if stat, ok := h.buf[data.Description]; ok {
stat.last = evTime stat.last = evTime
stat.count++ stat.count++
@@ -204,7 +192,7 @@ func (*failureHandler) String() string {
return "FailureHandler" return "FailureHandler"
} }
func sendFailureReports(ctx context.Context, reports []FailureReport, url string) { func sendFailureReports(ctx context.Context, reports []contract.FailureReport, url string) {
var b bytes.Buffer var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(reports); err != nil { if err := json.NewEncoder(&b).Encode(reports); err != nil {
panic(err) panic(err)
@@ -235,8 +223,8 @@ func sendFailureReports(ctx context.Context, reports []FailureReport, url string
resp.Body.Close() resp.Body.Close()
} }
func newFailureReport(stat *failureStat) FailureReport { func newFailureReport(stat *failureStat) contract.FailureReport {
return FailureReport{ return contract.FailureReport{
FailureData: stat.data, FailureData: stat.data,
Count: stat.count, Count: stat.count,
Version: build.LongVersion, Version: build.LongVersion,