stream.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package streams
  14. import (
  15. "sync"
  16. streamsv1 "github.com/docker/compose-cli/cli/server/protos/streams/v1"
  17. )
  18. // Stream is a bidirectional stream for container IO
  19. type Stream struct {
  20. streamsv1.Streaming_NewStreamServer
  21. errm sync.Mutex
  22. ErrChan chan<- error
  23. }
  24. // CloseWithError sends the result of an action to the errChan or nil
  25. // if no erros
  26. func (s *Stream) CloseWithError(err error) error {
  27. s.errm.Lock()
  28. defer s.errm.Unlock()
  29. if s.ErrChan != nil {
  30. if err != nil {
  31. s.ErrChan <- err
  32. }
  33. close(s.ErrChan)
  34. s.ErrChan = nil
  35. }
  36. return nil
  37. }