client

package
v0.0.0-...-8d51551 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2016 License: BSD-2-Clause Imports: 11 Imported by: 2

Documentation

Overview

Package client provides a SysDB client implementation.

The Connect function connects to a SysDB server as the specified user:

c, err := client.Connect("unix:/var/run/sysdbd.sock", "username")
if err != nil {
	// handle error
}
defer c.Close()

Then, it can issue requests to the server:

major, minor, patch, extra, err := c.ServerVersion()
if err != nil {
	// handle error
}
fmt.Printf("Connected to SysDB %d.%d.%d%s\n", major, minor, patch, extra)

or:

res, err := c.Call(&proto.Message{Type: proto.ConnectionServerVersion})
if err != nil {
	// handle error
}
fmt.Printf("%v\n", res)

or execute queries:

q, err := client.QueryString("FETCH %s %s", client.Identifier(typ), name)
if err != nil {
	// handle error
}
res, err := c.Query(q)
if err != nil {
	// handle error
}

// res is one of the object types defined in the sysdb package.
switch typ {
case "host":
	host := res.(*sysdb.Host)
	// do something
	// ...
}

Each client maintains multiple connections to a SysDB server allowing to handle multiple requests in parallel. The SysDB server is able to handle that easily making it a cheap approach. The low-level Dial function creates a single connection to a SysDB server allowing to perform low-level operations:

conn, err := client.Dial("unix:/var/run/sysdbd.sock", "username")
if err != nil {
	// handle error
}
defer conn.Close()

The github.com/sysdb/go/proto package provides support for handling requests and responses. Use the Send and Receive functions to communicate with the server:

m := &proto.Message{
	Type: proto.ConnectionQuery,
	Raw:  []byte{"LOOKUP hosts MATCHING attribute.architecture = 'amd64';"},
}
if err := conn.Send(m); err != nil {
	// handle error
}
m, err := conn.Receive()
if err != nil {
	// handle error
}
if m.Type == proto.ConnectionError {
	// handle failed query
}
// ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QueryString

func QueryString(q string, args ...interface{}) (string, error)

QueryString formats a query string. The query q may include printf string verbs (%s) for each argument. The arguments may be of type Identifier, string, or time.Time and will be formatted to make them suitable for use in a query.

This function tries to prevent injection attacks but it's not fool-proof. It will go away once the SysDB network protocol supports arguments to queries.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

A Client is a client for SysDB.

A client may be used from multiple goroutines in parallel.

func Connect

func Connect(addr, user string) (*Client, error)

Connect creates a new client connected to a SysDB server instance at the specified address using the specified user.

The address may be a IP address or a UNIX domain socket, either prefixed with 'unix:' or specifying an absolute file-system path.

func (*Client) Call

func (c *Client) Call(req *proto.Message) (*proto.Message, error)

Call sends the specified request to the server and waits for its reply. It blocks until the full reply has been received.

func (*Client) Close

func (c *Client) Close()

Close closes a client connection. It may not be further used after calling this function.

The function waits for all pending operations to finish.

func (*Client) Query

func (c *Client) Query(q string) (interface{}, error)

Query executes a query on the server. It returns a sysdb object on success.

func (*Client) ServerVersion

func (c *Client) ServerVersion() (major, minor, patch int, extra string, err error)

ServerVersion queries and returns the version of the remote server.

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

A Conn is a connection to a SysDB server instance.

Multiple goroutines may invoke methods on a Conn simultaneously but since the SysDB protocol requires a strict ordering of request and response messages, the communication with the server will usually happen sequentially.

func Dial

func Dial(addr, user string) (*Conn, error)

Dial sets up a client connection to a SysDB server instance at the specified address using the specified user.

The address may be a UNIX domain socket, either prefixed with 'unix:' or specifying an absolute file-system path.

func (*Conn) Close

func (c *Conn) Close()

Close closes the client connection.

Any blocked Send or Receive operations will be unblocked and return errors.

func (*Conn) Receive

func (c *Conn) Receive() (*proto.Message, error)

Receive waits for a reply from the server and returns the raw message.

Receive operations block until a full message could be read from the underlying socket. This ensures that server and client don't get out of sync.

func (*Conn) Send

func (c *Conn) Send(m *proto.Message) error

Send sends the specified raw message to the server.

Send operations block until the full message could be written to the underlying sockets. This ensures that server and client don't get out of sync.

type Identifier

type Identifier string

An Identifier is a string that may not be quoted or escaped in a query.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL