-
Notifications
You must be signed in to change notification settings - Fork 931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ftr: Add dubbo-go-cli telnet tool support #818
Changes from 8 commits
3b9229b
16baeda
63ae531
b99152a
4f2d462
6d5a472
841c4e6
0008fa9
4fe1da9
54d4984
7b7b8a5
34e0999
8f7fc94
62ec58a
b41de25
aebfe94
11fd33b
1599fd4
d4ce11b
53db19c
07754c0
b6022bb
d305b67
93168df
34289f4
1ab564f
5919b11
3dc2f17
55737aa
65cd856
f3ef409
18ab1c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.idea/ | ||
|
||
|
||
# Binary | ||
example/dubbo-go-cli | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# dubbo-go-cli | ||
|
||
### 1. 解决问题 | ||
|
||
针对正在运行的dubbo服务,需要拥有一个命令行工具来针对服务进行测试。 | ||
该服务需要支持dubbo协议,方便用户进行自定义传输包体。 | ||
|
||
### 2. cli工具获取方法 | ||
`sh build.sh` | ||
|
||
### 3. 使用方法:见[example](./example/README.md) | ||
|
||
|
||
|
||
### 第三方依赖(临时兼容) | ||
|
||
github.com/LaurenceLiZhixin/dubbo-go-hessian2 \ | ||
github.com/LaurenceLiZhixin/json-interface-parser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls submit ur code to github.com/dubbogo/gost. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export GOPROXY="http://goproxy.io" | ||
go build -o dubbo-go-cli |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
import ( | ||
"go.uber.org/atomic" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/tools/cli/common" | ||
"github.com/apache/dubbo-go/tools/cli/protocol" | ||
_ "github.com/apache/dubbo-go/tools/cli/protocol/dubbo" | ||
) | ||
|
||
const defaultBufferSize = 4096 | ||
|
||
// TelnetClient maintain a connection to target | ||
type TelnetClient struct { | ||
responseTimeout time.Duration | ||
protocolName string | ||
requestList []*protocol.Request | ||
conn *net.TCPConn | ||
proto protocol.Protocol | ||
|
||
sequence atomic.Uint64 | ||
pendingResponses *sync.Map | ||
waitNum atomic.Uint64 | ||
} | ||
|
||
// NewTelnetClient create a new tcp connection, and create default request | ||
func NewTelnetClient(host string, port int, protocolName, interfaceID, version, group, method string, reqPkg interface{}) (*TelnetClient, error) { | ||
tcpAddr := createTCPAddr(host, port) | ||
resolved := resolveTCPAddr(tcpAddr) | ||
conn, err := net.DialTCP("tcp", nil, resolved) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Printf("connected to %s:%d!\n", host, port) | ||
log.Printf("try calling interface:%s.%s\n", interfaceID, method) | ||
log.Printf("with protocol:%s\n\n", protocolName) | ||
proto := common.GetProtocol(protocolName) | ||
|
||
return &TelnetClient{ | ||
conn: conn, | ||
responseTimeout: 100000000, //default timeout | ||
protocolName: protocolName, | ||
pendingResponses: &sync.Map{}, | ||
proto: proto, | ||
requestList: []*protocol.Request{ | ||
{ | ||
InterfaceID: interfaceID, | ||
Version: version, | ||
Method: method, | ||
Group: group, | ||
Params: []interface{}{reqPkg}, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func createTCPAddr(host string, port int) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls using net.JoinHostPort instead. |
||
var buffer bytes.Buffer | ||
buffer.WriteString(host) | ||
buffer.WriteByte(':') | ||
buffer.WriteString(fmt.Sprintf("%d", port)) | ||
return buffer.String() | ||
} | ||
|
||
func resolveTCPAddr(addr string) *net.TCPAddr { | ||
resolved, error := net.ResolveTCPAddr("tcp", addr) | ||
if nil != error { | ||
log.Fatalf("Error occured while resolving TCP address \"%v\": %v\n", addr, error) | ||
} | ||
|
||
return resolved | ||
} | ||
|
||
// ProcessRequests send all requests | ||
func (t *TelnetClient) ProcessRequests(userPkg interface{}) { | ||
for i, _ := range t.requestList { | ||
t.processSingleRequest(t.requestList[i], userPkg) | ||
} | ||
} | ||
|
||
// addPendingResponse add a response @model to pending queue | ||
// once the rsp got, the model will be used. | ||
func (t *TelnetClient) addPendingResponse(model interface{}) uint64 { | ||
seqId := t.sequence.Load() | ||
t.pendingResponses.Store(seqId, model) | ||
t.waitNum.Inc() | ||
t.sequence.Inc() | ||
return seqId | ||
} | ||
|
||
// removePendingResponse delete item from pending queue by @seq | ||
func (t *TelnetClient) removePendingResponse(seq uint64) { | ||
if t.pendingResponses == nil { | ||
return | ||
} | ||
if _, ok := t.pendingResponses.Load(seq); ok { | ||
t.pendingResponses.Delete(seq) | ||
t.waitNum.Dec() | ||
} | ||
return | ||
} | ||
|
||
// processSingleRequest call one req. | ||
func (t *TelnetClient) processSingleRequest(req *protocol.Request, userPkg interface{}) { | ||
// proto create package procedure | ||
req.ID = t.sequence.Load() | ||
inputData, err := t.proto.Write(req) | ||
if err != nil { | ||
log.Fatalln("error: handler.Writer err = ", err) | ||
} | ||
startTime := time.Now() | ||
|
||
// init rsp Package and add to pending queue | ||
seqId := t.addPendingResponse(userPkg) | ||
defer t.removePendingResponse(seqId) | ||
|
||
requestDataChannel := make(chan []byte) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not need to set its length for this chan? |
||
doneChannel := make(chan bool) | ||
responseDataChannel := make(chan []byte) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same as above |
||
|
||
// start data transfer procedure | ||
go t.readInputData(string(inputData), requestDataChannel, doneChannel) | ||
go t.readServerData(t.conn, responseDataChannel) | ||
|
||
for { | ||
select { | ||
case request := <-requestDataChannel: | ||
if _, err := t.conn.Write(request); nil != err { | ||
log.Fatalf("Error occured while writing to TCP socket: %v\n", err) | ||
} | ||
case response := <-responseDataChannel: | ||
rspPkg, _, err := t.proto.Read(response, t.pendingResponses) | ||
if err != nil { | ||
log.Fatalln("Error with protocol Read(): ", err) | ||
} | ||
t.removePendingResponse(seqId) | ||
log.Printf("After %dms , Got Rsp:", time.Now().Sub(startTime).Milliseconds()) | ||
common.PrintInterface(rspPkg) | ||
if t.waitNum.Sub(0) == 0 { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (t *TelnetClient) readInputData(inputData string, toSent chan<- []byte, doneChannel chan<- bool) { | ||
toSent <- []byte(inputData) | ||
doneChannel <- true | ||
} | ||
|
||
func (t *TelnetClient) readServerData(connection *net.TCPConn, received chan<- []byte) { | ||
buffer := make([]byte, defaultBufferSize) | ||
var err error | ||
var n int | ||
for nil == err { | ||
n, err = connection.Read(buffer) | ||
received <- buffer[:n] | ||
} | ||
|
||
t.assertEOF(err) | ||
} | ||
|
||
func (t *TelnetClient) assertEOF(error error) { | ||
if "EOF" != error.Error() { | ||
log.Fatalf("Error occured while operating on TCP socket: %v\n", error) | ||
} | ||
} | ||
|
||
// Destroy close the tcp conn | ||
func (t *TelnetClient) Destroy() { | ||
t.conn.Close() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/tools/cli/protocol" | ||
) | ||
|
||
var ( | ||
protocols = make(map[string]func() protocol.Protocol) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls set its init length param, such as 8 |
||
) | ||
|
||
// SetProtocol sets the protocol extension with @name | ||
func SetProtocol(name string, v func() protocol.Protocol) { | ||
protocols[name] = v | ||
} | ||
|
||
// GetProtocol finds the protocol extension with @name | ||
func GetProtocol(name string) protocol.Protocol { | ||
if protocols[name] == nil { | ||
panic("protocol for " + name + " is not existing, make sure you have import the package.") | ||
} | ||
return protocols[name]() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
) | ||
|
||
// PrintInterface print the interface by level | ||
func PrintInterface(v interface{}) { | ||
val := reflect.ValueOf(v).Elem() | ||
typ := reflect.TypeOf(v) | ||
log.Printf("%+v\n", v) | ||
nums := val.NumField() | ||
for i := 0; i < nums; i++ { | ||
if typ.Elem().Field(i).Type.Kind() == reflect.Ptr { | ||
log.Printf("%s: ", typ.Elem().Field(i).Name) | ||
PrintInterface(val.Field(i).Interface()) | ||
} | ||
} | ||
fmt.Println("") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey, guy, pls using English in an Apache project. Or u rename this file to readme_cn.md and add an English readme.