options.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package pomeloClient
  2. import (
  3. "time"
  4. cfacade "github.com/mhaya/facade"
  5. )
  6. type (
  7. options struct {
  8. serializer cfacade.ISerializer // protocol serializer
  9. heartBeat int // second
  10. requestTimeout time.Duration // Send request timeout
  11. handshake string // handshake content
  12. isErrorBreak bool // an error occurs,is it break
  13. }
  14. Option func(options *options)
  15. // HandshakeSys struct
  16. HandshakeSys struct {
  17. Dict map[string]uint16 `json:"dict"`
  18. Heartbeat int `json:"heartbeat"`
  19. Serializer string `json:"serializer"`
  20. }
  21. // HandshakeData struct
  22. HandshakeData struct {
  23. Code int `json:"code"`
  24. Sys HandshakeSys `json:"sys"`
  25. }
  26. )
  27. func (p *options) Serializer() cfacade.ISerializer {
  28. return p.serializer
  29. }
  30. func WithSerializer(serializer cfacade.ISerializer) Option {
  31. return func(options *options) {
  32. options.serializer = serializer
  33. }
  34. }
  35. func WithHeartbeat(heartBeat int) Option {
  36. return func(options *options) {
  37. options.heartBeat = heartBeat
  38. }
  39. }
  40. func WithRequestTimeout(requestTimeout time.Duration) Option {
  41. return func(options *options) {
  42. options.requestTimeout = requestTimeout
  43. }
  44. }
  45. func WithHandshake(handshake string) Option {
  46. return func(options *options) {
  47. options.handshake = handshake
  48. }
  49. }
  50. func WithErrorBreak(isBreak bool) Option {
  51. return func(options *options) {
  52. options.isErrorBreak = isBreak
  53. }
  54. }