Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'GoError: unexpected EOF' when running behind corporate proxy #15

Closed
lhotrifork opened this issue Mar 23, 2021 · 1 comment
Closed

'GoError: unexpected EOF' when running behind corporate proxy #15

lhotrifork opened this issue Mar 23, 2021 · 1 comment

Comments

@lhotrifork
Copy link

Hi,

I've just made my first xk6 extension which was very fun! Basically it allows you to talk to NATS from k6 scripts.
Everything is working fine on localhost (as always), however I see an error when running in our staging environment, where every outgoing HTTP request must go through our corporate proxy.

Extension code:

package nats

import (
	"context"

	"time"

	"github.com/loadimpact/k6/js/common"
	"github.com/nats-io/nats.go"

	"github.com/loadimpact/k6/js/modules"
)

func init() {
	modules.Register("k6/x/nats", new(Nats))
}

type Nats struct{}

type Client struct {
	client         *nats.Conn
	defaultTimeout time.Duration
}

func (r *Nats) XClient(ctxPtr *context.Context, natsServer string, timeoutMS int) interface{} {
	rt := common.GetRuntime(*ctxPtr)

	conn, err := nats.Connect(natsServer)
	if err != nil {
		panic(err)
	}
	var timeout time.Duration
	if timeoutMS == 0 {
		timeout = time.Millisecond * 5 // 5 second default timeout
	} else {
		timeout = time.Millisecond * time.Duration(timeoutMS)
	}
	return common.Bind(rt, &Client{client: conn, defaultTimeout: timeout}, ctxPtr)
}

func (c *Client) Request(subject string, payload string) (string, error) {
	msg, err := c.client.Request(subject, []byte(payload), c.defaultTimeout)
	if err != nil {
		return "", err
	}
	return string(msg.Data), err
}

func (c *Client) Publish(subject string, payload string) error {
	return c.client.Publish(subject, []byte(payload))
}

Which gets built into a binary:

$ xk6 build v0.31.1 --with github.com/username/xk6-nats-bridge="/home/username/go/src/github.com/username/xk6-nats-bridge"

Used in a script:

import { check } from 'k6';
import nats from 'k6/x/nats';
import * as faker from 'faker/locale/en_US';

export let options = {
  vus: 10,
  duration: '30s',
};

const natsTimeoutMS = 2000;
const natsServer = 'nats:4222';
const natsClient = new nats.Client(natsServer, natsTimeoutMS);

export default function () {
  const want = 'sample data'
  const payload = faker.name.findName()
  const subject = 'testSubject';
  var got = natsClient.request(subject, payload)
  check(got, {
    'Correct data received from NATS': (data) => data === want
  })

  const reportingSubject = 'reports.loadTest';
  natsClient.publish(reportingSubject, 'Tested OK!');
}

The script get bundled with webpack to include

When running the custom binary the proxy settings are set as env vars:

$ export http_proxy=http://proxy.domain.com
$ export HTTP_PROXY=http://proxy.domain.com
$ export https_proxy=http://proxy.domain.com
$ export HTTPS_PROXY=http://proxy.domain.com
$ k6 run main.js

The code works fine when running on my local kubernetes cluster (no proxy), but fails on another cluster that uses the proxy.

It fails with the following error:

time="2021-03-23T16:21:43Z" level=error msg="GoError: unexpected EOF\n\tat github.com/loadimpact/k6/js/common.Bind.func1 (native)\n\tat file:///main.js:1:3972(90)\n" executor=constant-vus scenario=default source=stacktrace

I know that xk6 is bleeding edge and experimental, but any pointers on what goes wrong, or how to increase logging will be much appreciated.

@mstoykov
Copy link
Contributor

mstoykov commented Mar 23, 2021

Hi @lhotrifork ,

I have no experience with nats, but from this docs it seems it is not HTTP based but just build on top of tcp. This means that setting HTTPS_PROXY will not help you.

Given this issue and PR, you should be able to tell the nats to go through a proxy by setting a custom dialer.

Hope this helps you.

I will close this as I don't think this is about the development of xk6 or anything that we can do. You are very much welcome though to open a thread on the community forum where I and others can still help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants