Skip to content

Commit

Permalink
fix golint
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony committed Dec 17, 2018
1 parent 2c27861 commit b034eca
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 198 deletions.
112 changes: 53 additions & 59 deletions benchmarks/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,57 @@ package main
import (
"bufio"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
//mrand "math/rand"
"net"
"os"
"runtime"
"strconv"
"sync/atomic"
"time"

log "github.com/thinkboy/log4go"
log "github.com/golang/glog"
)

const (
OP_HANDSHARE = int32(0)
OP_HANDSHARE_REPLY = int32(1)
OP_HEARTBEAT = int32(2)
OP_HEARTBEAT_REPLY = int32(3)
OP_SEND_SMS = int32(4)
OP_SEND_SMS_REPLY = int32(5)
OP_DISCONNECT_REPLY = int32(6)
OP_AUTH = int32(7)
OP_AUTH_REPLY = int32(8)
OP_TEST = int32(254)
OP_TEST_REPLY = int32(255)
opHeartbeat = int32(2)
opHeartbeatReply = int32(3)
opAuth = int32(7)
opAuthReply = int32(8)
)

const (
rawHeaderLen = uint16(16)
heart = 240 * time.Second //s
heart = 30 * time.Second
)

// Proto proto.
type Proto struct {
PackLen int32 // package length
HeaderLen int16 // header length
Ver int16 // protocol version
Operation int32 // operation for request
SeqId int32 // sequence number chosen by client
Seq int32 // sequence number chosen by client
Body []byte // body
}

// AuthToken auth token.
type AuthToken struct {
Mid int64
Key string
RoomID string
Platform string
Accepts []int32
}

