[chore] little startup tweaks (#2941)

* [chore] little startup tweaks

* go fmt
This commit is contained in:
tobi 2024-05-30 11:55:57 +02:00 committed by GitHub
parent 2fd69ec58b
commit 4db596b8b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 41 deletions

View file

@ -78,7 +78,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
// depending on what services were // depending on what services were
// managed to be started. // managed to be started.
state state.State state = new(state.State)
route *router.Router route *router.Router
) )
@ -137,7 +137,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
state.Caches.Start() state.Caches.Start()
// Open connection to the database now caches started. // Open connection to the database now caches started.
dbService, err := bundb.NewBunDBService(ctx, &state) dbService, err := bundb.NewBunDBService(ctx, state)
if err != nil { if err != nil {
return fmt.Errorf("error creating dbservice: %s", err) return fmt.Errorf("error creating dbservice: %s", err)
} }
@ -177,14 +177,14 @@ var Start action.GTSAction = func(ctx context.Context) error {
}) })
// Build handlers used in later initializations. // Build handlers used in later initializations.
mediaManager := media.NewManager(&state) mediaManager := media.NewManager(state)
oauthServer := oauth.New(ctx, dbService) oauthServer := oauth.New(ctx, dbService)
typeConverter := typeutils.NewConverter(&state) typeConverter := typeutils.NewConverter(state)
visFilter := visibility.NewFilter(&state) visFilter := visibility.NewFilter(state)
spamFilter := spam.NewFilter(&state) spamFilter := spam.NewFilter(state)
federatingDB := federatingdb.New(&state, typeConverter, visFilter, spamFilter) federatingDB := federatingdb.New(state, typeConverter, visFilter, spamFilter)
transportController := transport.NewController(&state, federatingDB, &federation.Clock{}, client) transportController := transport.NewController(state, federatingDB, &federation.Clock{}, client)
federator := federation.NewFederator(&state, federatingDB, transportController, typeConverter, visFilter, mediaManager) federator := federation.NewFederator(state, federatingDB, transportController, typeConverter, visFilter, mediaManager)
// Decide whether to create a noop email // Decide whether to create a noop email
// sender (won't send emails) or a real one. // sender (won't send emails) or a real one.
@ -205,18 +205,18 @@ var Start action.GTSAction = func(ctx context.Context) error {
// Initialize both home / list timelines. // Initialize both home / list timelines.
state.Timelines.Home = timeline.NewManager( state.Timelines.Home = timeline.NewManager(
tlprocessor.HomeTimelineGrab(&state), tlprocessor.HomeTimelineGrab(state),
tlprocessor.HomeTimelineFilter(&state, visFilter), tlprocessor.HomeTimelineFilter(state, visFilter),
tlprocessor.HomeTimelineStatusPrepare(&state, typeConverter), tlprocessor.HomeTimelineStatusPrepare(state, typeConverter),
tlprocessor.SkipInsert(), tlprocessor.SkipInsert(),
) )
if err := state.Timelines.Home.Start(); err != nil { if err := state.Timelines.Home.Start(); err != nil {
return fmt.Errorf("error starting home timeline: %s", err) return fmt.Errorf("error starting home timeline: %s", err)
} }
state.Timelines.List = timeline.NewManager( state.Timelines.List = timeline.NewManager(
tlprocessor.ListTimelineGrab(&state), tlprocessor.ListTimelineGrab(state),
tlprocessor.ListTimelineFilter(&state, visFilter), tlprocessor.ListTimelineFilter(state, visFilter),
tlprocessor.ListTimelineStatusPrepare(&state, typeConverter), tlprocessor.ListTimelineStatusPrepare(state, typeConverter),
tlprocessor.SkipInsert(), tlprocessor.SkipInsert(),
) )
if err := state.Timelines.List.Start(); err != nil { if err := state.Timelines.List.Start(); err != nil {
@ -230,17 +230,19 @@ var Start action.GTSAction = func(ctx context.Context) error {
// Add a task to the scheduler to sweep caches. // Add a task to the scheduler to sweep caches.
// Frequency = 1 * minute // Frequency = 1 * minute
// Threshold = 60% capacity // Threshold = 60% capacity
_ = state.Workers.Scheduler.AddRecurring( if !state.Workers.Scheduler.AddRecurring(
"@cachesweep", // id "@cachesweep", // id
time.Time{}, // start time.Time{}, // start
time.Minute, // freq time.Minute, // freq
func(context.Context, time.Time) { func(context.Context, time.Time) {
state.Caches.Sweep(60) state.Caches.Sweep(60)
}, },
) ) {
return fmt.Errorf("error scheduling cache sweep: %w", err)
}
// Create background cleaner. // Create background cleaner.
cleaner := cleaner.New(&state) cleaner := cleaner.New(state)
// Now schedule background cleaning tasks. // Now schedule background cleaning tasks.
if err := cleaner.ScheduleJobs(); err != nil { if err := cleaner.ScheduleJobs(); err != nil {
@ -255,7 +257,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
federator, federator,
oauthServer, oauthServer,
mediaManager, mediaManager,
&state, state,
emailSender, emailSender,
) )
@ -308,7 +310,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
// note: hooks adding ctx fields must be ABOVE // note: hooks adding ctx fields must be ABOVE
// the logger, otherwise won't be accessible. // the logger, otherwise won't be accessible.
middleware.Logger(config.GetLogClientIP()), middleware.Logger(config.GetLogClientIP()),
middleware.HeaderFilter(&state), middleware.HeaderFilter(state),
middleware.UserAgent(), middleware.UserAgent(),
middleware.CORS(), middleware.CORS(),
middleware.ExtraHeaders(), middleware.ExtraHeaders(),
@ -366,7 +368,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
var ( var (
authModule = api.NewAuth(dbService, processor, idp, routerSession, sessionName) // auth/oauth paths authModule = api.NewAuth(dbService, processor, idp, routerSession, sessionName) // auth/oauth paths
clientModule = api.NewClient(&state, processor) // api client endpoints clientModule = api.NewClient(state, processor) // api client endpoints
metricsModule = api.NewMetrics() // Metrics endpoints metricsModule = api.NewMetrics() // Metrics endpoints
healthModule = api.NewHealth(dbService.Ready) // Health check endpoints healthModule = api.NewHealth(dbService.Ready) // Health check endpoints
fileserverModule = api.NewFileserver(processor) // fileserver endpoints fileserverModule = api.NewFileserver(processor) // fileserver endpoints

View file

@ -64,7 +64,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
// depending on what services were // depending on what services were
// managed to be started. // managed to be started.
state state.State state = new(state.State)
route *router.Router route *router.Router
) )
@ -85,7 +85,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
// Stop any currently running // Stop any currently running
// worker processes / scheduled // worker processes / scheduled
// tasks from being executed. // tasks from being executed.
testrig.StopWorkers(&state) testrig.StopWorkers(state)
if state.Timelines.Home != nil { if state.Timelines.Home != nil {
// Home timeline mgr was setup, ensure it gets stopped. // Home timeline mgr was setup, ensure it gets stopped.
@ -130,7 +130,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
} }
// Initialize caches and database // Initialize caches and database
state.DB = testrig.NewTestDB(&state) state.DB = testrig.NewTestDB(state)
// New test db inits caches so we don't need to do // New test db inits caches so we don't need to do
// that twice, we can just start the initialized caches. // that twice, we can just start the initialized caches.
@ -156,11 +156,11 @@ var Start action.GTSAction = func(ctx context.Context) error {
testrig.StandardStorageSetup(state.Storage, "./testrig/media") testrig.StandardStorageSetup(state.Storage, "./testrig/media")
// Initialize workers. // Initialize workers.
testrig.StartNoopWorkers(&state) testrig.StartNoopWorkers(state)
defer testrig.StopWorkers(&state) defer testrig.StopWorkers(state)
// build backend handlers // build backend handlers
transportController := testrig.NewTestTransportController(&state, testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) { transportController := testrig.NewTestTransportController(state, testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
r := io.NopCloser(bytes.NewReader([]byte{})) r := io.NopCloser(bytes.NewReader([]byte{}))
return &http.Response{ return &http.Response{
StatusCode: 200, StatusCode: 200,
@ -170,34 +170,34 @@ var Start action.GTSAction = func(ctx context.Context) error {
}, },
}, nil }, nil
}, "")) }, ""))
mediaManager := testrig.NewTestMediaManager(&state) mediaManager := testrig.NewTestMediaManager(state)
federator := testrig.NewTestFederator(&state, transportController, mediaManager) federator := testrig.NewTestFederator(state, transportController, mediaManager)
emailSender := testrig.NewEmailSender("./web/template/", nil) emailSender := testrig.NewEmailSender("./web/template/", nil)
typeConverter := typeutils.NewConverter(&state) typeConverter := typeutils.NewConverter(state)
filter := visibility.NewFilter(&state) filter := visibility.NewFilter(state)
// Initialize both home / list timelines. // Initialize both home / list timelines.
state.Timelines.Home = timeline.NewManager( state.Timelines.Home = timeline.NewManager(
tlprocessor.HomeTimelineGrab(&state), tlprocessor.HomeTimelineGrab(state),
tlprocessor.HomeTimelineFilter(&state, filter), tlprocessor.HomeTimelineFilter(state, filter),
tlprocessor.HomeTimelineStatusPrepare(&state, typeConverter), tlprocessor.HomeTimelineStatusPrepare(state, typeConverter),
tlprocessor.SkipInsert(), tlprocessor.SkipInsert(),
) )
if err := state.Timelines.Home.Start(); err != nil { if err := state.Timelines.Home.Start(); err != nil {
return fmt.Errorf("error starting home timeline: %s", err) return fmt.Errorf("error starting home timeline: %s", err)
} }
state.Timelines.List = timeline.NewManager( state.Timelines.List = timeline.NewManager(
tlprocessor.ListTimelineGrab(&state), tlprocessor.ListTimelineGrab(state),
tlprocessor.ListTimelineFilter(&state, filter), tlprocessor.ListTimelineFilter(state, filter),
tlprocessor.ListTimelineStatusPrepare(&state, typeConverter), tlprocessor.ListTimelineStatusPrepare(state, typeConverter),
tlprocessor.SkipInsert(), tlprocessor.SkipInsert(),
) )
if err := state.Timelines.List.Start(); err != nil { if err := state.Timelines.List.Start(); err != nil {
return fmt.Errorf("error starting list timeline: %s", err) return fmt.Errorf("error starting list timeline: %s", err)
} }
processor := testrig.NewTestProcessor(&state, federator, emailSender, mediaManager) processor := testrig.NewTestProcessor(state, federator, emailSender, mediaManager)
// Initialize metrics. // Initialize metrics.
if err := metrics.Initialize(state.DB); err != nil { if err := metrics.Initialize(state.DB); err != nil {
@ -222,7 +222,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
middlewares = append(middlewares, []gin.HandlerFunc{ middlewares = append(middlewares, []gin.HandlerFunc{
middleware.Logger(config.GetLogClientIP()), middleware.Logger(config.GetLogClientIP()),
middleware.HeaderFilter(&state), middleware.HeaderFilter(state),
middleware.UserAgent(), middleware.UserAgent(),
middleware.CORS(), middleware.CORS(),
middleware.ExtraHeaders(), middleware.ExtraHeaders(),
@ -280,7 +280,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
var ( var (
authModule = api.NewAuth(state.DB, processor, idp, routerSession, sessionName) // auth/oauth paths authModule = api.NewAuth(state.DB, processor, idp, routerSession, sessionName) // auth/oauth paths
clientModule = api.NewClient(&state, processor) // api client endpoints clientModule = api.NewClient(state, processor) // api client endpoints
metricsModule = api.NewMetrics() // Metrics endpoints metricsModule = api.NewMetrics() // Metrics endpoints
healthModule = api.NewHealth(state.DB.Ready) // Health check endpoints healthModule = api.NewHealth(state.DB.Ready) // Health check endpoints
fileserverModule = api.NewFileserver(processor) // fileserver endpoints fileserverModule = api.NewFileserver(processor) // fileserver endpoints
@ -304,7 +304,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
webModule.Route(route) webModule.Route(route)
// Create background cleaner. // Create background cleaner.
cleaner := cleaner.New(&state) cleaner := cleaner.New(state)
// Now schedule background cleaning tasks. // Now schedule background cleaning tasks.
if err := cleaner.ScheduleJobs(); err != nil { if err := cleaner.ScheduleJobs(); err != nil {