feat(idlewatcher): implement real-time SSE-based loading page with enhanced UX

This major overhaul of the idlewatcher system introduces a modern, real-time loading experience with Server-Sent Events (SSE) streaming and improved error handling.

- **Real-time Event Streaming**: New SSE endpoint (`/$godoxy/wake-events`) provides live updates during container wake process
- **Enhanced Loading Page**: Modern console-style interface with timestamped events and color-coded status messages
- **Improved Static Asset Management**: Dedicated paths for CSS, JS, and favicon to avoid conflicting with upstream assets
- **Event History Buffer**: Stores wake events for reconnecting clients and debugging

- Refactored HTTP request handling with cleaner static asset routing
- Added `WakeEvent` system with structured event types (starting, waking_dep, dep_ready, container_woke, waiting_ready, ready, error)
- Implemented thread-safe event broadcasting using xsync.Map for concurrent SSE connections
- Enhanced error handling with detailed logging and user-friendly error messages
- Simplified loading page template system with better asset path management
- Fixed race conditions in dependency waking and state management

- Removed `common.go` functions (canceled, waitStarted) - moved inline for better context
- Updated Waker interface to accept context parameter in Wake() method
- New static asset paths use `/$godoxy/` prefix to avoid conflicts

- Console-style output with Fira Code font for better readability
- Color-coded event types (yellow for starting, blue for dependencies, green for success, red for errors)
- Automatic page refresh when container becomes ready
- Improved visual design with better glassmorphism effects and responsive layout
- Real-time progress feedback during dependency wake and container startup

This change transforms the static loading page into a dynamic, informative experience that keeps users informed during the wake process while maintaining backward compatibility with existing routing behavior.
This commit is contained in:
yusing
2025-11-07 14:58:33 +08:00
parent 3cbd70f73a
commit 910ef639a4
19 changed files with 531 additions and 180 deletions

View File

@@ -8,11 +8,12 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
idlewatcher "github.com/yusing/godoxy/internal/idlewatcher/types"
"github.com/yusing/godoxy/internal/types"
"github.com/yusing/godoxy/internal/utils/pool"
gperr "github.com/yusing/goutils/errs"
"github.com/yusing/goutils/http/httpheaders"
"github.com/yusing/goutils/task"
"golang.org/x/sync/errgroup"
)
// TODO: stats of each server.
@@ -218,14 +219,20 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
http.Error(rw, "Service unavailable", http.StatusServiceUnavailable)
return
}
if r.Header.Get(httpheaders.HeaderGoDoxyCheckRedirect) != "" {
if r.URL.Path == idlewatcher.WakeEventsPath {
var errs errgroup.Group
// wake all servers
for _, srv := range srvs {
if err := srv.TryWake(); err != nil {
lb.l.Warn().Err(err).
Str("server", srv.Name()).
Msg("failed to wake server")
}
errs.Go(func() error {
err := srv.TryWake()
if err != nil {
return fmt.Errorf("failed to wake server %q: %w", srv.Name(), err)
}
return nil
})
}
if err := errs.Wait(); err != nil {
gperr.LogWarn("failed to wake some servers", err, &lb.l)
}
}
lb.impl.ServeHTTP(srvs, rw, r)

View File

@@ -1,6 +1,7 @@
package loadbalancer
import (
"context"
"net/http"
idlewatcher "github.com/yusing/godoxy/internal/idlewatcher/types"
@@ -66,7 +67,7 @@ func (srv *server) String() string {
func (srv *server) TryWake() error {
waker, ok := srv.Handler.(idlewatcher.Waker)
if ok {
return waker.Wake()
return waker.Wake(context.Background())
}
return nil
}