Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2017 gRPC authors.
|
|
|
|
*
|
|
|
|
* 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 grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/balancer"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
"google.golang.org/grpc/resolver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PickFirstBalancerName is the name of the pick_first balancer.
|
|
|
|
const PickFirstBalancerName = "pick_first"
|
|
|
|
|
|
|
|
func newPickfirstBuilder() balancer.Builder {
|
|
|
|
return &pickfirstBuilder{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pickfirstBuilder struct{}
|
|
|
|
|
|
|
|
func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
|
|
|
|
return &pickfirstBalancer{cc: cc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*pickfirstBuilder) Name() string {
|
|
|
|
return PickFirstBalancerName
|
|
|
|
}
|
|
|
|
|
|
|
|
type pickfirstBalancer struct {
|
|
|
|
cc balancer.ClientConn
|
|
|
|
sc balancer.SubConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
|
|
|
|
if err != nil {
|
2019-12-03 08:18:49 -05:00
|
|
|
if grpclog.V(2) {
|
|
|
|
grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
|
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if b.sc == nil {
|
|
|
|
b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{})
|
|
|
|
if err != nil {
|
|
|
|
//TODO(yuxuanli): why not change the cc state to Idle?
|
2019-12-03 08:18:49 -05:00
|
|
|
if grpclog.V(2) {
|
|
|
|
grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
|
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc})
|
|
|
|
b.sc.Connect()
|
|
|
|
} else {
|
|
|
|
b.sc.UpdateAddresses(addrs)
|
|
|
|
b.sc.Connect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
|
2019-12-03 08:18:49 -05:00
|
|
|
if grpclog.V(2) {
|
|
|
|
grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
|
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
if b.sc != sc {
|
2019-12-03 08:18:49 -05:00
|
|
|
if grpclog.V(2) {
|
|
|
|
grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
|
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if s == connectivity.Shutdown {
|
|
|
|
b.sc = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s {
|
|
|
|
case connectivity.Ready, connectivity.Idle:
|
|
|
|
b.cc.UpdateBalancerState(s, &picker{sc: sc})
|
|
|
|
case connectivity.Connecting:
|
|
|
|
b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable})
|
|
|
|
case connectivity.TransientFailure:
|
|
|
|
b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *pickfirstBalancer) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
type picker struct {
|
|
|
|
err error
|
|
|
|
sc balancer.SubConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
|
|
|
|
if p.err != nil {
|
|
|
|
return nil, nil, p.err
|
|
|
|
}
|
|
|
|
return p.sc, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
balancer.Register(newPickfirstBuilder())
|
|
|
|
}
|