Skip to content

Commit

Permalink
golint code
Browse files Browse the repository at this point in the history
Signed-off-by: jerrylou <gunsluo@gmail.com>
  • Loading branch information
gunsluo committed Mar 9, 2017
1 parent 9efee95 commit d48ebd5
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 64 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#公共代码
[![Build Status](https://travis-ci.org/gunsluo/common.svg?branch=master)](https://travis-ci.org/gunsluo/common) [![Coverage Status](https://coveralls.io/repos/github/gunsluo/common/badge.svg?branch=master)](https://coveralls.io/github/gunsluo/common?branch=master)
[![Build Status](https://travis-ci.org/gunsluo/common.svg?branch=master)](https://travis-ci.org/gunsluo/common) [![Coverage Status](https://coveralls.io/repos/github/gunsluo/common/badge.svg?branch=master)](https://coveralls.io/github/gunsluo/common?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/gunsluo/common)](https://goreportcard.com/report/github.com/gunsluo/common)

##RPC

Expand All @@ -17,7 +17,7 @@ import (

func main() {

client := rpc.NewRPCClient("127.0.0.1:9999", 10)
client := rpc.NewClient("127.0.0.1:9999", 10)

req := new(model.RequestArg)
req.ArgOne = "test"
Expand All @@ -44,8 +44,10 @@ import (
"github.com/gunsluo/common/rpc"
)

// Handle define handle
type Handle int

// Test test is example
func (t *Handle) Test(args *model.RequestArg, reply *model.ResponseArg) error {

fmt.Println("receive:", args)
Expand All @@ -54,14 +56,14 @@ func (t *Handle) Test(args *model.RequestArg, reply *model.ResponseArg) error {
reply.Code = 200
reply.Msg = "Success"

fmt.Println("respone:", reply)
fmt.Println("response:", reply)

return nil
}

func main() {

server := rpc.NewRPCServer("0.0.0.0:9999")
server := rpc.NewServer("0.0.0.0:9999")
server.Register(new(Handle))

server.Run()
Expand Down
2 changes: 1 addition & 1 deletion example/rpc/client/rpc_client_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {

client := rpc.NewRPCClient("127.0.0.1:9999", 10)
client := rpc.NewClient("127.0.0.1:9999", 10)

req := new(model.RequestArg)
req.ArgOne = "test"
Expand Down
6 changes: 4 additions & 2 deletions example/rpc/model/common_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package model

import "fmt"

// CommonRPCResponse RPC Response
type CommonRPCResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

func (this *CommonRPCResponse) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s>", this.Code, this.Msg)
// String RPC Response to string
func (resp *CommonRPCResponse) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s>", resp.Code, resp.Msg)
}
6 changes: 4 additions & 2 deletions example/rpc/model/req_arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package model

import "fmt"

// RequestArg example request
type RequestArg struct {
ArgOne string `json:"arg"`
}

func (this *RequestArg) String() string {
return fmt.Sprintf("<arg: %s>", this.ArgOne)
// String request to string
func (req *RequestArg) String() string {
return fmt.Sprintf("<arg: %s>", req.ArgOne)
}
6 changes: 4 additions & 2 deletions example/rpc/model/resp_arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
)

// ResponseArg response arg example
type ResponseArg struct {
RespArgOne string `json:"resp_arg_one"`
CommonRPCResponse
}

func (this *ResponseArg) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s, ResponseArg: %s>", this.Code, this.Msg, this.RespArgOne)
// String response arg to string
func (resp *ResponseArg) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s, ResponseArg: %s>", resp.Code, resp.Msg, resp.RespArgOne)
}
6 changes: 4 additions & 2 deletions example/rpc/server/rpc_server_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/gunsluo/common/rpc"
)

// Handle define handle
type Handle int

// Test test is example
func (t *Handle) Test(args *model.RequestArg, reply *model.ResponseArg) error {

fmt.Println("receive:", args)
Expand All @@ -17,14 +19,14 @@ func (t *Handle) Test(args *model.RequestArg, reply *model.ResponseArg) error {
reply.Code = 200
reply.Msg = "Success"

fmt.Println("respone:", reply)
fmt.Println("response:", reply)

return nil
}

func main() {

server := rpc.NewRPCServer("0.0.0.0:9999")
server := rpc.NewServer("0.0.0.0:9999")
server.Register(new(Handle))

server.Run()
Expand Down
3 changes: 3 additions & 0 deletions hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
"encoding/hex"
)

// HmacSha1ToHex to hex num
func HmacSha1ToHex(message []byte, secret string) string {
h := hmac.New(sha1.New, []byte(secret))
h.Write(message)
return hex.EncodeToString(h.Sum(nil))
}

// HmacSha1 to byte slice
func HmacSha1(message []byte, secret string) []byte {
h := hmac.New(sha1.New, []byte(secret))
h.Write(message)
return h.Sum(nil)
}

// VerifyHmacSha1 verify hmacsha1 result
func VerifyHmacSha1(message []byte, secret string, signature string) bool {

sign := HmacSha1(message, secret)
Expand Down
6 changes: 4 additions & 2 deletions rpc/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"time"
)

func NewClient(network, address string, timeout time.Duration) (*rpc.Client, error) {
// NewRPCClient new client
func NewRPCClient(network, address string, timeout time.Duration) (*rpc.Client, error) {
conn, err := net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
}
return rpc.NewClient(conn), nil
}

func NewJsonClient(network, address string, timeout time.Duration) (*rpc.Client, error) {
// NewJSONClient new jsonclient
func NewJSONClient(network, address string, timeout time.Duration) (*rpc.Client, error) {
conn, err := net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions rpc/model/common_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package model

import "fmt"

// CommonRPCResponse define common RPC Response
type CommonRPCResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

func (this *CommonRPCResponse) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s>", this.Code, this.Msg)
// String common RPC Response to string
func (resp *CommonRPCResponse) String() string {
return fmt.Sprintf("<Code: %d, Msg: %s>", resp.Code, resp.Msg)
}
3 changes: 2 additions & 1 deletion rpc/model/null_request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package model

type NullRpcRequest struct {
// NullRPCRequest define null RPC Request
type NullRPCRequest struct {
}
50 changes: 27 additions & 23 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,50 @@ import (
"time"
)

type RPCClient struct {
// Client define rpc client
type Client struct {
sync.Mutex
rpcClient *rpc.Client
RpcServer string
rpcServer string
Timeout time.Duration
}

func NewRPCClient(rpcServer string, timeout int) *RPCClient {
client := new(RPCClient)
client.RpcServer = rpcServer
// NewClient new rpc client
func NewClient(rpcServer string, timeout int) *Client {
client := new(Client)
client.rpcServer = rpcServer
client.Timeout = time.Duration(timeout) * time.Second

return client
}

func (this *RPCClient) Close() {
if this.rpcClient != nil {
this.rpcClient.Close()
this.rpcClient = nil
// Close rpc client close
func (client *Client) Close() {
if client.rpcClient != nil {
client.rpcClient.Close()
client.rpcClient = nil
}
}

func (this *RPCClient) insureConn() {
if this.rpcClient != nil {
func (client *Client) insureConn() {
if client.rpcClient != nil {
return
}

var err error
var retry int = 1
retry := 1

for {
if this.rpcClient != nil {
if client.rpcClient != nil {
return
}

this.rpcClient, err = NewJsonClient("tcp", this.RpcServer, this.Timeout)
client.rpcClient, err = NewJSONClient("tcp", client.rpcServer, client.Timeout)
if err == nil {
return
}

log.Printf("dial %s fail: %v", this.RpcServer, err)
log.Printf("dial %s fail: %v", client.rpcServer, err)

if retry > 6 {
retry = 1
Expand All @@ -60,28 +63,29 @@ func (this *RPCClient) insureConn() {
}
}

func (this *RPCClient) Call(method string, args interface{}, reply interface{}) error {
// Call rpc client send msg
func (client *Client) Call(method string, args interface{}, reply interface{}) error {

this.Lock()
defer this.Unlock()
client.Lock()
defer client.Unlock()

this.insureConn()
client.insureConn()

timeout := time.Duration(50 * time.Second)
done := make(chan error)

go func() {
err := this.rpcClient.Call(method, args, reply)
err := client.rpcClient.Call(method, args, reply)
done <- err
}()

select {
case <-time.After(timeout):
log.Printf("[WARN] rpc call timeout %v => %v", this.rpcClient, this.RpcServer)
this.Close()
log.Printf("[WARN] rpc call timeout %v => %v", client.rpcClient, client.rpcServer)
client.Close()
case err := <-done:
if err != nil {
this.Close()
client.Close()
return err
}
}
Expand Down
22 changes: 13 additions & 9 deletions rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import (
"net/rpc/jsonrpc"
)

type RPCServer struct {
// Server rpc server
type Server struct {
server *rpc.Server
Listen string
}

func NewRPCServer(listen string) *RPCServer {
rpcServer := new(RPCServer)
// NewServer new rpc server
func NewServer(listen string) *Server {
rpcServer := new(Server)
rpcServer.Listen = listen
rpcServer.server = rpc.NewServer()

return rpcServer
}

func (this *RPCServer) Run() {
// Run listen rpc server
func (server *Server) Run() {

tcpAddr, err := net.ResolveTCPAddr("tcp", this.Listen)
tcpAddr, err := net.ResolveTCPAddr("tcp", server.Listen)
if err != nil {
panic(err)
}
Expand All @@ -31,7 +34,7 @@ func (this *RPCServer) Run() {
if err != nil {
panic(err)
}
log.Println("rpc listening", this.Listen)
log.Println("rpc listening", server.Listen)

for {
conn, err := listener.Accept()
Expand All @@ -40,10 +43,11 @@ func (this *RPCServer) Run() {
continue
}
// go rpc.ServeConn(conn)
go this.server.ServeCodec(jsonrpc.NewServerCodec(conn))
go server.server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}

func (this *RPCServer) Register(rcvr interface{}) error {
return this.server.Register(rcvr)
// Register rpc server register recipient
func (server *Server) Register(rcvr interface{}) error {
return server.server.Register(rcvr)
}
2 changes: 2 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// ToString read file content from filepath
func ToString(filePath string) (string, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
Expand All @@ -13,6 +14,7 @@ func ToString(filePath string) (string, error) {
return string(b), nil
}

// ToTrimString read file content from filepath and replace space
func ToTrimString(filePath string) (string, error) {
str, err := ToString(filePath)
if err != nil {
Expand Down
Loading

0 comments on commit d48ebd5

Please sign in to comment.