Files
teleport/api/client/streamwatcher.go
T
Andrey Bulgakov ad80fa3e17 Partial cache healthiness (#24080)
* Partial cache healthiness

* IsSupersetOf -> Contains

* NewWatchStatus fixes

* make map[kindSubKind]types.WatchKind with capacity

* fixed TestRootNetwork?

* post-rebase fix

* fixes regarding docs/comments

* const noSubKind = ""

* preallocate slices for validKinds

* DisablePartialHealth

* test fixes

* fixed struct logging field name

* use custom identity in TestEventsPermissionsPartialSuccess

* fixed TestNodeCAFiltering

* don't look at versions in events APIs

* RFD state: implemented
2023-04-15 20:07:43 +00:00

113 lines
2.5 KiB
Go

/*
Copyright 2020 Gravitational, Inc.
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 client
import (
"context"
"sync"
"github.com/gravitational/trace"
"github.com/gravitational/trace/trail"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
)
// NewWatcher returns a new streamWatcher
func (c *Client) NewWatcher(ctx context.Context, watch types.Watch) (types.Watcher, error) {
cancelCtx, cancel := context.WithCancel(ctx)
protoWatch := proto.Watch{
Kinds: watch.Kinds,
AllowPartialSuccess: watch.AllowPartialSuccess,
}
stream, err := c.grpc.WatchEvents(cancelCtx, &protoWatch)
if err != nil {
cancel()
return nil, trail.FromGRPC(err)
}
w := &streamWatcher{
stream: stream,
ctx: cancelCtx,
cancel: cancel,
eventsC: make(chan types.Event),
}
go w.receiveEvents()
return w, nil
}
type streamWatcher struct {
mu sync.RWMutex
stream proto.AuthService_WatchEventsClient
ctx context.Context
cancel context.CancelFunc
eventsC chan types.Event
err error
}
// Error returns the streamWatcher's error
func (w *streamWatcher) Error() error {
w.mu.RLock()
defer w.mu.RUnlock()
if w.err == nil {
return trace.Wrap(w.ctx.Err())
}
return w.err
}
func (w *streamWatcher) closeWithError(err error) {
w.mu.Lock()
defer w.mu.Unlock()
w.Close()
w.err = err
}
// Events returns the streamWatcher's events channel
func (w *streamWatcher) Events() <-chan types.Event {
return w.eventsC
}
func (w *streamWatcher) receiveEvents() {
for {
event, err := w.stream.Recv()
if err != nil {
w.closeWithError(trail.FromGRPC(err))
return
}
out, err := EventFromGRPC(*event)
if err != nil {
w.closeWithError(trail.FromGRPC(err))
return
}
select {
case w.eventsC <- *out:
case <-w.Done():
return
}
}
}
// Done returns a channel that closes once the streamWatcher is Closed
func (w *streamWatcher) Done() <-chan struct{} {
return w.ctx.Done()
}
// Close the streamWatcher
func (w *streamWatcher) Close() error {
w.cancel()
return nil
}