runCoordinator runs a coordinator and returns whether a reconnect should occur.
(ctx context.Context, tClient tailnetproto.DRPCTailnetClient24, network *tailnet.Conn)
| 1904 | // runCoordinator runs a coordinator and returns whether a reconnect |
| 1905 | // should occur. |
| 1906 | func (a *agent) runCoordinator(ctx context.Context, tClient tailnetproto.DRPCTailnetClient24, network *tailnet.Conn) (retErr error) { |
| 1907 | // we run the RPC on the hardCtx so that we have a chance to send the disconnect message if we |
| 1908 | // gracefully shut down. |
| 1909 | coordinate, err := tClient.Coordinate(a.hardCtx) |
| 1910 | if err != nil { |
| 1911 | return xerrors.Errorf("failed to connect to the coordinate endpoint: %w", err) |
| 1912 | } |
| 1913 | defer func() { |
| 1914 | reason, initiator := classifyCoordinatorRPCExit(ctx, retErr) |
| 1915 | a.logger.Debug(ctx, "disconnected from coordination RPC", |
| 1916 | codersdk.ConnectionDirectionServerToAgent.SlogField(), |
| 1917 | reason.SlogField(), |
| 1918 | reason.SlogExpectedField(), |
| 1919 | initiator.SlogField(), |
| 1920 | slog.Error(retErr), |
| 1921 | ) |
| 1922 | }() |
| 1923 | defer func() { |
| 1924 | cErr := coordinate.Close() |
| 1925 | if cErr != nil { |
| 1926 | a.logger.Debug(ctx, "error closing Coordinate client", slog.Error(err)) |
| 1927 | } |
| 1928 | }() |
| 1929 | a.logger.Info(ctx, "connected to coordination RPC") |
| 1930 | |
| 1931 | // This allows the Close() routine to wait for the coordinator to gracefully disconnect. |
| 1932 | disconnected := a.setCoordDisconnected() |
| 1933 | if disconnected == nil { |
| 1934 | return nil // already closed by something else |
| 1935 | } |
| 1936 | defer close(disconnected) |
| 1937 | |
| 1938 | ctrl := tailnet.NewAgentCoordinationController(a.logger, network) |
| 1939 | coordination := ctrl.New(coordinate) |
| 1940 | |
| 1941 | errCh := make(chan error, 1) |
| 1942 | go func() { |
| 1943 | defer close(errCh) |
| 1944 | select { |
| 1945 | case <-ctx.Done(): |
| 1946 | err := coordination.Close(a.hardCtx) |
| 1947 | if err != nil { |
| 1948 | a.logger.Warn(ctx, "failed to close remote coordination", slog.Error(err)) |
| 1949 | } |
| 1950 | return |
| 1951 | case err := <-coordination.Wait(): |
| 1952 | errCh <- err |
| 1953 | } |
| 1954 | }() |
| 1955 | return <-errCh |
| 1956 | } |
| 1957 | |
| 1958 | func (a *agent) setCoordDisconnected() chan struct{} { |
| 1959 | a.closeMutex.Lock() |
no test coverage detected