callback_serializer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "google.golang.org/grpc/internal/buffer"
  22. )
  23. // CallbackSerializer provides a mechanism to schedule callbacks in a
  24. // synchronized manner. It provides a FIFO guarantee on the order of execution
  25. // of scheduled callbacks. New callbacks can be scheduled by invoking the
  26. // Schedule() method.
  27. //
  28. // This type is safe for concurrent access.
  29. type CallbackSerializer struct {
  30. // done is closed once the serializer is shut down completely, i.e all
  31. // scheduled callbacks are executed and the serializer has deallocated all
  32. // its resources.
  33. done chan struct{}
  34. callbacks *buffer.Unbounded
  35. }
  36. // NewCallbackSerializer returns a new CallbackSerializer instance. The provided
  37. // context will be passed to the scheduled callbacks. Users should cancel the
  38. // provided context to shutdown the CallbackSerializer. It is guaranteed that no
  39. // callbacks will be added once this context is canceled, and any pending un-run
  40. // callbacks will be executed before the serializer is shut down.
  41. func NewCallbackSerializer(ctx context.Context) *CallbackSerializer {
  42. cs := &CallbackSerializer{
  43. done: make(chan struct{}),
  44. callbacks: buffer.NewUnbounded(),
  45. }
  46. go cs.run(ctx)
  47. return cs
  48. }
  49. // Schedule adds a callback to be scheduled after existing callbacks are run.
  50. //
  51. // Callbacks are expected to honor the context when performing any blocking
  52. // operations, and should return early when the context is canceled.
  53. //
  54. // Return value indicates if the callback was successfully added to the list of
  55. // callbacks to be executed by the serializer. It is not possible to add
  56. // callbacks once the context passed to NewCallbackSerializer is cancelled.
  57. func (cs *CallbackSerializer) Schedule(f func(ctx context.Context)) bool {
  58. return cs.callbacks.Put(f) == nil
  59. }
  60. func (cs *CallbackSerializer) run(ctx context.Context) {
  61. defer close(cs.done)
  62. // TODO: when Go 1.21 is the oldest supported version, this loop and Close
  63. // can be replaced with:
  64. //
  65. // context.AfterFunc(ctx, cs.callbacks.Close)
  66. for ctx.Err() == nil {
  67. select {
  68. case <-ctx.Done():
  69. // Do nothing here. Next iteration of the for loop will not happen,
  70. // since ctx.Err() would be non-nil.
  71. case cb := <-cs.callbacks.Get():
  72. cs.callbacks.Load()
  73. cb.(func(context.Context))(ctx)
  74. }
  75. }
  76. // Close the buffer to prevent new callbacks from being added.
  77. cs.callbacks.Close()
  78. // Run all pending callbacks.
  79. for cb := range cs.callbacks.Get() {
  80. cs.callbacks.Load()
  81. cb.(func(context.Context))(ctx)
  82. }
  83. }
  84. // Done returns a channel that is closed after the context passed to
  85. // NewCallbackSerializer is canceled and all callbacks have been executed.
  86. func (cs *CallbackSerializer) Done() <-chan struct{} {
  87. return cs.done
  88. }