2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/uuid"
|
2020-06-29 10:50:53 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-06-05 16:28:18 +02:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
streamsv1 "github.com/docker/compose-cli/protos/streams/v1"
|
|
|
|
"github.com/docker/compose-cli/server/proxy/streams"
|
2020-06-05 16:28:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p *proxy) NewStream(stream streamsv1.Streaming_NewStreamServer) error {
|
|
|
|
var (
|
|
|
|
ctx = stream.Context()
|
|
|
|
id = uuid.New().String()
|
|
|
|
)
|
|
|
|
md := metadata.New(map[string]string{
|
|
|
|
"id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
// return the id of the stream to the client
|
|
|
|
if err := stream.SendHeader(md); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
errc := make(chan error)
|
|
|
|
|
|
|
|
p.mu.Lock()
|
2020-06-06 22:46:35 +02:00
|
|
|
p.streams[id] = &streams.Stream{
|
2020-06-05 16:28:18 +02:00
|
|
|
Streaming_NewStreamServer: stream,
|
2020-06-06 22:46:35 +02:00
|
|
|
ErrChan: errc,
|
2020-06-05 16:28:18 +02:00
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.mu.Lock()
|
|
|
|
delete(p.streams, id)
|
|
|
|
p.mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
2020-06-29 10:50:53 +02:00
|
|
|
logrus.Debug("client context canceled")
|
2020-06-05 16:28:18 +02:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|