Made error handling for redis client less code noisy.

This commit is contained in:
Sascha L. Teichmann 2014-08-03 09:02:18 +02:00
parent 0b11ac01db
commit 8a8351e8c1

129
main.go
View File

@ -43,17 +43,9 @@ func (c *connection) Hget(hash, key []byte) bool {
defer ro.Close() defer ro.Close()
if data, err = c.db.Get(ro, key); err != nil { if data, err = c.db.Get(ro, key); err != nil {
log.Printf("Something is wrong with db: %s", err) log.Printf("Something is wrong with db: %s", err)
if err = c.writeError(); err != nil { return c.writeError()
log.Printf("Send message to client failed: %s", err)
return false
} }
} else { return c.writeBlock(data)
if err = c.writeBlock(data); err != nil {
log.Printf("Send message to client failed: %s", err)
return false
}
}
return true
} }
func (c *connection) Hset(hash, key, block []byte) bool { func (c *connection) Hset(hash, key, block []byte) bool {
@ -64,37 +56,22 @@ func (c *connection) Hset(hash, key, block []byte) bool {
if exists, err = c.keyExists(key); err != nil { if exists, err = c.keyExists(key); err != nil {
log.Printf("Something is wrong with db: %s", err) log.Printf("Something is wrong with db: %s", err)
if err = c.writeError(); err != nil { return c.writeError()
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
if c.tx != nil { if c.tx != nil {
c.tx.Put(key, block) c.tx.Put(key, block)
c.intArray = append(c.intArray, exists) c.intArray = append(c.intArray, exists)
if err = c.writeQueued(); err != nil { return c.writeQueued()
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
wo := leveldb.NewWriteOptions() wo := leveldb.NewWriteOptions()
defer wo.Close() defer wo.Close()
if err = c.db.Put(wo, key, block); err != nil { if err = c.db.Put(wo, key, block); err != nil {
log.Printf("Something is wrong with db: %s", err) log.Printf("Something is wrong with db: %s", err)
if err = c.writeError(); err != nil { return c.writeError()
log.Printf("Writing message to client failed: %s", err)
return false
} }
return true return c.writeInteger(exists)
}
if err = c.writeInteger(exists); err != nil {
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
func (c *connection) keyExists(key []byte) (exists int, err error) { func (c *connection) keyExists(key []byte) (exists int, err error) {
@ -118,21 +95,13 @@ func (c *connection) Multi() bool {
} else { } else {
c.tx = leveldb.NewWriteBatch() c.tx = leveldb.NewWriteBatch()
} }
if err := c.writeOk(); err != nil { return c.writeOk()
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
func (c *connection) Exec() bool { func (c *connection) Exec() bool {
var err error var err error
if c.tx == nil { if c.tx == nil {
if err = c.writeEmptyArray(); err != nil { return c.writeEmptyArray()
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
tx := c.tx tx := c.tx
c.tx = nil c.tx = nil
@ -143,17 +112,9 @@ func (c *connection) Exec() bool {
defer wo.Close() defer wo.Close()
if err = c.db.Write(wo, tx); err != nil { if err = c.db.Write(wo, tx); err != nil {
log.Printf("Something went wrong in writing transaction: %s", err) log.Printf("Something went wrong in writing transaction: %s", err)
if err = c.writeError(); err != nil { return c.writeError()
log.Printf("Writing message to client failed: %s", err)
return false
} }
return true return c.writeIntegerArray(arr)
}
if err = c.writeIntegerArray(arr); err != nil {
log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
var ( var (
@ -165,44 +126,68 @@ var (
redisQueued = []byte("+QUEUED\r\n") redisQueued = []byte("+QUEUED\r\n")
) )
func (c *connection) writeError() (err error) { func (c *connection) writeError() bool {
_, err = c.conn.Write(redisDbError) if _, err := c.conn.Write(redisDbError); err != nil {
return log.Printf("Something went wrong writing to client %s", err)
return false
}
return true
} }
func (c *connection) writeEmptyArray() (err error) { func (c *connection) writeEmptyArray() bool {
_, err = c.conn.Write(redisDbError) if _, err := c.conn.Write(redisDbError); err != nil {
return log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
func (c *connection) writeInteger(v int) (err error) { func (c *connection) writeInteger(v int) bool {
_, err = c.conn.Write([]byte(fmt.Sprintf(":%d\r\n", v))) if _, err := c.conn.Write([]byte(fmt.Sprintf(":%d\r\n", v))); err != nil {
return log.Printf("Writing message to client failed: %s", err)
return false
}
return true
} }
func (c *connection) writeIntegerArray(arr []int) (err error) { func (c *connection) writeIntegerArray(arr []int) bool {
if _, err = c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil { if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil {
return log.Printf("Writing message to client failed: %s", err)
return false
} }
for x := range arr { for x := range arr {
if err = c.writeInteger(x); err != nil { if !c.writeInteger(x) {
return return false
} }
} }
return return true
} }
func (c *connection) writeOk() (err error) { func (c *connection) writeOk() bool {
_, err = c.conn.Write(redisOk) if _, err := c.conn.Write(redisOk); err != nil {
return log.Printf("Something went wrong writing to client %s", err)
return false
}
return true
} }
func (c *connection) writeQueued() (err error) { func (c *connection) writeQueued() bool {
_, err = c.conn.Write(redisQueued) if _, err := c.conn.Write(redisQueued); err != nil {
return log.Printf("Something went wrong writing to client %s", err)
return false
}
return true
} }
func (c *connection) writeBlock(data []byte) (err error) { func (c *connection) writeBlock(data []byte) bool {
if err := c.writeBulkString(data); err != nil {
log.Printf("Something went wrong writing to client %s", err)
return false
}
return true
}
func (c *connection) writeBulkString(data []byte) (err error) {
con := c.conn con := c.conn
if data == nil { if data == nil {
_, err = con.Write(redisNoSuchBlock) _, err = con.Write(redisNoSuchBlock)