github.com/mailru/activerecord@v1.12.2/pkg/octopus/connection.go (about) 1 package octopus 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/mailru/activerecord/pkg/iproto/iproto" 8 ) 9 10 var ( 11 ErrConnection = fmt.Errorf("error dial to box") 12 ) 13 14 func GetConnection(ctx context.Context, octopusOpts *ConnectionOptions) (*Connection, error) { 15 pool, err := iproto.Dial(ctx, "tcp", octopusOpts.server, octopusOpts.poolCfg) 16 if err != nil { 17 return nil, fmt.Errorf("%w %s with connect timeout '%d': %s", ErrConnection, octopusOpts.server, octopusOpts.poolCfg.ConnectTimeout, err) 18 } 19 20 return &Connection{pool: pool, opts: octopusOpts}, nil 21 } 22 23 type Connection struct { 24 pool *iproto.Pool 25 opts *ConnectionOptions 26 } 27 28 func (c *Connection) Call(ctx context.Context, rt RequetsTypeType, data []byte) ([]byte, error) { 29 if c == nil || c.pool == nil { 30 return []byte{}, fmt.Errorf("attempt call from empty connection") 31 } 32 33 return c.pool.Call(ctx, uint32(rt), data) 34 } 35 36 func (c *Connection) InstanceMode() any { 37 return c.opts.InstanceMode() 38 } 39 40 func (c *Connection) Close() { 41 if c == nil || c.pool == nil { 42 return 43 } 44 45 c.pool.Close() 46 } 47 48 func (c *Connection) Done() <-chan struct{} { 49 return c.pool.Done() 50 } 51 52 func (c *Connection) Info() string { 53 return fmt.Sprintf("Server: %s, timeout; %d, poolSize: %d", c.opts.server, c.opts.poolCfg.ConnectTimeout, c.opts.poolCfg.Size) 54 }