func New()

in go/wsl/driver/driver.go [74:137]


func New(ctx context.Context, localHost, sessionID string, caps map[string]interface{}, portRecycler PortRecycler) (*Driver, error) {
	wslCaps, err := extractWSLCaps(sessionID, caps)
	if err != nil {
		return nil, err
	}
	hostPort := net.JoinHostPort(localHost, strconv.Itoa(wslCaps.port))

	d := &Driver{
		Address:      fmt.Sprintf("http://%s", hostPort),
		caps:         wslCaps,
		stopped:      make(chan error),
		portRecycler: portRecycler,
	}

	errChan, err := d.startDriver()
	if err != nil {
		return nil, err
	}

	deadline, cancel := context.WithTimeout(ctx, d.caps.timeout)
	defer cancel()

	statusURL := fmt.Sprintf("http://%s/status", hostPort)

	for {
		select {
		case err := <-errChan:
			return nil, err
		default:
		}

		if response, err := httphelper.Get(deadline, statusURL); err == nil {
			if !d.caps.status {
				// just wait for successful connection because status endpoint doesn't work.
				break
			}
			if response.StatusCode == http.StatusOK {
				respJSON := map[string]interface{}{}
				if err := json.NewDecoder(response.Body).Decode(&respJSON); err == nil {
					if status, ok := respJSON["status"].(float64); ok {
						if int(status) == 0 {
							break
						}
					} else if value, ok := respJSON["value"].(map[string]interface{}); ok {
						if ready, _ := value["ready"].(bool); ready {
							break
						}
					}
				}
			}
		}

		if deadline.Err() != nil {
			if d.cmd != nil {
				go d.cmd.Process.Kill()
			}
			return nil, deadline.Err()
		}

		time.Sleep(10 * time.Millisecond)
	}

	return d, nil
}