gsel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gsel provides selector definition and implements.
  7. package gsel
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/net/gsvc"
  11. )
  12. // Builder creates and returns selector in runtime.
  13. type Builder interface {
  14. Name() string
  15. Build() Selector
  16. }
  17. // Selector for service balancer.
  18. type Selector interface {
  19. // Pick selects and returns service.
  20. Pick(ctx context.Context) (node Node, done DoneFunc, err error)
  21. // Update updates services into Selector.
  22. Update(ctx context.Context, nodes Nodes) error
  23. }
  24. // Node is node interface.
  25. type Node interface {
  26. Service() gsvc.Service
  27. Address() string
  28. }
  29. // Nodes contains multiple Node.
  30. type Nodes []Node
  31. // DoneFunc is callback function when RPC invoke done.
  32. type DoneFunc func(ctx context.Context, di DoneInfo)
  33. // DoneInfo contains additional information for done.
  34. type DoneInfo struct {
  35. // Err is the rpc error the RPC finished with. It could be nil.
  36. Err error
  37. // Trailer contains the metadata from the RPC's trailer, if present.
  38. Trailer DoneInfoMD
  39. // BytesSent indicates if any bytes have been sent to the server.
  40. BytesSent bool
  41. // BytesReceived indicates if any byte has been received from the server.
  42. BytesReceived bool
  43. // ServerLoad is the load received from server. It's usually sent as part of
  44. // trailing metadata.
  45. //
  46. // The only supported type now is *orca_v1.LoadReport.
  47. ServerLoad interface{}
  48. }
  49. // DoneInfoMD is a mapping from metadata keys to value array.
  50. // Users should use the following two convenience functions New and Pairs to generate MD.
  51. type DoneInfoMD interface {
  52. // Len returns the number of items in md.
  53. Len() int
  54. // Get obtains the values for a given key.
  55. //
  56. // k is converted to lowercase before searching in md.
  57. Get(k string) []string
  58. // Set sets the value of a given key with a slice of values.
  59. //
  60. // k is converted to lowercase before storing in md.
  61. Set(key string, values ...string)
  62. // Append adds the values to key k, not overwriting what was already stored at
  63. // that key.
  64. //
  65. // k is converted to lowercase before storing in md.
  66. Append(k string, values ...string)
  67. // Delete removes the values for a given key k which is converted to lowercase
  68. // before removing it from md.
  69. Delete(k string)
  70. }
  71. // String formats and returns Nodes as string.
  72. func (ns Nodes) String() string {
  73. var s string
  74. for _, node := range ns {
  75. if s != "" {
  76. s += ","
  77. }
  78. s += node.Address()
  79. }
  80. return s
  81. }