コード例 #1
0
ファイル: request_handler.go プロジェクト: krumts/gorouter
func (h *RequestHandler) serveWebSocket(iter route.EndpointIterator) error {
	var err error
	var connection net.Conn

	client, _, err := h.hijack()
	if err != nil {
		return err
	}

	defer func() {
		client.Close()
		if connection != nil {
			connection.Close()
		}
	}()

	retry := 0
	for {
		endpoint := iter.Next()
		if endpoint == nil {
			h.reporter.CaptureBadGateway(h.request)
			err = noEndpointsAvailable
			h.HandleBadGateway(err)
			return err
		}

		connection, err = net.DialTimeout("tcp", endpoint.CanonicalAddr(), 5*time.Second)
		if err == nil {
			h.setupRequest(endpoint)
			break
		}

		iter.EndpointFailed()

		h.StenoLogger.Set("Error", err.Error())
		h.StenoLogger.Warn("proxy.websocket.failed")

		retry++
		if retry == maxRetries {
			return err
		}
	}

	if connection != nil {
		err = h.request.Write(connection)
		if err != nil {
			return err
		}

		forwardIO(client, connection)
	}
	return nil
}
コード例 #2
0
func (h *RequestHandler) serveTcp(iter route.EndpointIterator) error {
	var err error
	var connection net.Conn

	client, _, err := h.hijack()
	if err != nil {
		return err
	}

	defer func() {
		client.Close()
		if connection != nil {
			connection.Close()
		}
	}()

	retry := 0
	for {
		endpoint := iter.Next()
		if endpoint == nil {
			h.reporter.CaptureBadGateway(h.request)
			err = noEndpointsAvailable
			h.HandleBadGateway(err)
			return err
		}

		connection, err = net.DialTimeout("tcp", endpoint.CanonicalAddr(), 5*time.Second)
		if err == nil {
			break
		}

		iter.EndpointFailed()
		h.logger.Error("tcp-connection-failed", err)

		retry++
		if retry == maxRetries {
			return err
		}
	}

	if connection != nil {
		forwardIO(client, connection)
	}

	return nil
}