blob: 467392b8d45556c72e1e19ab2e5b2cd8c45858c1 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
2 * Copyright 2019 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Package buffer provides an implementation of an unbounded buffer.
19package buffer
20
Abhay Kumar03713392025-12-30 05:20:58 +000021import (
22 "errors"
23 "sync"
24)
khenaidoo5fc5cea2021-08-11 17:39:16 -040025
26// Unbounded is an implementation of an unbounded buffer which does not use
27// extra goroutines. This is typically used for passing updates from one entity
28// to another within gRPC.
29//
30// All methods on this type are thread-safe and don't block on anything except
31// the underlying mutex used for synchronization.
32//
33// Unbounded supports values of any type to be stored in it by using a channel
Abhay Kumar03713392025-12-30 05:20:58 +000034// of `any`. This means that a call to Put() incurs an extra memory allocation,
35// and also that users need a type assertion while reading. For performance
36// critical code paths, using Unbounded is strongly discouraged and defining a
37// new type specific implementation of this buffer is preferred. See
khenaidoo5fc5cea2021-08-11 17:39:16 -040038// internal/transport/transport.go for an example of this.
39type Unbounded struct {
Abhay Kumar03713392025-12-30 05:20:58 +000040 c chan any
Akash Kankanala761955c2024-02-21 19:32:20 +053041 closed bool
Abhay Kumar03713392025-12-30 05:20:58 +000042 closing bool
khenaidoo5fc5cea2021-08-11 17:39:16 -040043 mu sync.Mutex
Abhay Kumar03713392025-12-30 05:20:58 +000044 backlog []any
khenaidoo5fc5cea2021-08-11 17:39:16 -040045}
46
47// NewUnbounded returns a new instance of Unbounded.
48func NewUnbounded() *Unbounded {
Abhay Kumar03713392025-12-30 05:20:58 +000049 return &Unbounded{c: make(chan any, 1)}
khenaidoo5fc5cea2021-08-11 17:39:16 -040050}
51
Abhay Kumar03713392025-12-30 05:20:58 +000052var errBufferClosed = errors.New("Put called on closed buffer.Unbounded")
53
khenaidoo5fc5cea2021-08-11 17:39:16 -040054// Put adds t to the unbounded buffer.
Abhay Kumar03713392025-12-30 05:20:58 +000055func (b *Unbounded) Put(t any) error {
khenaidoo5fc5cea2021-08-11 17:39:16 -040056 b.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +053057 defer b.mu.Unlock()
Abhay Kumar03713392025-12-30 05:20:58 +000058 if b.closing {
59 return errBufferClosed
Akash Kankanala761955c2024-02-21 19:32:20 +053060 }
khenaidoo5fc5cea2021-08-11 17:39:16 -040061 if len(b.backlog) == 0 {
62 select {
63 case b.c <- t:
Abhay Kumar03713392025-12-30 05:20:58 +000064 return nil
khenaidoo5fc5cea2021-08-11 17:39:16 -040065 default:
66 }
67 }
68 b.backlog = append(b.backlog, t)
Abhay Kumar03713392025-12-30 05:20:58 +000069 return nil
khenaidoo5fc5cea2021-08-11 17:39:16 -040070}
71
Abhay Kumar03713392025-12-30 05:20:58 +000072// Load sends the earliest buffered data, if any, onto the read channel returned
73// by Get(). Users are expected to call this every time they successfully read a
khenaidoo5fc5cea2021-08-11 17:39:16 -040074// value from the read channel.
75func (b *Unbounded) Load() {
76 b.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +053077 defer b.mu.Unlock()
khenaidoo5fc5cea2021-08-11 17:39:16 -040078 if len(b.backlog) > 0 {
79 select {
80 case b.c <- b.backlog[0]:
81 b.backlog[0] = nil
82 b.backlog = b.backlog[1:]
83 default:
84 }
Abhay Kumar03713392025-12-30 05:20:58 +000085 } else if b.closing && !b.closed {
86 b.closed = true
87 close(b.c)
khenaidoo5fc5cea2021-08-11 17:39:16 -040088 }
khenaidoo5fc5cea2021-08-11 17:39:16 -040089}
90
91// Get returns a read channel on which values added to the buffer, via Put(),
92// are sent on.
93//
94// Upon reading a value from this channel, users are expected to call Load() to
95// send the next buffered value onto the channel if there is any.
Akash Kankanala761955c2024-02-21 19:32:20 +053096//
97// If the unbounded buffer is closed, the read channel returned by this method
Abhay Kumar03713392025-12-30 05:20:58 +000098// is closed after all data is drained.
99func (b *Unbounded) Get() <-chan any {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400100 return b.c
101}
Akash Kankanala761955c2024-02-21 19:32:20 +0530102
Abhay Kumar03713392025-12-30 05:20:58 +0000103// Close closes the unbounded buffer. No subsequent data may be Put(), and the
104// channel returned from Get() will be closed after all the data is read and
105// Load() is called for the final time.
Akash Kankanala761955c2024-02-21 19:32:20 +0530106func (b *Unbounded) Close() {
107 b.mu.Lock()
108 defer b.mu.Unlock()
Abhay Kumar03713392025-12-30 05:20:58 +0000109 if b.closing {
Akash Kankanala761955c2024-02-21 19:32:20 +0530110 return
111 }
Abhay Kumar03713392025-12-30 05:20:58 +0000112 b.closing = true
113 if len(b.backlog) == 0 {
114 b.closed = true
115 close(b.c)
116 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530117}