登录
首页 >  Golang >  Go问答

用于测量压缩/解压缩时间的 GRPC 自定义编解码器

来源:stackoverflow

时间:2024-04-10 18:36:34 455浏览 收藏

一分耕耘,一分收获!既然都打开这篇《用于测量压缩/解压缩时间的 GRPC 自定义编解码器》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我想测量压缩和解压缩 grpc 库处理的有效负载的成本/时间。我创建了一些函数,我相信这些函数会给我相当准确的计时(请参阅 measuretimeandcpu ),根据我所读到的内容,我需要创建自己的编解码器,包装默认的 proto 编解码器,以便我可以“覆盖”编组/解组功能:

package codec

import (
    "bytes"
    "compress/gzip"
    "fmt"
    "io/ioutil"

    "google.golang.org/grpc/encoding"
    "log"
    "my/json-over-grpc/pkg/metrics"
)

func init() {
    fmt.println("registering custom codec")
    encoding.registercodec(&timercodec{
        encoding.getcodec("proto"),
    })
}

type timercodec struct {
    encoding.codec
}

func (g *timercodec) marshal(v interface{}) ([]byte, error) {
        fmt.println("g.codec ", g.codec)
        return g.codec.marshal(v)
}

func (g *timercodec) unmarshal(data []byte, v interface{}) error {
    fmt.println("unmarshalling")
    // check if the data is compressed with gzip
    if len(data) >= 2 && data[0] == 0x1f && data[1] == 0x8b {
        var err error
        starttime, startcputime := metrics.measuretimeandcpu(func() {
            var reader *gzip.reader
            reader, err = gzip.newreader(bytes.newreader(data))
            if err != nil {
                return
            }
            defer reader.close()
            uncompressed, _ := ioutil.readall(reader)
            data = uncompressed
        })
        if err != nil {
            return err
        }

        log.printf("decompression wall time: %d, cpu time: %d", starttime, startcputime)
    }

    return g.codec.unmarshal(data, v)
}

func (g *timercodec) name() string {
    return "mytimercodec" // use a unique name for your codec
}

在我的服务器中我正在做

    cdc := &codec.timercodec{
        codec: encoding.getcodec("proto"),
    }
    encoding.registercodec(cdc)

在我的客户上我正在做

    conn, err := grpc.dial(":10000",
        grpc.withtransportcredentials(insecure.newcredentials()),
        grpc.withdefaultcalloptions(grpc.callcontentsubtype("mytimercodec")),
    )

我也尝试过在客户端中进行调用时

    opts := []grpc.CallOption{
        grpc.CallContentSubtype((&codec.TimerCodec{
            Codec: encoding.GetCodec("proto"),
        }).Name()), // Use TimerCodec for message transmission
    }

但我在客户端一直面临的是 panic: 运行时错误: 无效的内存地址或 nil 指针取消引用 来自我的自定义编解码器中的 marshal 函数中的 return g.codec.marshal(v) 行,即 g .codecnil

我正在努力寻找有关执行此操作的方法的文档,所以我怀疑我在某个地方稍微偏离了方向,或者是否有更简单的方法来执行此操作...... 谢谢!


正确答案


“proto”编解码器永远不会注册,因此 encoding.getcodec("proto") 始终返回 nilencoding/proto/proto.go 在自己的 init() 函数中注册此编解码器,因此您需要确保加载此子包。

根据包的组织方式,我建议:

import (
    "fmt"

    "google.golang.org/grpc/encoding"
    "google.golang.org/grpc/encoding/proto"
)

func init() {
    encoding.RegisterCodec(&TimerCodec{
        encoding.GetCodec(proto.Name),
    })
}

type TimerCodec struct {
    encoding.Codec
}

但是,您应该使用内置的 go benchmarking 库,而不是编写自己的计时逻辑:https://pkg.go.dev/testing#hdr-benchmarks

到这里,我们也就讲完了《用于测量压缩/解压缩时间的 GRPC 自定义编解码器》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>