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

client needs a unix socket config #10232

Closed
james-lawrence opened this issue Aug 25, 2018 · 2 comments
Closed

client needs a unix socket config #10232

james-lawrence opened this issue Aug 25, 2018 · 2 comments

Comments

@james-lawrence
Copy link

james-lawrence commented Aug 25, 2018

Proposal: add the ability for the client to connect to unix sockets

Current behavior: PR #7315 added the ability for the server to listen over unix sockets, but the client doesn't have a method to connect to them.

Desired behavior: add a method to the client that connects to a unix socket.

Use case: when running telegraf on local systems and an application wants to send metrics to telegraf it shouldn't require a tcp/udp connection because of the client library doesn't support unix sockets.

I've hacked together a unix client for my software, but it'd be nice for this functionality to be
in the clients themselves.

// unixClient - client for connecting to influxdb through unix sockets.
type unixClient struct {
	address string
	net.Conn
	m *sync.RWMutex
}

func (t *unixClient) Query(q client.Query) (*client.Response, error) {
	return nil, errors.New("query is not available with the unix client")
}

// Ping checks that status of cluster, and will always return 0 time and no
// error for UDP clients.
func (t *unixClient) Ping(timeout time.Duration) (time.Duration, string, error) {
	return time.Duration(0), "", nil
}

// Write takes a BatchPoints object and writes all Points to InfluxDB.
func (t *unixClient) Write(bp client.BatchPoints) error {
	var (
		err  error
		conn net.Conn
		b    bytes.Buffer
	)

	for _, p := range bp.Points() {
		if _, err = b.WriteString(p.PrecisionString(bp.Precision())); err != nil {
			return err
		}

		if err = b.WriteByte('\n'); err != nil {
			return err
		}
	}

	if conn, err = t.conn(); err != nil {
		return err
	}

	_, err = conn.Write(b.Bytes())
	return t.badConn(err)
}

func (t *unixClient) conn() (c net.Conn, err error) {
	if c = t.getConn(); c == nil {
		return t.newConn()
	}

	return c, nil
}

func (t *unixClient) newConn() (_ignored net.Conn, err error) {
	t.m.Lock()
	defer t.m.Unlock()

	// recheck incase another thread beat us to creating a conn.
	if t.Conn != nil {
		return t.Conn, nil
	}

	if t.Conn, err = net.Dial("unix", t.address); err != nil {
		return nil, errors.WithStack(err)
	}

	return t.Conn, nil
}

func (t *unixClient) getConn() net.Conn {
	t.m.RLock()
	defer t.m.RUnlock()

	return t.Conn
}

func (t *unixClient) badConn(err error) error {
	if err == nil {
		return err
	}

	_ = t.Close()

	return err
}

func (t *unixClient) Close() (err error) {
	t.m.Lock()
	defer t.m.Unlock()

	// ignore error
	if err = t.Conn.Close(); err != nil {
		return errors.WithStack(err)
	}

	t.Conn = nil

	return nil
}
@dgnorton dgnorton added the 1.x label Jan 7, 2019
@stale
Copy link

stale bot commented Jul 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 24, 2019
@stale
Copy link

stale bot commented Jul 31, 2019

This issue has been automatically closed because it has not had recent activity. Please reopen if this issue is still important to you. Thank you for your contributions.

@stale stale bot closed this as completed Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants