Documentation ¶
Overview ¶
Connections
The Conn interface is the primary interface for working with RexPro. Applications create connections by calling the Dial, DialWithTimeout or NewConn functions.
The application must call the connection Close method when the application is done with the connection.
Executing Commands ¶
The Conn interface has a generic method for executing Gremlin commands using rexpro:
DoScript(script string, bindings map[string]interface{}) (reply interface{}, err error)
Concurrency ¶
Connections support a single concurrent caller to the `DoScript` method. For full concurrent access to RexPro, use the thread-safe Pool to get and release connections from within a goroutine.
Usage - Simple
func main() { var properties = map[string]interface{}{"properties": map[string]interface{}{"foo": "bar", "score": 5}} if c, err := rexpro.Dial("your-host:8184", "your-graph-name"); err == nil { defer c.Close() if resp, err := c.DoScript("g.addVertex(properties)", properties); err == nil { doSomethingWith(resp) } } }
Usage - Connection Pools
var RexproPool *rexpro.Pool func init() { RexproPool = &rexpro.Pool{ MaxIdle: 5, MaxActive: 200, IdleTimeout: 25 * time.Second, Dial: func() (rexpro.Conn, error) { return rexpro.Dial("your-host:8184", "your-graph-name") }, TestOnBorrow: func(c rexpro.Conn, t time.Time) error { _, err := c.DoScript("1", map[string]interface{}{}) return err }, } } func main() { var ( c = RexproPool.Get() properties = map[string]interface{}{"properties": map[string]interface{}{"foo": "bar", "score": 5}} ) defer c.Close() if resp, err := c.DoScript("g.addVertex(properties)", properties); err == nil { doSomethingWith(resp) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPoolExhausted = errors.New("gorexpro: connection pool exhausted")
ErrPoolExhausted is returned from pool connection methods when the maximum number of database connections in the pool has been reached.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn interface { // Closes the connection. Close() error // DoScript sends a command and bindings to the server and // returns the received reply. DoScript(string, map[string]interface{}) ([]interface{}, error) // NewSession creates a new rexpro session handler NewSession() (Session, error) // NewAuthSession creates a new authenticated rexpro session handler NewAuthSession(string, string) (Session, error) }
type MessageType ¶
type MessageType byte
const ( MSGPACK MessageType = iota JSON )
Serializer Definitions
const ( ERROR MessageType = iota SESSION_REQUEST SESSION_RESPONSE SCRIPT_REQUEST SCRIPT_RESPONSE )
Message Definitions
type Pool ¶
type Pool struct { // Dial is an application supplied function for creating new connections. Dial func() (Conn, error) // TestOnBorrow is an optional application supplied function for checking // the health of an idle connection before the connection is used again by // the application. Argument t is the time that the connection was returned // to the pool. If the function returns an error, then the connection is // closed. TestOnBorrow func(c Conn, t time.Time) error // Maximum number of idle connections in the pool. MaxIdle int // Maximum number of connections allocated by the pool at a given time. // When zero, there is no limit on the number of connections in the pool. MaxActive int // Close connections after remaining idle for this duration. If the value // is zero, then idle connections are not closed. Applications should set // the timeout to a value less than the server's timeout. IdleTimeout time.Duration // contains filtered or unexported fields }
Pool maintains a pool of connections. The application calls the Get method to get a connection from the pool and the connection's Close method to return the connection's resources to the pool.
The following example shows how to use a pool in your application. The application creates a pool at application startup and makes it available to request handlers, possibly using a global variable:
var server string // host:port of server var graphName string // name of graph ... pool = &rexpro.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func () (rexpro.Conn, error) { c, err := rexpro.Dial(server, graphName) if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c rexpro.Conn, t time.Time) error { _, err := c.DoScript("1") return err }, }
This pool has a maximum of three connections to the server specified by the variable "server". Each connection is authenticated using a password.
A request handler gets a connection from the pool and closes the connection when the handler is done:
conn := pool.Get() defer conn.Close() // do something with the connection
func NewPool ¶
NewPool returns a pool that uses newPool to create connections as needed. The pool keeps a maximum of maxIdle idle connections.
func (*Pool) ActiveCount ¶
ActiveCount returns the number of active connections in the pool.
type SerializerType ¶
type SerializerType byte
type Session ¶
type Session interface { // Begin starts a new session on the rexpro server Begin() error // Close closes the session at the rexpro server while leaving the // connection intact. Close() error // DoScript sends a command and bindings to the server and // returns the received reply. DoScript(string, map[string]interface{}) ([]interface{}, error) }