gsel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Build() Selector
  15. }
  16. // Selector for service balancer.
  17. type Selector interface {
  18. // Pick selects and returns service.
  19. Pick(ctx context.Context) (node Node, done DoneFunc, err error)
  20. // Update updates services into Selector.
  21. Update(ctx context.Context, nodes Nodes) error
  22. }
  23. // Node is node interface.
  24. type Node interface {
  25. Service() gsvc.Service
  26. Address() string
  27. }
  28. // Nodes contains multiple Node.
  29. type Nodes []Node
  30. // DoneFunc is callback function when RPC invoke done.
  31. type DoneFunc func(ctx context.Context, di DoneInfo)
  32. // DoneInfo contains additional information for done.
  33. type DoneInfo struct {
  34. // Err is the rpc error the RPC finished with. It could be nil.
  35. Err error
  36. // Trailer contains the metadata from the RPC's trailer, if present.
  37. Trailer DoneInfoMD
  38. // BytesSent indicates if any bytes have been sent to the server.
  39. BytesSent bool
  40. // BytesReceived indicates if any byte has been received from the server.
  41. BytesReceived bool
  42. // ServerLoad is the load received from server. It's usually sent as part of
  43. // trailing metadata.
  44. //
  45. // The only supported type now is *orca_v1.LoadReport.
  46. ServerLoad interface{}
  47. }
  48. // DoneInfoMD is a mapping from metadata keys to value array.
  49. // Users should use the following two convenience functions New and Pairs to generate MD.
  50. type DoneInfoMD interface {
  51. // Len returns the number of items in md.
  52. Len() int
  53. // Get obtains the values for a given key.
  54. //
  55. // k is converted to lowercase before searching in md.
  56. Get(k string) []string
  57. // Set sets the value of a given key with a slice of values.
  58. //
  59. // k is converted to lowercase before storing in md.
  60. Set(key string, values ...string)
  61. // Append adds the values to key k, not overwriting what was already stored at
  62. // that key.
  63. //
  64. // k is converted to lowercase before storing in md.
  65. Append(k string, values ...string)
  66. // Delete removes the values for a given key k which is converted to lowercase
  67. // before removing it from md.
  68. Delete(k string)
  69. }
  70. // String formats and returns Nodes as string.
  71. func (ns Nodes) String() string {
  72. var s string
  73. for _, node := range ns {
  74. if s != "" {
  75. s += ","
  76. }
  77. s += node.Address()
  78. }
  79. return s
  80. }