|
| 1 | +// Copyright (c) 2025 Winlin |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +package bootstrap |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + "srs-proxy/internal/debug" |
| 11 | + "srs-proxy/internal/env" |
| 12 | + "srs-proxy/internal/errors" |
| 13 | + "srs-proxy/internal/lb" |
| 14 | + "srs-proxy/internal/logger" |
| 15 | + "srs-proxy/internal/protocol" |
| 16 | + "srs-proxy/internal/signal" |
| 17 | + "srs-proxy/internal/version" |
| 18 | +) |
| 19 | + |
| 20 | +// Bootstrap defines the interface for application bootstrap operations. |
| 21 | +type Bootstrap interface { |
| 22 | + // Start initializes the context with logger and signal handlers, then runs the bootstrap. |
| 23 | + // Returns any error encountered during startup. |
| 24 | + Start(ctx context.Context) error |
| 25 | + |
| 26 | + // Run initializes and starts all proxy servers and the load balancer. |
| 27 | + // It blocks until the context is cancelled. |
| 28 | + Run(ctx context.Context) error |
| 29 | +} |
| 30 | + |
| 31 | +// bootstrapImpl implements the Bootstrap interface. |
| 32 | +type bootstrapImpl struct{} |
| 33 | + |
| 34 | +// NewBootstrap creates a new Bootstrap instance. |
| 35 | +func NewBootstrap() Bootstrap { |
| 36 | + return &bootstrapImpl{} |
| 37 | +} |
| 38 | + |
| 39 | +// Start initializes the context with logger and signal handlers, then runs the bootstrap. |
| 40 | +// Returns any error encountered during startup. |
| 41 | +func (b *bootstrapImpl) Start(ctx context.Context) error { |
| 42 | + ctx = logger.WithContext(ctx) |
| 43 | + logger.Df(ctx, "%v/%v started", version.Signature(), version.Version()) |
| 44 | + |
| 45 | + // Install signals. |
| 46 | + ctx, cancel := context.WithCancel(ctx) |
| 47 | + signal.InstallSignals(ctx, cancel) |
| 48 | + |
| 49 | + // Run the main loop, ignore the user cancel error. |
| 50 | + err := b.Run(ctx) |
| 51 | + if err != nil && ctx.Err() != context.Canceled { |
| 52 | + logger.Ef(ctx, "main: %+v", err) |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + logger.Df(ctx, "%v done", version.Signature()) |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// Run initializes and starts all proxy servers and the load balancer. |
| 61 | +// It blocks until the context is cancelled. |
| 62 | +func (b *bootstrapImpl) Run(ctx context.Context) error { |
| 63 | + // Setup the environment variables. |
| 64 | + environment, err := env.NewEnvironment(ctx) |
| 65 | + if err != nil { |
| 66 | + return errors.Wrapf(err, "create environment") |
| 67 | + } |
| 68 | + |
| 69 | + // When cancelled, the program is forced to exit due to a timeout. Normally, this doesn't occur |
| 70 | + // because the main thread exits after the context is cancelled. However, sometimes the main thread |
| 71 | + // may be blocked for some reason, so a forced exit is necessary to ensure the program terminates. |
| 72 | + if err := signal.InstallForceQuit(ctx, environment); err != nil { |
| 73 | + return errors.Wrapf(err, "install force quit") |
| 74 | + } |
| 75 | + |
| 76 | + // Start the Go pprof if enabled. |
| 77 | + debug.HandleGoPprof(ctx, environment) |
| 78 | + |
| 79 | + // Initialize the load balancer. |
| 80 | + if err := b.initializeLoadBalancer(ctx, environment); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + // Parse the gracefully quit timeout. |
| 85 | + gracefulQuitTimeout, err := time.ParseDuration(environment.GraceQuitTimeout()) |
| 86 | + if err != nil { |
| 87 | + return errors.Wrapf(err, "parse gracefully quit timeout") |
| 88 | + } |
| 89 | + |
| 90 | + // Start all servers and block until context is cancelled. |
| 91 | + return b.startServers(ctx, environment, gracefulQuitTimeout) |
| 92 | +} |
| 93 | + |
| 94 | +// initializeLoadBalancer sets up the load balancer based on configuration. |
| 95 | +func (b *bootstrapImpl) initializeLoadBalancer(ctx context.Context, environment env.Environment) error { |
| 96 | + switch environment.LoadBalancerType() { |
| 97 | + case "redis": |
| 98 | + lb.SrsLoadBalancer = lb.NewRedisLoadBalancer(environment) |
| 99 | + default: |
| 100 | + lb.SrsLoadBalancer = lb.NewMemoryLoadBalancer(environment) |
| 101 | + } |
| 102 | + |
| 103 | + if err := lb.SrsLoadBalancer.Initialize(ctx); err != nil { |
| 104 | + return errors.Wrapf(err, "initialize srs load balancer") |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// startServers initializes and starts all protocol servers. |
| 111 | +func (b *bootstrapImpl) startServers(ctx context.Context, environment env.Environment, gracefulQuitTimeout time.Duration) error { |
| 112 | + // Start the RTMP server. |
| 113 | + srsRTMPServer := protocol.NewSRSRTMPServer(environment) |
| 114 | + if err := srsRTMPServer.Run(ctx); err != nil { |
| 115 | + return errors.Wrapf(err, "rtmp server") |
| 116 | + } |
| 117 | + defer srsRTMPServer.Close() |
| 118 | + |
| 119 | + // Start the WebRTC server. |
| 120 | + srsWebRTCServer := protocol.NewSRSWebRTCServer(environment) |
| 121 | + if err := srsWebRTCServer.Run(ctx); err != nil { |
| 122 | + return errors.Wrapf(err, "rtc server") |
| 123 | + } |
| 124 | + defer srsWebRTCServer.Close() |
| 125 | + |
| 126 | + // Start the HTTP API server. |
| 127 | + srsHTTPAPIServer := protocol.NewSRSHTTPAPIServer(environment, gracefulQuitTimeout, srsWebRTCServer) |
| 128 | + if err := srsHTTPAPIServer.Run(ctx); err != nil { |
| 129 | + return errors.Wrapf(err, "http api server") |
| 130 | + } |
| 131 | + defer srsHTTPAPIServer.Close() |
| 132 | + |
| 133 | + // Start the SRT server. |
| 134 | + srsSRTServer := protocol.NewSRSSRTServer(environment) |
| 135 | + if err := srsSRTServer.Run(ctx); err != nil { |
| 136 | + return errors.Wrapf(err, "srt server") |
| 137 | + } |
| 138 | + defer srsSRTServer.Close() |
| 139 | + |
| 140 | + // Start the System API server. |
| 141 | + systemAPI := protocol.NewSystemAPI(environment, gracefulQuitTimeout) |
| 142 | + if err := systemAPI.Run(ctx); err != nil { |
| 143 | + return errors.Wrapf(err, "system api server") |
| 144 | + } |
| 145 | + defer systemAPI.Close() |
| 146 | + |
| 147 | + // Start the HTTP web server. |
| 148 | + srsHTTPStreamServer := protocol.NewSRSHTTPStreamServer(environment, gracefulQuitTimeout) |
| 149 | + if err := srsHTTPStreamServer.Run(ctx); err != nil { |
| 150 | + return errors.Wrapf(err, "http server") |
| 151 | + } |
| 152 | + defer srsHTTPStreamServer.Close() |
| 153 | + |
| 154 | + // Wait for the main loop to quit. |
| 155 | + <-ctx.Done() |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments