gor 引流测试中间件golang代码添加注释

发布时间:2020-07-01 23:27:35 作者:gcjs10
来源:网络 阅读:1090

package main

import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"github.com/buger/goreplay/proto"
"os"
)

// requestID -> originalToken
var originalTokens map[string][]byte

// originalToken -> replayedToken
var tokenAliases map[string][]byte

func main() {
originalTokens = make(map[string][]byte)
tokenAliases = make(map[string][]byte)

scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
    encoded := scanner.Bytes()
    buf := make([]byte, len(encoded)/2)
    hex.Decode(buf, encoded)

    process(buf)
}

}

func process(buf []byte) {
// First byte indicate payload type, possible values:
// 1 - Request
// 2 - Response
// 3 - ReplayedResponse
payloadType := buf[0]
headerSize := bytes.IndexByte(buf, '\n') + 1
header := buf[:headerSize-1]

// Header contains space separated values of: request type, request id, and request start time (or round-trip time for responses)
meta := bytes.Split(header, []byte(" "))
// For each request you should receive 3 payloads (request, response, replayed response) with same request id
reqID := string(meta[1])
payload := buf[headerSize:]

Debug("Received payload:", string(buf))

switch payloadType {
case '1': // Request
    if bytes.Equal(proto.Path(payload), []byte("/token")) {
        originalTokens[reqID] = []byte{}
        Debug("Found token request:", reqID)
    } else {
        token, vs, _ := proto.PathParam(payload, []byte("token"))//取到生产服务器的token值

        if vs != -1 { // If there is GET token param
            if alias, ok := tokenAliases[string(token)]; ok {  //检查要替换的token值是否存在
                // Rewrite original token to alias
                payload = proto.SetPathParam(payload, []byte("token"), alias)//将生产的token替换成测服的token

                // Copy modified payload to our buffer
                buf = append(buf[:headerSize], payload...)
            }
        }
    }

    // Emitting data back
    os.Stdout.Write(encode(buf))//重写请求准备发往测试服务器
case '2': // Original response
    if _, ok := originalTokens[reqID]; ok {
        // Token is inside response body
        secureToken := proto.Body(payload) //取到生产服务器中返回的token值
        originalTokens[reqID] = secureToken
        Debug("Remember origial token:", string(secureToken))
    }
case '3': // Replayed response
    if originalToken, ok := originalTokens[reqID]; ok {
        delete(originalTokens, reqID)
        secureToken := proto.Body(payload)
        tokenAliases[string(originalToken)] = secureToken//拿到测试服务器的token值用来替换掉正服的token值

        Debug("Create alias for new token token, was:", string(originalToken), "now:", string(secureToken))
    }
}

}

func encode(buf []byte) []byte {
dst := make([]byte, len(buf)*2+1)
hex.Encode(dst, buf)
dst[len(dst)-1] = '\n'

return dst

}

func Debug(args ...interface{}) {
fmt.Fprint(os.Stderr, "[DEBUG][TOKEN-MOD] ")
fmt.Fprintln(os.Stderr, args...)
}

推荐阅读:
  1. 使用 gor 复制真实流量压测ES集群
  2. python怎么安装虚拟环境virtualenv

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

引流测试中间件 olang ola

上一篇:网页代码中的 script type=application/ld+json端代码使用后来干什么?

下一篇:WSFC备份恢复

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》