package telegram

import (
	

	

	
)

func ( *Client) ( context.Context) error {
	.log.Debug("Triggering restart")
	.resetReady()

	select {
	case <-.Done():
		return .Err()
	case .restart <- struct{}{}:
		.log.Debug("Restart initialized")
	}

	select {
	case <-.Done():
		return .Err()
	case <-.ready.Ready():
		.log.Info("Restart ensured")
		return nil
	}
}

func ( *Client) ( context.Context,  int,  bin.Encoder,  bin.Decoder) error {
	// Acquire or cancel.
	select {
	case .migration <- struct{}{}:
	case <-.Done():
		return .Err()
	}
	// Release.
	defer func() {
		<-.migration
	}()

	// Check if someone already migrated.
	 := .session.Load()
	if .DC ==  {
		return .invokeConn(, , )
	}

	,  := context.WithTimeout(, .migrationTimeout)
	defer ()
	if  := .migrateToDc(, );  != nil {
		return errors.Wrap(, "migrate to dc")
	}

	// Re-trying request on another connection.
	return .invokeConn(, , )
}

func ( *Client) ( context.Context,  int) error {
	.session.Migrate()
	return .ensureRestart()
}

// MigrateTo forces client to migrate to another DC.
func ( *Client) ( context.Context,  int) error {
	// Acquire or cancel.
	select {
	case .migration <- struct{}{}:
	case <-.Done():
		return .Err()
	}
	// Release.
	defer func() {
		<-.migration
	}()

	return .migrateToDc(, )
}