github.com/secure-build/gitlab-runner@v12.5.0+incompatible/session/terminal/terminal.go (about)

     1  package terminal
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  )
     7  
     8  type InteractiveTerminal interface {
     9  	Connect() (Conn, error)
    10  }
    11  
    12  type Conn interface {
    13  	Start(w http.ResponseWriter, r *http.Request, timeoutCh, disconnectCh chan error)
    14  	Close() error
    15  }
    16  
    17  func ProxyTerminal(timeoutCh, disconnectCh, proxyStopCh chan error, proxyFunc func()) {
    18  	disconnected := make(chan bool, 1)
    19  	// terminal exit handler
    20  	go func() {
    21  		// wait for either session timeout or disconnection from the client
    22  		select {
    23  		case err := <-timeoutCh:
    24  			proxyStopCh <- err
    25  		case <-disconnected:
    26  			// forward the disconnection event if there is any waiting receiver
    27  			nonBlockingSend(disconnectCh,
    28  				errors.New("finished proxying (client disconnected?)"))
    29  		}
    30  	}()
    31  
    32  	proxyFunc()
    33  	disconnected <- true
    34  }
    35  
    36  func nonBlockingSend(ch chan error, err error) {
    37  	select {
    38  	case ch <- err:
    39  	default:
    40  	}
    41  }