Skip to content

Commit

Permalink
code health: fix all places highlighted by linter
Browse files Browse the repository at this point in the history
Relates to #142
  • Loading branch information
vr009 committed Apr 7, 2022
1 parent 31ebde8 commit bad0a66
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 127 deletions.
84 changes: 60 additions & 24 deletions client_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ type IntKey struct {
I int
}

func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(1)
enc.EncodeInt(k.I)
func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(1); err != nil {
return
}
if err = enc.EncodeInt(k.I); err != nil {
return
}
return nil
}

Expand All @@ -22,9 +26,13 @@ type UintKey struct {
I uint
}

func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(1)
enc.EncodeUint(k.I)
func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(1); err != nil {
return
}
if err = enc.EncodeUint(k.I); err != nil {
return
}
return nil
}

Expand All @@ -34,9 +42,13 @@ type StringKey struct {
S string
}

func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(1)
enc.EncodeString(k.S)
func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(1); err != nil {
return
}
if err = enc.EncodeString(k.S); err != nil {
return
}
return nil
}

Expand All @@ -46,10 +58,16 @@ type IntIntKey struct {
I1, I2 int
}

func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(2)
enc.EncodeInt(k.I1)
enc.EncodeInt(k.I2)
func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(2); err != nil {
return
}
if err = enc.EncodeInt(k.I1); err != nil {
return
}
if err = enc.EncodeInt(k.I2); err != nil {
return
}
return nil
}

Expand All @@ -60,10 +78,16 @@ type Op struct {
Arg interface{}
}

func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(3)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
func (o Op) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(3); err != nil {
return
}
if err = enc.EncodeString(o.Op); err != nil {
return
}
if err = enc.EncodeInt(o.Field); err != nil {
return
}
return enc.Encode(o.Arg)
}

Expand All @@ -75,12 +99,24 @@ type OpSplice struct {
Replace string
}

func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
enc.EncodeSliceLen(5)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
enc.EncodeInt(o.Pos)
enc.EncodeInt(o.Len)
enc.EncodeString(o.Replace)
func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) (err error) {
if err = enc.EncodeSliceLen(5); err != nil {
return
}
if err = enc.EncodeString(o.Op); err != nil {
return
}
if err = enc.EncodeInt(o.Field); err != nil {
return
}
if err = enc.EncodeInt(o.Pos); err != nil {
return
}
if err = enc.EncodeInt(o.Len); err != nil {
return
}
if err = enc.EncodeString(o.Replace); err != nil {
return
}
return nil
}
24 changes: 12 additions & 12 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type connShard struct {
bufmut sync.Mutex
buf smallWBuf
enc *msgpack.Encoder
_pad [16]uint64
_pad [16]uint64 //nolint
}

// Greeting is a message sent by tarantool on connect.
Expand Down Expand Up @@ -280,7 +280,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
conn.mutex.Lock()
defer conn.mutex.Unlock()
if err := conn.createConnection(true); err != nil {
conn.closeConnection(err, true)
_ = conn.closeConnection(err, true)
}
}(conn)
err = nil
Expand All @@ -297,7 +297,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
if err = conn.loadSchema(); err != nil {
conn.mutex.Lock()
defer conn.mutex.Unlock()
conn.closeConnection(err, true)
_ = conn.closeConnection(err, true)
return nil, err
}
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
conn.notify(ReconnectFailed)
reconnects++
conn.mutex.Unlock()
time.Sleep(now.Add(conn.opts.Reconnect).Sub(time.Now()))
time.Sleep(time.Until(now.Add(conn.opts.Reconnect)))
conn.mutex.Lock()
}
if conn.state == connClosed {
Expand Down Expand Up @@ -542,13 +542,13 @@ func (conn *Connection) reconnect(neterr error, c net.Conn) {
defer conn.mutex.Unlock()
if conn.opts.Reconnect > 0 {
if c == conn.c {
conn.closeConnection(neterr, false)
_ = conn.closeConnection(neterr, false)
if err := conn.createConnection(true); err != nil {
conn.closeConnection(err, true)
_ = conn.closeConnection(err, true)
}
}
} else {
conn.closeConnection(neterr, true)
_ = conn.closeConnection(neterr, true)
}
}

Expand Down Expand Up @@ -579,7 +579,7 @@ func (conn *Connection) pinger() {
return
case <-t.C:
}
conn.Ping()
_, _ = conn.Ping()
}
}

Expand Down Expand Up @@ -687,7 +687,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
*pair.last = fut
pair.last = &fut.next
if conn.opts.Timeout > 0 {
fut.timeout = time.Now().Sub(epoch) + conn.opts.Timeout
fut.timeout = time.Until(epoch) + conn.opts.Timeout
}
shard.rmut.Unlock()
if conn.rlimit != nil && conn.opts.RLimitAction == RLimitWait {
Expand Down Expand Up @@ -795,9 +795,9 @@ func (conn *Connection) timeouts() {
return
case <-t.C:
}
minNext := time.Now().Sub(epoch) + timeout
minNext := time.Until(epoch) + timeout
for i := range conn.shard {
nowepoch = time.Now().Sub(epoch)
nowepoch = time.Until(epoch)
shard := &conn.shard[i]
for pos := range shard.requests {
shard.rmut.Lock()
Expand All @@ -824,7 +824,7 @@ func (conn *Connection) timeouts() {
shard.rmut.Unlock()
}
}
nowepoch = time.Now().Sub(epoch)
nowepoch = time.Until(epoch)
if nowepoch+time.Microsecond < minNext {
t.Reset(minNext - nowepoch)
} else {
Expand Down
8 changes: 6 additions & 2 deletions deadline_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ type DeadlineIO struct {

func (d *DeadlineIO) Write(b []byte) (n int, err error) {
if d.to > 0 {
d.c.SetWriteDeadline(time.Now().Add(d.to))
if err = d.c.SetWriteDeadline(time.Now().Add(d.to)); err != nil {
return
}
}
n, err = d.c.Write(b)
return
}

func (d *DeadlineIO) Read(b []byte) (n int, err error) {
if d.to > 0 {
d.c.SetReadDeadline(time.Now().Add(d.to))
if err = d.c.SetReadDeadline(time.Now().Add(d.to)); err != nil {
return
}
}
n, err = d.c.Read(b)
return
Expand Down
8 changes: 4 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Tuple struct {
/* instruct msgpack to pack this struct as array,
* so no custom packer is needed */
_msgpack struct{} `msgpack:",asArray"`
_msgpack struct{} `msgpack:",asArray"` //nolint
Id uint
Msg string
Name string
Expand Down Expand Up @@ -99,7 +99,7 @@ func Example() {
}
client, err := tarantool.Connect(server, opts)
if err != nil {
fmt.Errorf("Failed to connect: %s", err.Error())
fmt.Printf("Failed to connect: %s", err.Error())
return
}

Expand All @@ -109,8 +109,8 @@ func Example() {
fmt.Println("Ping Error", err)

// delete tuple for cleaning
client.Delete(spaceNo, indexNo, []interface{}{uint(10)})
client.Delete(spaceNo, indexNo, []interface{}{uint(11)})
_, _ = client.Delete(spaceNo, indexNo, []interface{}{uint(10)})
_, _ = client.Delete(spaceNo, indexNo, []interface{}{uint(11)})

// insert new tuple { 10, 1 }
resp, err = client.Insert(spaceNo, []interface{}{uint(10), "test", "one"})
Expand Down
Loading

0 comments on commit bad0a66

Please sign in to comment.