Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force IPv4 with GCP client #623

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions pkg/pipeline/sink/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type SegmentSink struct {

closedSegments chan SegmentUpdate
playlistUpdates chan SegmentUpdate
throttle core.Throttle
done core.Fuse
}

Expand Down Expand Up @@ -103,7 +102,6 @@ func newSegmentSink(u uploader.Uploader, p *config.PipelineConfig, o *config.Seg
openSegmentsStartTime: make(map[string]uint64),
closedSegments: make(chan SegmentUpdate, maxPendingUploads),
playlistUpdates: make(chan SegmentUpdate, maxPendingUploads),
throttle: core.NewThrottle(time.Second * 2),
done: core.NewFuse(),
}

Expand Down Expand Up @@ -183,35 +181,22 @@ func (s *SegmentSink) handlePlaylistUpdates(update SegmentUpdate) error {
<-update.uploadComplete

s.playlistLock.Lock()
defer s.playlistLock.Unlock()

if err := s.playlist.Append(segmentStartTime, duration, update.filename); err != nil {
s.playlistLock.Unlock()
return err
}
if err := s.uploadPlaylist(); err != nil {
s.callbacks.OnError(err)
}
if s.livePlaylist != nil {
if err := s.livePlaylist.Append(segmentStartTime, duration, update.filename); err != nil {
s.playlistLock.Unlock()
return err
}
}
s.playlistLock.Unlock()

// throttle playlist uploads
s.throttle(func() {
s.playlistLock.Lock()
defer s.playlistLock.Unlock()
if s.done.IsBroken() {
return
}

if err := s.uploadPlaylist(); err != nil {
if err := s.uploadLivePlaylist(); err != nil {
s.callbacks.OnError(err)
}
if s.livePlaylist != nil {
if err := s.uploadLivePlaylist(); err != nil {
s.callbacks.OnError(err)
}
}
})
}

return nil
}
Expand Down
32 changes: 28 additions & 4 deletions pkg/pipeline/sink/uploader/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"syscall"
"time"

"cloud.google.com/go/storage"
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"

"github.com/livekit/egress/pkg/errors"
"github.com/livekit/egress/pkg/types"
"github.com/livekit/protocol/livekit"
)
Expand All @@ -42,15 +46,35 @@ func newGCPUploader(conf *livekit.GCPUpload) (uploader, error) {
conf: conf,
}

var err error
var opts []option.ClientOption
if conf.Credentials != "" {
u.client, err = storage.NewClient(context.Background(), option.WithCredentialsJSON([]byte(u.conf.Credentials)))
} else {
u.client, err = storage.NewClient(context.Background())
opts = append(opts, option.WithCredentialsJSON([]byte(conf.Credentials)))
}

// override default transport DialContext
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{
Timeout: time.Second * 30,
KeepAlive: time.Second * 30,
FallbackDelay: -1,
ControlContext: func(ctx context.Context, network, address string, c syscall.RawConn) error {
// force ipv4 to avoid "service not available in your location, forbidden" errors from Google
if network == "tcp6" {
return errors.New("tcp6 disabled")
}
return nil
},
}).DialContext(ctx, network, addr)
}

c, err := storage.NewClient(context.Background(), opts...)
// restore default transport
http.DefaultTransport = defaultTransport
if err != nil {
return nil, err
}
u.client = c

return u, nil
}
Expand Down
Loading