mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-15 14:50:21 +01:00
Made error handling for redis client less code noisy.
This commit is contained in:
parent
0b11ac01db
commit
8a8351e8c1
129
main.go
129
main.go
|
@ -43,17 +43,9 @@ func (c *connection) Hget(hash, key []byte) bool {
|
|||
defer ro.Close()
|
||||
if data, err = c.db.Get(ro, key); err != nil {
|
||||
log.Printf("Something is wrong with db: %s", err)
|
||||
if err = c.writeError(); err != nil {
|
||||
log.Printf("Send message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if err = c.writeBlock(data); err != nil {
|
||||
log.Printf("Send message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return c.writeError()
|
||||
}
|
||||
return true
|
||||
return c.writeBlock(data)
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Printf("Something is wrong with db: %s", err)
|
||||
if err = c.writeError(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeError()
|
||||
}
|
||||
|
||||
if c.tx != nil {
|
||||
c.tx.Put(key, block)
|
||||
c.intArray = append(c.intArray, exists)
|
||||
if err = c.writeQueued(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeQueued()
|
||||
}
|
||||
|
||||
wo := leveldb.NewWriteOptions()
|
||||
defer wo.Close()
|
||||
if err = c.db.Put(wo, key, block); err != nil {
|
||||
log.Printf("Something is wrong with db: %s", err)
|
||||
if err = c.writeError(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeError()
|
||||
}
|
||||
if err = c.writeInteger(exists); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeInteger(exists)
|
||||
}
|
||||
|
||||
func (c *connection) keyExists(key []byte) (exists int, err error) {
|
||||
|
@ -118,21 +95,13 @@ func (c *connection) Multi() bool {
|
|||
} else {
|
||||
c.tx = leveldb.NewWriteBatch()
|
||||
}
|
||||
if err := c.writeOk(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeOk()
|
||||
}
|
||||
|
||||
func (c *connection) Exec() bool {
|
||||
var err error
|
||||
if c.tx == nil {
|
||||
if err = c.writeEmptyArray(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeEmptyArray()
|
||||
}
|
||||
tx := c.tx
|
||||
c.tx = nil
|
||||
|
@ -143,17 +112,9 @@ func (c *connection) Exec() bool {
|
|||
defer wo.Close()
|
||||
if err = c.db.Write(wo, tx); err != nil {
|
||||
log.Printf("Something went wrong in writing transaction: %s", err)
|
||||
if err = c.writeError(); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeError()
|
||||
}
|
||||
if err = c.writeIntegerArray(arr); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return c.writeIntegerArray(arr)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -165,44 +126,68 @@ var (
|
|||
redisQueued = []byte("+QUEUED\r\n")
|
||||
)
|
||||
|
||||
func (c *connection) writeError() (err error) {
|
||||
_, err = c.conn.Write(redisDbError)
|
||||
return
|
||||
func (c *connection) writeError() bool {
|
||||
if _, err := c.conn.Write(redisDbError); err != nil {
|
||||
log.Printf("Something went wrong writing to client %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *connection) writeEmptyArray() (err error) {
|
||||
_, err = c.conn.Write(redisDbError)
|
||||
return
|
||||
func (c *connection) writeEmptyArray() bool {
|
||||
if _, err := c.conn.Write(redisDbError); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *connection) writeInteger(v int) (err error) {
|
||||
_, err = c.conn.Write([]byte(fmt.Sprintf(":%d\r\n", v)))
|
||||
return
|
||||
func (c *connection) writeInteger(v int) bool {
|
||||
if _, err := c.conn.Write([]byte(fmt.Sprintf(":%d\r\n", v))); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *connection) writeIntegerArray(arr []int) (err error) {
|
||||
if _, err = c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil {
|
||||
return
|
||||
func (c *connection) writeIntegerArray(arr []int) bool {
|
||||
if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil {
|
||||
log.Printf("Writing message to client failed: %s", err)
|
||||
return false
|
||||
}
|
||||
for x := range arr {
|
||||
if err = c.writeInteger(x); err != nil {
|
||||
return
|
||||
if !c.writeInteger(x) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *connection) writeOk() (err error) {
|
||||
_, err = c.conn.Write(redisOk)
|
||||
return
|
||||
func (c *connection) writeOk() bool {
|
||||
if _, err := c.conn.Write(redisOk); err != nil {
|
||||
log.Printf("Something went wrong writing to client %s", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *connection) writeQueued() (err error) {
|
||||
_, err = c.conn.Write(redisQueued)
|
||||
return
|
||||
func (c *connection) writeQueued() bool {
|
||||
if _, err := c.conn.Write(redisQueued); err != nil {
|
||||
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
|
||||
if data == nil {
|
||||
_, err = con.Write(redisNoSuchBlock)
|
||||
|
|
Loading…
Reference in New Issue
Block a user