2023-09-19 10:37:37 -04:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2022 gRPC authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpcsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/internal/buffer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CallbackSerializer provides a mechanism to schedule callbacks in a
|
|
|
|
// synchronized manner. It provides a FIFO guarantee on the order of execution
|
|
|
|
// of scheduled callbacks. New callbacks can be scheduled by invoking the
|
|
|
|
// Schedule() method.
|
|
|
|
//
|
|
|
|
// This type is safe for concurrent access.
|
|
|
|
type CallbackSerializer struct {
|
2023-11-01 11:03:44 -04:00
|
|
|
// done is closed once the serializer is shut down completely, i.e all
|
2023-09-19 10:37:37 -04:00
|
|
|
// scheduled callbacks are executed and the serializer has deallocated all
|
|
|
|
// its resources.
|
2023-11-01 11:03:44 -04:00
|
|
|
done chan struct{}
|
2023-09-19 10:37:37 -04:00
|
|
|
|
|
|
|
callbacks *buffer.Unbounded
|
|
|
|
closedMu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCallbackSerializer returns a new CallbackSerializer instance. The provided
|
|
|
|
// context will be passed to the scheduled callbacks. Users should cancel the
|
|
|
|
// provided context to shutdown the CallbackSerializer. It is guaranteed that no
|
|
|
|
// callbacks will be added once this context is canceled, and any pending un-run
|
|
|
|
// callbacks will be executed before the serializer is shut down.
|
|
|
|
func NewCallbackSerializer(ctx context.Context) *CallbackSerializer {
|
2023-11-01 11:03:44 -04:00
|
|
|
cs := &CallbackSerializer{
|
|
|
|
done: make(chan struct{}),
|
2023-09-19 10:37:37 -04:00
|
|
|
callbacks: buffer.NewUnbounded(),
|
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
go cs.run(ctx)
|
|
|
|
return cs
|
2023-09-19 10:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule adds a callback to be scheduled after existing callbacks are run.
|
|
|
|
//
|
|
|
|
// Callbacks are expected to honor the context when performing any blocking
|
|
|
|
// operations, and should return early when the context is canceled.
|
|
|
|
//
|
|
|
|
// Return value indicates if the callback was successfully added to the list of
|
|
|
|
// callbacks to be executed by the serializer. It is not possible to add
|
|
|
|
// callbacks once the context passed to NewCallbackSerializer is cancelled.
|
2023-11-01 11:03:44 -04:00
|
|
|
func (cs *CallbackSerializer) Schedule(f func(ctx context.Context)) bool {
|
|
|
|
cs.closedMu.Lock()
|
|
|
|
defer cs.closedMu.Unlock()
|
2023-09-19 10:37:37 -04:00
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
if cs.closed {
|
2023-09-19 10:37:37 -04:00
|
|
|
return false
|
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
cs.callbacks.Put(f)
|
2023-09-19 10:37:37 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (cs *CallbackSerializer) run(ctx context.Context) {
|
2023-09-19 10:37:37 -04:00
|
|
|
var backlog []func(context.Context)
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
defer close(cs.done)
|
2023-09-19 10:37:37 -04:00
|
|
|
for ctx.Err() == nil {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Do nothing here. Next iteration of the for loop will not happen,
|
|
|
|
// since ctx.Err() would be non-nil.
|
2023-11-01 11:03:44 -04:00
|
|
|
case callback, ok := <-cs.callbacks.Get():
|
2023-09-19 10:37:37 -04:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
cs.callbacks.Load()
|
2023-09-19 10:37:37 -04:00
|
|
|
callback.(func(ctx context.Context))(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch pending callbacks if any, and execute them before returning from
|
2023-11-01 11:03:44 -04:00
|
|
|
// this method and closing cs.done.
|
|
|
|
cs.closedMu.Lock()
|
|
|
|
cs.closed = true
|
|
|
|
backlog = cs.fetchPendingCallbacks()
|
|
|
|
cs.callbacks.Close()
|
|
|
|
cs.closedMu.Unlock()
|
2023-09-19 10:37:37 -04:00
|
|
|
for _, b := range backlog {
|
|
|
|
b(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:03:44 -04:00
|
|
|
func (cs *CallbackSerializer) fetchPendingCallbacks() []func(context.Context) {
|
2023-09-19 10:37:37 -04:00
|
|
|
var backlog []func(context.Context)
|
|
|
|
for {
|
|
|
|
select {
|
2023-11-01 11:03:44 -04:00
|
|
|
case b := <-cs.callbacks.Get():
|
2023-09-19 10:37:37 -04:00
|
|
|
backlog = append(backlog, b.(func(context.Context)))
|
2023-11-01 11:03:44 -04:00
|
|
|
cs.callbacks.Load()
|
2023-09-19 10:37:37 -04:00
|
|
|
default:
|
|
|
|
return backlog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 11:03:44 -04:00
|
|
|
|
|
|
|
// Done returns a channel that is closed after the context passed to
|
|
|
|
// NewCallbackSerializer is canceled and all callbacks have been executed.
|
|
|
|
func (cs *CallbackSerializer) Done() <-chan struct{} {
|
|
|
|
return cs.done
|
|
|
|
}
|