optionsserver.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package server
  2. import (
  3. "slowwildws/internal/config"
  4. "slowwildws/internal/constants"
  5. "time"
  6. )
  7. type ServerOptions func(opt *serverOption)
  8. type serverOption struct {
  9. maxConnectionIdle time.Duration // 连接空闲超时时间
  10. ack constants.AckType // ack类型
  11. ackTimeout time.Duration // ack等待超时时间
  12. concurrency int // 并发数
  13. }
  14. func newServerOptions(conf config.Config, opts ...ServerOptions) serverOption {
  15. o := serverOption{
  16. maxConnectionIdle: time.Duration(conf.DefaultMaxConnectionIdleTime) * time.Second,
  17. ackTimeout: time.Duration(conf.DefaultAckTimeout) * time.Second,
  18. concurrency: conf.DefaultConcurrency,
  19. }
  20. for _, opt := range opts {
  21. opt(&o)
  22. }
  23. return o
  24. }
  25. func WithServerAck(ack constants.AckType) ServerOptions {
  26. return func(opt *serverOption) {
  27. opt.ack = ack
  28. }
  29. }
  30. func WithServerMaxConnectionIdle(maxConnectionIdle time.Duration) ServerOptions {
  31. return func(opt *serverOption) {
  32. if maxConnectionIdle > 0 {
  33. opt.maxConnectionIdle = maxConnectionIdle
  34. }
  35. }
  36. }