From 75b4ba635c0224e046dc3c3d5e8d5d30c5b65d61 Mon Sep 17 00:00:00 2001 From: Artemiy Ryabinkov Date: Thu, 11 Jul 2019 00:16:58 +0300 Subject: [PATCH] try to improve readability of writable checking Signed-off-by: Artemiy Ryabinkov --- conn.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index e570249e..975cf337 100644 --- a/conn.go +++ b/conn.go @@ -79,6 +79,10 @@ func (t TargetSessionType) isValid() error { return errors.New("invalid value for target_session_attrs, expected \"any\" or \"read-write\"") } +func (t TargetSessionType) writableRequired() bool { + return t == ReadWriteTargetSession +} + // ConnConfig contains all the options used to establish a connection. type ConnConfig struct { // Name of host to connect to. (e.g. localhost) @@ -436,7 +440,7 @@ func connect(config ConnConfig, connInfo *pgtype.ConnInfo) (c *Conn, err error) continue } - err = c.writable() + err = c.checkWritable() if err != nil { c.die(err) @@ -470,8 +474,8 @@ func connect(config ConnConfig, connInfo *pgtype.ConnInfo) (c *Conn, err error) return nil, errors.New(strings.Join(errmsgs, ";")) } -func (c *Conn) writable() error { - if c.config.TargetSessionAttrs == "" || c.config.TargetSessionAttrs == AnyTargetSession { +func (c *Conn) checkWritable() error { + if !c.config.TargetSessionAttrs.writableRequired() { return nil } @@ -485,7 +489,7 @@ func (c *Conn) writable() error { switch st { case "on": - return errors.New("writable connection disabled by server") + return errors.New("writable transactions disabled by server") case "off": // If transaction_read_only = off, then connection is writable. return nil