tile38/controller/client.go

45 lines
938 B
Go
Raw Permalink Normal View History

2016-04-01 02:25:47 +03:00
package controller
import (
"net"
"time"
"github.com/tidwall/resp"
)
2016-04-03 05:16:36 +03:00
// Conn represents a simple resp connection.
2016-04-01 02:25:47 +03:00
type Conn struct {
conn net.Conn
rd *resp.Reader
wr *resp.Writer
}
2016-04-03 05:16:36 +03:00
// DialTimeout dials a resp server.
2016-04-01 02:25:47 +03:00
func DialTimeout(address string, timeout time.Duration) (*Conn, error) {
tcpconn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return nil, err
}
conn := &Conn{
conn: tcpconn,
rd: resp.NewReader(tcpconn),
wr: resp.NewWriter(tcpconn),
}
return conn, nil
}
2016-04-03 05:16:36 +03:00
// Close closes the connection.
2016-04-01 02:25:47 +03:00
func (conn *Conn) Close() error {
conn.wr.WriteMultiBulk("quit")
return conn.conn.Close()
}
2016-04-03 05:16:36 +03:00
// Do performs a command and returns a resp value.
2016-04-01 02:25:47 +03:00
func (conn *Conn) Do(commandName string, args ...interface{}) (val resp.Value, err error) {
if err := conn.wr.WriteMultiBulk(commandName, args...); err != nil {
return val, err
}
val, _, err = conn.rd.ReadValue()
return val, err
}