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 ¶
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 ¶
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 ¶
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.
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 ¶
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.
type Identifier ¶
type Identifier string
An Identifier is a string that may not be quoted or escaped in a query.