var (
countDown int64
)

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
log.Global = log.NewDefaultLogger(log.DEBUG)
flag.Parse()
defer log.Close()
begin, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
Expand Down Expand Up @@ -109,46 +110,49 @@ func startClient(key string) {

conn, err := net.Dial("tcp", os.Args[3])
if err != nil {
log.Error("net.Dial(\"%s\") error(%v)", os.Args[3], err)
log.Errorf("net.Dial(%s) error(%v)", os.Args[3], err)
return
}
seqId := int32(0)
seq := int32(0)
wr := bufio.NewWriter(conn)
rd := bufio.NewReader(conn)
authToken := &AuthToken{
123,
"test_key",
"test://1000",
"ios",
[]int32{1000, 1001, 1002},
}
proto := new(Proto)
proto.Ver = 1
// auth
// test handshake timeout
// time.Sleep(time.Second * 31)
proto.Operation = OP_AUTH
proto.SeqId = seqId
proto.Body = []byte(key)
proto.Operation = opAuth
proto.Seq = seq
proto.Body, _ = json.Marshal(authToken)
if err = tcpWriteProto(wr, proto); err != nil {
log.Error("tcpWriteProto() error(%v)", err)
log.Errorf("tcpWriteProto() error(%v)", err)
return
}
if err = tcpReadProto(rd, proto); err != nil {
log.Error("tcpReadProto() error(%v)", err)
log.Errorf("tcpReadProto() error(%v)", err)
return
}
log.Debug("key:%s auth ok, proto: %v", key, proto)
seqId++
log.Infof("key:%s auth ok, proto: %v", key, proto)
seq++
// writer
go func() {
proto1 := new(Proto)
hbProto := new(Proto)
for {
// heartbeat
proto1.Operation = OP_HEARTBEAT
proto1.SeqId = seqId
proto1.Body = nil
if err = tcpWriteProto(wr, proto1); err != nil {
log.Error("key:%s tcpWriteProto() error(%v)", key, err)
hbProto.Operation = opHeartbeat
hbProto.Seq = seq
hbProto.Body = nil
if err = tcpWriteProto(wr, hbProto); err != nil {
log.Errorf("key:%s tcpWriteProto() error(%v)", key, err)
return
}
log.Debug("key:%s Write heartbeat", key)
// test heartbeat
log.Infof("key:%s Write heartbeat", key)
time.Sleep(heart)
seqId++
seq++
select {
case <-quit:
return
Expand All @@ -159,21 +163,21 @@ func startClient(key string) {
// reader
for {
if err = tcpReadProto(rd, proto); err != nil {
log.Error("key:%s tcpReadProto() error(%v)", key, err)
log.Errorf("key:%s tcpReadProto() error(%v)", key, err)
quit <- true
return
}
if proto.Operation == OP_HEARTBEAT_REPLY {
log.Debug("key:%s receive heartbeat", key)
if proto.Operation == opAuthReply {
log.Infof("key:%s auth success", key)
} else if proto.Operation == opHeartbeatReply {
log.Infof("key:%s receive heartbeat", key)
if err = conn.SetReadDeadline(time.Now().Add(heart + 60*time.Second)); err != nil {
log.Error("conn.SetReadDeadline() error(%v)", err)
log.Errorf("conn.SetReadDeadline() error(%v)", err)
quit <- true
return
}
} else if proto.Operation == OP_TEST_REPLY {
log.Debug("body: %s", string(proto.Body))
} else if proto.Operation == OP_SEND_SMS_REPLY {
log.Info("key:%s msg: %s", key, string(proto.Body))
} else {
log.Infof("key:%s op:%d msg: %s", key, proto.Operation, string(proto.Body))
atomic.AddInt64(&countDown, 1)
}
}
Expand All @@ -193,11 +197,10 @@ func tcpWriteProto(wr *bufio.Writer, proto *Proto) (err error) {
if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
return
}
if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
if err = binary.Write(wr, binary.BigEndian, proto.Seq); err != nil {
return
}
if proto.Body != nil {
//log.Debug("cipher body: %v", proto.Body)
if err = binary.Write(wr, binary.BigEndian, proto.Body); err != nil {
return
}
Expand All @@ -215,29 +218,22 @@ func tcpReadProto(rd *bufio.Reader, proto *Proto) (err error) {
if err = binary.Read(rd, binary.BigEndian, &packLen); err != nil {
return
}
//log.Debug("packLen: %d", packLen)
if err = binary.Read(rd, binary.BigEndian, &headerLen); err != nil {
return
}
//log.Debug("headerLen: %d", headerLen)
if err = binary.Read(rd, binary.BigEndian, &proto.Ver); err != nil {
return
}
//log.Debug("ver: %d", proto.Ver)
if err = binary.Read(rd, binary.BigEndian, &proto.Operation); err != nil {
return
}
//log.Debug("operation: %d", proto.Operation)
if err = binary.Read(rd, binary.BigEndian, &proto.SeqId); err != nil {
if err = binary.Read(rd, binary.BigEndian, &proto.Seq); err != nil {
return
}
//log.Debug("seqId: %d", proto.SeqId)
var (
n = int(0)
t = int(0)
n, t int
bodyLen = int(packLen - int32(headerLen))
)
//log.Debug("read body len: %d", bodyLen)
if bodyLen > 0 {
proto.Body = make([]byte, bodyLen)
for {
Expand All @@ -246,8 +242,6 @@ func tcpReadProto(rd *bufio.Reader, proto *Proto) (err error) {
}
if n += t; n == bodyLen {
break
} else if n < bodyLen {
} else {
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/multi_push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
t int
)

const TestContent = "{\"test\":1}"
const testContent = "{\"test\":1}"

type pushsBodyMsg struct {
Msg json.RawMessage `json:"m"`
Expand All @@ -39,7 +39,7 @@ func init() {
return nil, err
}

c.SetDeadline(deadline)
_ = c.SetDeadline(deadline)
return c, nil
},
DisableKeepAlives: false,
Expand Down Expand Up @@ -97,7 +97,7 @@ func startPush(b, e int) {
for i := b; i < e; i++ {
l = append(l, int64(i))
}
msg := &pushsBodyMsg{Msg: json.RawMessage(TestContent), UserIds: l}
msg := &pushsBodyMsg{Msg: json.RawMessage(testContent), UserIds: l}
body, err := json.Marshal(msg)
if err != nil {
panic(err)
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ var (
t int
)

const TestContent = "{\"test\":1}"
const testContent = "{\"test\":1}"

type pushBodyMsg struct {
Msg json.RawMessage `json:"m"`
UserId int64 `json:"u"`
UserID int64 `json:"u"`
}

func init() {
Expand All @@ -39,7 +39,7 @@ func init() {
return nil, err
}

c.SetDeadline(deadline)
_ = c.SetDeadline(deadline)
return c, nil
},
DisableKeepAlives: false,
Expand Down Expand Up @@ -97,7 +97,7 @@ func startPush(b, e int) {
lg.Printf("start Push from %d to %d", b, e)
bodys := make([][]byte, e-b)
for i := 0; i < e-b; i++ {
msg := &pushBodyMsg{Msg: json.RawMessage(TestContent), UserId: int64(b)}
msg := &pushBodyMsg{Msg: json.RawMessage(testContent), UserID: int64(b)}
body, err := json.Marshal(msg)
if err != nil {
panic(err)
Expand Down
32 changes: 3 additions & 29 deletions benchmarks/push_rooms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
Expand All @@ -26,12 +24,7 @@ var (
httpClient *http.Client
)

const TestContent = "{\"test\":1}"

type pushBodyMsg struct {
Msg json.RawMessage `json:"m"`
UserId int64 `json:"u"`
}
const testContent = "{\"test\":1}"

func init() {
httpTransport := &http.Transport{
Expand All @@ -42,7 +35,7 @@ func init() {
return nil, err
}

c.SetDeadline(deadline)
_ = c.SetDeadline(deadline)
return c, nil
},
DisableKeepAlives: false,
Expand Down Expand Up @@ -92,16 +85,12 @@ func main() {
time.Sleep(9999 * time.Hour)
}

func stop() {
os.Exit(-1)
}

func startPush(b, e int, delay time.Duration) {
lg.Printf("start Push from %d to %d", b, e)

for {
for i := b; i < e; i++ {
resp, err := http.Post(fmt.Sprintf("http://%s/1/push/room?rid=%d", os.Args[3], i), "application/json", bytes.NewBufferString(TestContent))
resp, err := http.Post(fmt.Sprintf("http://%s/1/push/room?rid=%d", os.Args[3], i), "application/json", bytes.NewBufferString(testContent))
if err != nil {
lg.Printf("post error (%v)", err)
continue
Expand All @@ -119,18 +108,3 @@ func startPush(b, e int, delay time.Duration) {
}
}
}

func httpPost(url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", contentType)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}
2 changes: 1 addition & 1 deletion internal/comet/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (b *Bucket) Broadcast(p *grpc.Proto, op int32, tag string) {
if !ch.NeedPush(op, tag) {
continue
}
ch.Push(p)
_ = ch.Push(p)
}
b.cLock.RUnlock()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/comet/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *Room) Del(ch *Channel) bool {
func (r *Room) Push(p *grpc.Proto) {
r.rLock.RLock()
for ch := r.next; ch != nil; ch = ch.Next {
ch.Push(p)
_ = ch.Push(p)
}
r.rLock.RUnlock()
}
Expand Down
2 changes: 0 additions & 2 deletions internal/comet/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ type Server struct {

func newLogicClient(c *conf.RPCClient) logic.LogicClient {
opts := []grpc.DialOption{
// grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(time.Second),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.Dial))
defer cancel()
Expand Down
1 change: 0 additions & 1 deletion internal/comet/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func (s *Server) dispatchTCP(conn *net.TCPConn, wr *bufio.Writer, wp *bytes.Pool
// fetch message from svrbox(client send)
for {
if p, err = ch.CliProto.Get(); err != nil {
err = nil // must be empty error
break
}
if white {
Expand Down
Loading

0 comments on commit b034eca

Please sign in to comment.