Susumu Yata
null+****@clear*****
Thu Jul 27 12:36:20 JST 2017
Susumu Yata 2017-07-27 12:36:20 +0900 (Thu, 27 Jul 2017) New Revision: b9e91b3d444910dea92426445d30992959495aba https://github.com/groonga/grnci/commit/b9e91b3d444910dea92426445d30992959495aba Message: Add BufferSize to ClientOptions. Modified files: v2/libgrn/client.go v2/libgrn/conn.go Modified: v2/libgrn/client.go (+29 -13) =================================================================== --- v2/libgrn/client.go 2017-07-27 12:35:39 +0900 (a78a14e) +++ v2/libgrn/client.go 2017-07-27 12:36:20 +0900 (bd28e32) @@ -12,21 +12,31 @@ const ( // ClientOptions is options of Client. type ClientOptions struct { + BufferSize int // Buffer size MaxIdleConns int // Maximum number of idle connections } // NewClientOptions returns the default ClientOptions. func NewClientOptions() *ClientOptions { return &ClientOptions{ + BufferSize: defaultBufferSize, MaxIdleConns: defaultMaxIdleConns, } } +// connOptions returns options for conn. +func (o *ClientOptions) connOptions() *connOptions { + options := newConnOptions() + options.BufferSize = o.BufferSize + return options +} + // Client is a thread-safe GQTP client or DB handle. type Client struct { - addr string - baseConn *conn - idleConns chan *conn + addr string + connOptions *connOptions + baseConn *conn + idleConns chan *conn } // Dial returns a new Client connected to a GQTP server. @@ -35,13 +45,15 @@ func Dial(addr string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - cn, err := dial(addr) + connOptions := options.connOptions() + cn, err := dial(addr, connOptions) if err != nil { return nil, err } c := &Client{ - addr: addr, - idleConns: make(chan *conn, options.MaxIdleConns), + addr: addr, + connOptions: connOptions, + idleConns: make(chan *conn, options.MaxIdleConns), } c.idleConns <- cn cn.client = c @@ -53,13 +65,15 @@ func Open(path string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - cn, err := open(path) + connOptions := options.connOptions() + cn, err := open(path, connOptions) if err != nil { return nil, err } return &Client{ - baseConn: cn, - idleConns: make(chan *conn, options.MaxIdleConns), + connOptions: connOptions, + baseConn: cn, + idleConns: make(chan *conn, options.MaxIdleConns), }, nil } @@ -68,13 +82,15 @@ func Create(path string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - cn, err := create(path) + connOptions := options.connOptions() + cn, err := create(path, connOptions) if err != nil { return nil, err } return &Client{ - baseConn: cn, - idleConns: make(chan *conn, options.MaxIdleConns), + connOptions: connOptions, + baseConn: cn, + idleConns: make(chan *conn, options.MaxIdleConns), }, nil } @@ -110,7 +126,7 @@ func (c *Client) exec(cmd string, body io.Reader) (grnci.Response, error) { case conn = <-c.idleConns: default: if c.baseConn == nil { - conn, err = dial(c.addr) + conn, err = dial(c.addr, c.connOptions) if err != nil { return nil, err } Modified: v2/libgrn/conn.go (+36 -18) =================================================================== --- v2/libgrn/conn.go 2017-07-27 12:35:39 +0900 (adfe70e) +++ v2/libgrn/conn.go 2017-07-27 12:36:20 +0900 (a2dec36) @@ -17,28 +17,46 @@ const ( defaultBufferSize = 1 << 16 // Default buffer size ) +// connOptions is options of conn. +type connOptions struct { + BufferSize int +} + +// newConnOptions returns the default connOptions. +func newConnOptions() *connOptions { + return &connOptions{ + BufferSize: defaultBufferSize, + } +} + // conn is a thread-unsafe GQTP client or DB handle. type conn struct { - client *Client // Owner client if available - ctx *grnCtx // C.grn_ctx - db *grnDB // C.grn_obj - buf []byte // Copy buffer - ready bool // Whether or not the connection is ready to send a command - broken bool // Whether or not the connection is broken + client *Client // Owner client if available + ctx *grnCtx // C.grn_ctx + db *grnDB // C.grn_obj + options *connOptions // Options + buf []byte // Copy buffer + ready bool // Whether or not the connection is ready to send a command + broken bool // Whether or not the connection is broken } // newConn returns a new conn. -func newConn(ctx *grnCtx, db *grnDB) *conn { +func newConn(ctx *grnCtx, db *grnDB, options *connOptions) *conn { + if options == nil { + options = newConnOptions() + } + optionsClone := *options return &conn{ - ctx: ctx, - db: db, - buf: make([]byte, defaultBufferSize), - ready: true, + ctx: ctx, + db: db, + buf: make([]byte, options.BufferSize), + options: &optionsClone, + ready: true, } } // dial returns a new conn connected to a GQTP server. -func dial(addr string) (*conn, error) { +func dial(addr string, options *connOptions) (*conn, error) { a, err := grnci.ParseGQTPAddress(addr) if err != nil { return nil, err @@ -55,11 +73,11 @@ func dial(addr string) (*conn, error) { ctx.Close() return nil, err } - return newConn(ctx, nil), nil + return newConn(ctx, nil, options), nil } // open opens an existing DB and returns a new conn as its handle. -func open(path string) (*conn, error) { +func open(path string, options *connOptions) (*conn, error) { ctx, err := newGrnCtx() if err != nil { return nil, err @@ -69,11 +87,11 @@ func open(path string) (*conn, error) { ctx.Close() return nil, err } - return newConn(ctx, db), nil + return newConn(ctx, db, options), nil } // create creates a new DB and returns a new conn as its handle. -func create(path string) (*conn, error) { +func create(path string, options *connOptions) (*conn, error) { ctx, err := newGrnCtx() if err != nil { return nil, err @@ -83,7 +101,7 @@ func create(path string) (*conn, error) { ctx.Close() return nil, err } - return newConn(ctx, db), nil + return newConn(ctx, db, options), nil } // Dup duplicates the conn if it is a DB handle. @@ -97,7 +115,7 @@ func (c *conn) Dup() (*conn, error) { if err != nil { return nil, err } - return newConn(ctx, c.db), nil + return newConn(ctx, c.db, c.options), nil } // Close closes the conn. -------------- next part -------------- HTML����������������������������...下载