Hprose 过滤器 - 压缩传输

优质
小牛编辑
130浏览
2023-12-01

上面的例子,我们只使用了一个过滤器。在本例中,我们展示多个过滤器组合使用的效果。

compress_filter.go

  1. package main
  2.  
  3. import (
  4. "compress/gzip"
  5.  
  6. "io/ioutil"
  7.  
  8. "github.com/hprose/hprose-golang/io"
  9. "github.com/hprose/hprose-golang/rpc"
  10. )
  11.  
  12. // CompressFilter ...
  13. type CompressFilter struct{}
  14.  
  15. // InputFilter ...
  16. func (CompressFilter) InputFilter(data []byte, context rpc.Context) []byte {
  17. b := io.NewByteReader(data)
  18. reader, _ := gzip.NewReader(b)
  19. defer reader.Close()
  20. data, _ = ioutil.ReadAll(reader)
  21. return data
  22. }
  23.  
  24. // OutputFilter ...
  25. func (CompressFilter) OutputFilter(data []byte, context rpc.Context) []byte {
  26. b := &io.ByteWriter{}
  27. writer := gzip.NewWriter(b)
  28. writer.Write(data)
  29. writer.Flush()
  30. return b.Bytes()
  31. }

size_filter.go

  1. package main
  2.  
  3. import (
  4. "fmt"
  5.  
  6. "github.com/hprose/hprose-golang/rpc"
  7. )
  8.  
  9. // SizeFilter ...
  10. type SizeFilter struct {
  11. Message string
  12. }
  13.  
  14. // InputFilter ...
  15. func (sf SizeFilter) InputFilter(data []byte, context rpc.Context) []byte {
  16. fmt.Printf("%v input size: %d\r\n", sf.Message, len(data))
  17. return data
  18. }
  19.  
  20. // OutputFilter ...
  21. func (sf SizeFilter) OutputFilter(data []byte, context rpc.Context) []byte {
  22. fmt.Printf("%v output size: %d\r\n", sf.Message, len(data))
  23. return data
  24. }

main.go

  1. package main
  2.  
  3. import (
  4. "fmt"
  5.  
  6. "github.com/hprose/hprose-golang/rpc"
  7. )
  8.  
  9. // TestService is ...
  10. type TestService struct {
  11. Test func([]int) ([]int, error)
  12. }
  13.  
  14. func main() {
  15. server := rpc.NewTCPServer("")
  16. server.AddFunction("test", func(data []int) []int {
  17. return data
  18. }).
  19. AddFilter(
  20. SizeFilter{"Non compressed data on server"},
  21. CompressFilter{},
  22. SizeFilter{"Compressed data on server"},
  23. )
  24. server.Debug = true
  25. server.Handle()
  26. client := rpc.NewClient(server.URI())
  27. client.AddFilter(
  28. SizeFilter{"Non compressed data on client"},
  29. CompressFilter{},
  30. SizeFilter{"Compressed data on client"},
  31. )
  32. var testService *TestService
  33. client.UseService(&testService)
  34. args := make([]int, 100000)
  35. for i := range args {
  36. args[i] = i
  37. }
  38. result, err := testService.Test(args)
  39. fmt.Println(len(result), err)
  40. client.Close()
  41. server.Close()
  42. }

运行该程序,你会看到运行结果:


  1. Non compressed data on client output size: 688893
  2. Compressed data on client output size: 213244
  3. Compressed data on server input size: 213244
  4. Non compressed data on server input size: 688893
  5. Non compressed data on server output size: 688881
  6. Compressed data on server output size: 213223
  7. Compressed data on client input size: 213223
  8. Non compressed data on client input size: 688881
  9. 100000 <nil>

加密跟这个类似,这里就不再单独举加密的例子了。