client.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package gd_access_log
  2. import (
  3. "context"
  4. "fmt"
  5. "gd_gateway/apis"
  6. "time"
  7. "github.com/smallnest/rpcx/client"
  8. "github.com/smallnest/rpcx/protocol"
  9. "github.com/smallnest/rpcx/share"
  10. )
  11. type GdAccessLogXClient struct {
  12. xcli client.XClient
  13. }
  14. func (a *GdAccessLogXClient) Init(etcdAddrs []string, basePath, servicePath string) {
  15. opt := client.Option{
  16. Retries: 3,
  17. RPCPath: share.DefaultRPCPath,
  18. ConnectTimeout: 10 * time.Second,
  19. SerializeType: protocol.JSON,
  20. CompressType: protocol.None,
  21. BackupLatency: 10 * time.Millisecond,
  22. }
  23. d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
  24. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
  25. }
  26. func (a *GdAccessLogXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort string) {
  27. opt := client.Option{
  28. Retries: 1,
  29. RPCPath: share.DefaultRPCPath,
  30. ConnectTimeout: 10 * time.Second,
  31. SerializeType: protocol.JSON,
  32. CompressType: protocol.None,
  33. BackupLatency: 10 * time.Millisecond,
  34. }
  35. sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%s", k8sServiceName, k8sServicePort), "")
  36. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
  37. }
  38. func (a *GdAccessLogXClient) LogAddAccessLog(ctx context.Context, req *apis.LogAddAccessLogReq) (reply apis.LogAddAccessLogReply, err error) {
  39. err = a.xcli.Call(ctx, "LogAddAccessLog", req, &reply)
  40. return
  41. }
  42. func (a *GdAccessLogXClient) LogAddInvalidAccessLog(ctx context.Context, req *apis.LogAddInvalidAccessLogReq) (reply apis.LogAddInvalidAccessLogReply, err error) {
  43. err = a.xcli.Call(ctx, "LogAddInvalidAccessLog", req, &reply)
  44. return
  45. }