callback_serializer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpcsync
  19. import (
  20. "context"
  21. "sync"
  22. "google.golang.org/grpc/internal/buffer"
  23. )
  24. // CallbackSerializer provides a mechanism to schedule callbacks in a
  25. // synchronized manner. It provides a FIFO guarantee on the order of execution
  26. // of scheduled callbacks. New callbacks can be scheduled by invoking the
  27. // Schedule() method.
  28. //
  29. // This type is safe for concurrent access.
  30. type CallbackSerializer struct {
  31. // done is closed once the serializer is shut down completely, i.e all
  32. // scheduled callbacks are executed and the serializer has deallocated all
  33. // its resources.
  34. done chan struct{}
  35. callbacks *buffer.Unbounded
  36. closedMu sync.Mutex
  37. closed bool
  38. }
  39. // NewCallbackSerializer returns a new CallbackSerializer instance. The provided
  40. // context will be passed to the scheduled callbacks. Users should cancel the
  41. // provided context to shutdown the CallbackSerializer. It is guaranteed that no
  42. // callbacks will be added once this context is canceled, and any pending un-run
  43. // callbacks will be executed before the serializer is shut down.
  44. func NewCallbackSerializer(ctx context.Context) *CallbackSerializer {
  45. cs := &CallbackSerializer{
  46. done: make(chan struct{}),
  47. callbacks: buffer.NewUnbounded(),
  48. }
  49. go cs.run(ctx)
  50. return cs
  51. }
  52. // Schedule adds a callback to be scheduled after existing callbacks are run.
  53. //
  54. // Callbacks are expected to honor the context when performing any blocking
  55. // operations, and should return early when the context is canceled.
  56. //
  57. // Return value indicates if the callback was successfully added to the list of
  58. // callbacks to be executed by the serializer. It is not possible to add
  59. // callbacks once the context passed to NewCallbackSerializer is cancelled.
  60. func (cs *CallbackSerializer) Schedule(f func(ctx context.Context)) bool {
  61. cs.closedMu.Lock()
  62. defer cs.closedMu.Unlock()
  63. if cs.closed {
  64. return false
  65. }
  66. cs.callbacks.Put(f)
  67. return true
  68. }
  69. func (cs *CallbackSerializer) run(ctx context.Context) {
  70. var backlog []func(context.Context)
  71. defer close(cs.done)
  72. for ctx.Err() == nil {
  73. select {
  74. case <-ctx.Done():
  75. // Do nothing here. Next iteration of the for loop will not happen,
  76. // since ctx.Err() would be non-nil.
  77. case callback, ok := <-cs.callbacks.Get():
  78. if !ok {
  79. return
  80. }
  81. cs.callbacks.Load()
  82. callback.(func(ctx context.Context))(ctx)
  83. }
  84. }
  85. // Fetch pending callbacks if any, and execute them before returning from
  86. // this method and closing cs.done.
  87. cs.closedMu.Lock()
  88. cs.closed = true
  89. backlog = cs.fetchPendingCallbacks()
  90. cs.callbacks.Close()
  91. cs.closedMu.Unlock()
  92. for _, b := range backlog {
  93. b(ctx)
  94. }
  95. }
  96. func (cs *CallbackSerializer) fetchPendingCallbacks() []func(context.Context) {
  97. var backlog []func(context.Context)
  98. for {
  99. select {
  100. case b := <-cs.callbacks.Get():
  101. backlog = append(backlog, b.(func(context.Context)))
  102. cs.callbacks.Load()
  103. default:
  104. return backlog
  105. }
  106. }
  107. }
  108. // Done returns a channel that is closed after the context passed to
  109. // NewCallbackSerializer is canceled and all callbacks have been executed.
  110. func (cs *CallbackSerializer) Done() <-chan struct{} {
  111. return cs.done
  112. }