<commit_before>/*
Copyright 2017 The Kubernetes 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 csicommon

import (
	"context"
	"fmt"
	"os"
	"runtime/debug"
	"strings"
	"sync/atomic"

	"github.com/ceph/ceph-csi/internal/util"
	"github.com/ceph/ceph-csi/internal/util/log"

	"github.com/container-storage-interface/spec/lib/go/csi"
	rp "github.com/csi-addons/replication-lib-utils/protosanitizer"
	"github.com/csi-addons/spec/lib/go/replication"
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
	"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"k8s.io/klog/v2"
	"k8s.io/kubernetes/pkg/volume"
	mount "k8s.io/mount-utils"
)

func parseEndpoint(ep string) (string, string, error) {
	if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
		s := strings.SplitN(ep, "://", 2)
		if s[1] != "" {
			return s[0], s[1], nil
		}
	}

	return "", "", fmt.Errorf("invalid endpoint: %v", ep)
}

// NewVolumeCapabilityAccessMode returns volume access mode.
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
	return &csi.VolumeCapability_AccessMode{Mode: mode}
}

// NewDefaultNodeServer initializes default node server.
func NewDefaultNodeServer(d *CSIDriver, t string, topology map[string]string) *DefaultNodeServer {
	d.topology = topology

	return &DefaultNodeServer{
		Driver:  d,
		Type:    t,
		Mounter: mount.New(""),
	}
}

// NewDefaultIdentityServer initializes default identity server.
func NewDefaultIdentityServer(d *CSIDriver) *DefaultIdentityServer {
	return &DefaultIdentityServer{
		Driver: d,
	}
}

// NewDefaultControllerServer initializes default controller server.
func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
	return &DefaultControllerServer{
		Driver: d,
	}
}

// NewControllerServiceCapability returns controller capabilities.
func NewControllerServiceCapability(ctrlCap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
	return &csi.ControllerServiceCapability{
		Type: &csi.ControllerServiceCapability_Rpc{
			Rpc: &csi.ControllerServiceCapability_RPC{
				Type: ctrlCap,
			},
		},
	}
}

// Add replication request names to the list when we implement more API's.
func isReplicationRequest(req interface{}) bool {
	isReplicationRequest := true
	switch req.(type) {
	case *replication.EnableVolumeReplicationRequest:
	case *replication.DisableVolumeReplicationRequest:
	case *replication.PromoteVolumeRequest:
	case *replication.DemoteVolumeRequest:
	case *replication.ResyncVolumeRequest:
	default:
		isReplicationRequest = false
	}

	return isReplicationRequest
}

// NewMiddlewareServerOption creates a new grpc.ServerOption that configures a
// common format for log messages and other gRPC related handlers.
func NewMiddlewareServerOption(withMetrics bool) grpc.ServerOption {
	middleWare := []grpc.UnaryServerInterceptor{contextIDInjector, logGRPC, panicHandler}

	if withMetrics {
		middleWare = append(middleWare, grpc_prometheus.UnaryServerInterceptor)
	}

	return grpc_middleware.WithUnaryServerChain(middleWare...)
}

func getReqID(req interface{}) string {
	// if req is nil empty string will be returned
	reqID := ""
	switch r := req.(type) {
	case *csi.CreateVolumeRequest:
		reqID = r.Name

	case *csi.DeleteVolumeRequest:
		reqID = r.VolumeId

	case *csi.CreateSnapshotRequest:
		reqID = r.Name
	case *csi.DeleteSnapshotRequest:
		reqID = r.SnapshotId

	case *csi.ControllerExpandVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodeStageVolumeRequest:
		reqID = r.VolumeId
	case *csi.NodeUnstageVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodePublishVolumeRequest:
		reqID = r.VolumeId
	case *csi.NodeUnpublishVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodeExpandVolumeRequest:
		reqID = r.VolumeId

	case *replication.EnableVolumeReplicationRequest:
		reqID = r.VolumeId
	case *replication.DisableVolumeReplicationRequest:
		reqID = r.VolumeId

	case *replication.PromoteVolumeRequest:
		reqID = r.VolumeId
	case *replication.DemoteVolumeRequest:
		reqID = r.VolumeId

	case *replication.ResyncVolumeRequest:
		reqID = r.VolumeId
	}

	return reqID
}

var id uint64

func contextIDInjector(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (interface{}, error) {
	atomic.AddUint64(&id, 1)
	ctx = context.WithValue(ctx, log.CtxKey, id)
	if reqID := getReqID(req); reqID != "" {
		ctx = context.WithValue(ctx, log.ReqID, reqID)
	}

	return handler(ctx, req)
}

func logGRPC(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (interface{}, error) {
	log.ExtendedLog(ctx, "GRPC call: %s", info.FullMethod)
	if isReplicationRequest(req) {
		log.TraceLog(ctx, "GRPC request: %s", rp.StripReplicationSecrets(req))
	} else {
		log.TraceLog(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
	}

	resp, err := handler(ctx, req)
	if err != nil {
		klog.Errorf(log.Log(ctx, "GRPC error: %v"), err)
	} else {
		log.TraceLog(ctx, "GRPC response: %s", protosanitizer.StripSecrets(resp))
	}

	return resp, err
}

//nolint:nonamedreturns // named return used to send recovered panic error.
func panicHandler(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (resp interface{}, err error) {
	defer func() {
		if r := recover(); r != nil {
			klog.Errorf("panic occurred: %v", r)
			debug.PrintStack()
			err = status.Errorf(codes.Internal, "panic %v", r)
		}
	}()

	return handler(ctx, req)
}

// FilesystemNodeGetVolumeStats can be used for getting the metrics as
// requested by the NodeGetVolumeStats CSI procedure.
// It is shared for FileMode volumes, both the CephFS and RBD NodeServers call
// this.
func FilesystemNodeGetVolumeStats(
	ctx context.Context,
	mounter mount.Interface,
	targetPath string,
) (*csi.NodeGetVolumeStatsResponse, error) {
	isMnt, err := util.IsMountPoint(mounter, targetPath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, status.Errorf(codes.InvalidArgument, "targetpath %s does not exist", targetPath)
		}

		return nil, status.Error(codes.Internal, err.Error())
	}
	if !isMnt {
		return nil, status.Errorf(codes.InvalidArgument, "targetpath %s is not mounted", targetPath)
	}

	cephMetricsProvider := volume.NewMetricsStatFS(targetPath)
	volMetrics, volMetErr := cephMetricsProvider.GetMetrics()
	if volMetErr != nil {
		return nil, status.Error(codes.Internal, volMetErr.Error())
	}

	available, ok := (*(volMetrics.Available)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch available bytes")
	}
	capacity, ok := (*(volMetrics.Capacity)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch capacity bytes")

		return nil, status.Error(codes.Unknown, "failed to fetch capacity bytes")
	}
	used, ok := (*(volMetrics.Used)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch used bytes")
	}
	inodes, ok := (*(volMetrics.Inodes)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch available inodes")

		return nil, status.Error(codes.Unknown, "failed to fetch available inodes")
	}
	inodesFree, ok := (*(volMetrics.InodesFree)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch free inodes")
	}

	inodesUsed, ok := (*(volMetrics.InodesUsed)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch used inodes")
	}

	return &csi.NodeGetVolumeStatsResponse{
		Usage: []*csi.VolumeUsage{
			{
				Available: requirePositive(available),
				Total:     requirePositive(capacity),
				Used:      requirePositive(used),
				Unit:      csi.VolumeUsage_BYTES,
			},
			{
				Available: requirePositive(inodesFree),
				Total:     requirePositive(inodes),
				Used:      requirePositive(inodesUsed),
				Unit:      csi.VolumeUsage_INODES,
			},
		},
	}, nil
}

// requirePositive returns the value for `x` when it is greater or equal to 0,
// or returns 0 in the acse `x` is negative.
//
// This is used for VolumeUsage entries in the NodeGetVolumeStatsResponse. The
// CSI spec does not allow negative values in the VolumeUsage objects.
func requirePositive(x int64) int64 {
	if x >= 0 {
		return x
	}

	return 0
}

// IsBlockMultiNode checks the volume capabilities for BlockMode and MultiNode.
func IsBlockMultiNode(caps []*csi.VolumeCapability) (bool, bool) {
	isMultiNode := false
	isBlock := false
	for _, capability := range caps {
		if capability.GetAccessMode().GetMode() == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER {
			isMultiNode = true
		}
		if capability.GetBlock() != nil {
			isBlock = true
		}
	}

	return isBlock, isMultiNode
}

// IsFileRWO checks if it is of type RWO and file mode, if it is return value
// will be set to true.
func IsFileRWO(caps []*csi.VolumeCapability) bool {
	// the return value has been set to true, if the volume is of file mode and if the capabilities are of RWO
	// kind, ie SINGLE NODE but flexible to have one or more writers. This is also used as a validation in caller
	// to preserve the backward compatibility we had with file mode RWO volumes.

	// to preserve backward compatibility we allow RWO filemode, ideally SINGLE_NODE_WRITER check is good enough,
	// however more granular level check could help us in future, so keeping it here as an additional measure.
	for _, cap := range caps {
		if cap.AccessMode != nil {
			if cap.GetMount() != nil {
				switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
				case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
					csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER,
					csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER:
					return true
				}
			}
		}
	}

	return false
}

// IsReaderOnly check and set return value true only when the access mode is `READER ONLY` regardless of file
// or block mode.
func IsReaderOnly(caps []*csi.VolumeCapability) bool {
	for _, cap := range caps {
		if cap.AccessMode != nil {
			switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
			case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
				csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
				return true
			}
		}
	}

	return false
}

// IsBlockMultiWriter validates the volume capability slice against the access modes and access type.
// if the capability is of multi write the first return value will be set to true and if the request
// is of type block, the second return value will be set to true.
func IsBlockMultiWriter(caps []*csi.VolumeCapability) (bool, bool) {
	// multiWriter has been set and returned after validating multi writer caps regardless of
	// single or multi node access mode. The caps check is agnostic to whether it is a filesystem or block
	// mode volume.
	var multiWriter bool

	// block has been set and returned if the passed in capability is of block volume mode.
	var block bool

	for _, cap := range caps {
		if cap.AccessMode != nil {
			switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
			case csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
				csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER:
				multiWriter = true
			}
		}
		if cap.GetBlock() != nil {
			block = true
		}
	}

	return multiWriter, block
}
<commit_msg>util: use k8s.io/mount-utils/NewWithoutSystemd() to prevent logging<commit_after>/*
Copyright 2017 The Kubernetes 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 csicommon

import (
	"context"
	"fmt"
	"os"
	"runtime/debug"
	"strings"
	"sync/atomic"

	"github.com/ceph/ceph-csi/internal/util"
	"github.com/ceph/ceph-csi/internal/util/log"

	"github.com/container-storage-interface/spec/lib/go/csi"
	rp "github.com/csi-addons/replication-lib-utils/protosanitizer"
	"github.com/csi-addons/spec/lib/go/replication"
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
	"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"k8s.io/klog/v2"
	"k8s.io/kubernetes/pkg/volume"
	mount "k8s.io/mount-utils"
)

func parseEndpoint(ep string) (string, string, error) {
	if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
		s := strings.SplitN(ep, "://", 2)
		if s[1] != "" {
			return s[0], s[1], nil
		}
	}

	return "", "", fmt.Errorf("invalid endpoint: %v", ep)
}

// NewVolumeCapabilityAccessMode returns volume access mode.
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
	return &csi.VolumeCapability_AccessMode{Mode: mode}
}

// NewDefaultNodeServer initializes default node server.
func NewDefaultNodeServer(d *CSIDriver, t string, topology map[string]string) *DefaultNodeServer {
	d.topology = topology

	return &DefaultNodeServer{
		Driver:  d,
		Type:    t,
		Mounter: mount.NewWithoutSystemd(""),
	}
}

// NewDefaultIdentityServer initializes default identity server.
func NewDefaultIdentityServer(d *CSIDriver) *DefaultIdentityServer {
	return &DefaultIdentityServer{
		Driver: d,
	}
}

// NewDefaultControllerServer initializes default controller server.
func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
	return &DefaultControllerServer{
		Driver: d,
	}
}

// NewControllerServiceCapability returns controller capabilities.
func NewControllerServiceCapability(ctrlCap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
	return &csi.ControllerServiceCapability{
		Type: &csi.ControllerServiceCapability_Rpc{
			Rpc: &csi.ControllerServiceCapability_RPC{
				Type: ctrlCap,
			},
		},
	}
}

// Add replication request names to the list when we implement more API's.
func isReplicationRequest(req interface{}) bool {
	isReplicationRequest := true
	switch req.(type) {
	case *replication.EnableVolumeReplicationRequest:
	case *replication.DisableVolumeReplicationRequest:
	case *replication.PromoteVolumeRequest:
	case *replication.DemoteVolumeRequest:
	case *replication.ResyncVolumeRequest:
	default:
		isReplicationRequest = false
	}

	return isReplicationRequest
}

// NewMiddlewareServerOption creates a new grpc.ServerOption that configures a
// common format for log messages and other gRPC related handlers.
func NewMiddlewareServerOption(withMetrics bool) grpc.ServerOption {
	middleWare := []grpc.UnaryServerInterceptor{contextIDInjector, logGRPC, panicHandler}

	if withMetrics {
		middleWare = append(middleWare, grpc_prometheus.UnaryServerInterceptor)
	}

	return grpc_middleware.WithUnaryServerChain(middleWare...)
}

func getReqID(req interface{}) string {
	// if req is nil empty string will be returned
	reqID := ""
	switch r := req.(type) {
	case *csi.CreateVolumeRequest:
		reqID = r.Name

	case *csi.DeleteVolumeRequest:
		reqID = r.VolumeId

	case *csi.CreateSnapshotRequest:
		reqID = r.Name
	case *csi.DeleteSnapshotRequest:
		reqID = r.SnapshotId

	case *csi.ControllerExpandVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodeStageVolumeRequest:
		reqID = r.VolumeId
	case *csi.NodeUnstageVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodePublishVolumeRequest:
		reqID = r.VolumeId
	case *csi.NodeUnpublishVolumeRequest:
		reqID = r.VolumeId

	case *csi.NodeExpandVolumeRequest:
		reqID = r.VolumeId

	case *replication.EnableVolumeReplicationRequest:
		reqID = r.VolumeId
	case *replication.DisableVolumeReplicationRequest:
		reqID = r.VolumeId

	case *replication.PromoteVolumeRequest:
		reqID = r.VolumeId
	case *replication.DemoteVolumeRequest:
		reqID = r.VolumeId

	case *replication.ResyncVolumeRequest:
		reqID = r.VolumeId
	}

	return reqID
}

var id uint64

func contextIDInjector(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (interface{}, error) {
	atomic.AddUint64(&id, 1)
	ctx = context.WithValue(ctx, log.CtxKey, id)
	if reqID := getReqID(req); reqID != "" {
		ctx = context.WithValue(ctx, log.ReqID, reqID)
	}

	return handler(ctx, req)
}

func logGRPC(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (interface{}, error) {
	log.ExtendedLog(ctx, "GRPC call: %s", info.FullMethod)
	if isReplicationRequest(req) {
		log.TraceLog(ctx, "GRPC request: %s", rp.StripReplicationSecrets(req))
	} else {
		log.TraceLog(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
	}

	resp, err := handler(ctx, req)
	if err != nil {
		klog.Errorf(log.Log(ctx, "GRPC error: %v"), err)
	} else {
		log.TraceLog(ctx, "GRPC response: %s", protosanitizer.StripSecrets(resp))
	}

	return resp, err
}

//nolint:nonamedreturns // named return used to send recovered panic error.
func panicHandler(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (resp interface{}, err error) {
	defer func() {
		if r := recover(); r != nil {
			klog.Errorf("panic occurred: %v", r)
			debug.PrintStack()
			err = status.Errorf(codes.Internal, "panic %v", r)
		}
	}()

	return handler(ctx, req)
}

// FilesystemNodeGetVolumeStats can be used for getting the metrics as
// requested by the NodeGetVolumeStats CSI procedure.
// It is shared for FileMode volumes, both the CephFS and RBD NodeServers call
// this.
func FilesystemNodeGetVolumeStats(
	ctx context.Context,
	mounter mount.Interface,
	targetPath string,
) (*csi.NodeGetVolumeStatsResponse, error) {
	isMnt, err := util.IsMountPoint(mounter, targetPath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, status.Errorf(codes.InvalidArgument, "targetpath %s does not exist", targetPath)
		}

		return nil, status.Error(codes.Internal, err.Error())
	}
	if !isMnt {
		return nil, status.Errorf(codes.InvalidArgument, "targetpath %s is not mounted", targetPath)
	}

	cephMetricsProvider := volume.NewMetricsStatFS(targetPath)
	volMetrics, volMetErr := cephMetricsProvider.GetMetrics()
	if volMetErr != nil {
		return nil, status.Error(codes.Internal, volMetErr.Error())
	}

	available, ok := (*(volMetrics.Available)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch available bytes")
	}
	capacity, ok := (*(volMetrics.Capacity)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch capacity bytes")

		return nil, status.Error(codes.Unknown, "failed to fetch capacity bytes")
	}
	used, ok := (*(volMetrics.Used)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch used bytes")
	}
	inodes, ok := (*(volMetrics.Inodes)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch available inodes")

		return nil, status.Error(codes.Unknown, "failed to fetch available inodes")
	}
	inodesFree, ok := (*(volMetrics.InodesFree)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch free inodes")
	}

	inodesUsed, ok := (*(volMetrics.InodesUsed)).AsInt64()
	if !ok {
		log.ErrorLog(ctx, "failed to fetch used inodes")
	}

	return &csi.NodeGetVolumeStatsResponse{
		Usage: []*csi.VolumeUsage{
			{
				Available: requirePositive(available),
				Total:     requirePositive(capacity),
				Used:      requirePositive(used),
				Unit:      csi.VolumeUsage_BYTES,
			},
			{
				Available: requirePositive(inodesFree),
				Total:     requirePositive(inodes),
				Used:      requirePositive(inodesUsed),
				Unit:      csi.VolumeUsage_INODES,
			},
		},
	}, nil
}

// requirePositive returns the value for `x` when it is greater or equal to 0,
// or returns 0 in the acse `x` is negative.
//
// This is used for VolumeUsage entries in the NodeGetVolumeStatsResponse. The
// CSI spec does not allow negative values in the VolumeUsage objects.
func requirePositive(x int64) int64 {
	if x >= 0 {
		return x
	}

	return 0
}

// IsBlockMultiNode checks the volume capabilities for BlockMode and MultiNode.
func IsBlockMultiNode(caps []*csi.VolumeCapability) (bool, bool) {
	isMultiNode := false
	isBlock := false
	for _, capability := range caps {
		if capability.GetAccessMode().GetMode() == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER {
			isMultiNode = true
		}
		if capability.GetBlock() != nil {
			isBlock = true
		}
	}

	return isBlock, isMultiNode
}

// IsFileRWO checks if it is of type RWO and file mode, if it is return value
// will be set to true.
func IsFileRWO(caps []*csi.VolumeCapability) bool {
	// the return value has been set to true, if the volume is of file mode and if the capabilities are of RWO
	// kind, ie SINGLE NODE but flexible to have one or more writers. This is also used as a validation in caller
	// to preserve the backward compatibility we had with file mode RWO volumes.

	// to preserve backward compatibility we allow RWO filemode, ideally SINGLE_NODE_WRITER check is good enough,
	// however more granular level check could help us in future, so keeping it here as an additional measure.
	for _, cap := range caps {
		if cap.AccessMode != nil {
			if cap.GetMount() != nil {
				switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
				case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
					csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER,
					csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER:
					return true
				}
			}
		}
	}

	return false
}

// IsReaderOnly check and set return value true only when the access mode is `READER ONLY` regardless of file
// or block mode.
func IsReaderOnly(caps []*csi.VolumeCapability) bool {
	for _, cap := range caps {
		if cap.AccessMode != nil {
			switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
			case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
				csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
				return true
			}
		}
	}

	return false
}

// IsBlockMultiWriter validates the volume capability slice against the access modes and access type.
// if the capability is of multi write the first return value will be set to true and if the request
// is of type block, the second return value will be set to true.
func IsBlockMultiWriter(caps []*csi.VolumeCapability) (bool, bool) {
	// multiWriter has been set and returned after validating multi writer caps regardless of
	// single or multi node access mode. The caps check is agnostic to whether it is a filesystem or block
	// mode volume.
	var multiWriter bool

	// block has been set and returned if the passed in capability is of block volume mode.
	var block bool

	for _, cap := range caps {
		if cap.AccessMode != nil {
			switch cap.AccessMode.Mode { //nolint:exhaustive // only check what we want
			case csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
				csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER:
				multiWriter = true
			}
		}
		if cap.GetBlock() != nil {
			block = true
		}
	}

	return multiWriter, block
}
<|endoftext|>


<commit_before>// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package legacy

import (
	"fmt"
	"reflect"
	"sync"

	papi "github.com/golang/protobuf/protoapi"
	ptag "github.com/golang/protobuf/v2/internal/encoding/tag"
	pimpl "github.com/golang/protobuf/v2/internal/impl"
	pfmt "github.com/golang/protobuf/v2/internal/typefmt"
	pvalue "github.com/golang/protobuf/v2/internal/value"
	pref "github.com/golang/protobuf/v2/reflect/protoreflect"
	ptype "github.com/golang/protobuf/v2/reflect/prototype"
)

// extensionDescKey is a comparable version of protoapi.ExtensionDesc
// suitable for use as a key in a map.
type extensionDescKey struct {
	typeV2        pref.ExtensionType
	extendedType  reflect.Type
	extensionType reflect.Type
	field         int32
	name          string
	tag           string
	filename      string
}

func extensionDescKeyOf(d *papi.ExtensionDesc) extensionDescKey {
	return extensionDescKey{
		d.Type,
		reflect.TypeOf(d.ExtendedType),
		reflect.TypeOf(d.ExtensionType),
		d.Field, d.Name, d.Tag, d.Filename,
	}
}

var (
	extensionTypeCache sync.Map // map[extensionDescKey]protoreflect.ExtensionType
	extensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoapi.ExtensionDesc
)

// extensionDescFromType converts a v2 protoreflect.ExtensionType to a
// v1 protoapi.ExtensionDesc. The returned ExtensionDesc must not be mutated.
func extensionDescFromType(t pref.ExtensionType) *papi.ExtensionDesc {
	// Fast-path: check the cache for whether this ExtensionType has already
	// been converted to a legacy descriptor.
	if d, ok := extensionDescCache.Load(t); ok {
		return d.(*papi.ExtensionDesc)
	}

	// Determine the parent type if possible.
	var parent papi.Message
	if mt, ok := t.ExtendedType().(pref.MessageType); ok {
		// Create a new parent message and unwrap it if possible.
		mv := mt.New()
		t := reflect.TypeOf(mv)
		if mv, ok := mv.(pvalue.Unwrapper); ok {
			t = reflect.TypeOf(mv.ProtoUnwrap())
		}

		// Check whether the message implements the legacy v1 Message interface.
		mz := reflect.Zero(t).Interface()
		if mz, ok := mz.(papi.Message); ok {
			parent = mz
		}
	}

	// Determine the v1 extension type, which is unfortunately not the same as
	// the v2 ExtensionType.GoType.
	extType := t.GoType()
	switch extType.Kind() {
	case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
		extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
	case reflect.Ptr:
		if extType.Elem().Kind() == reflect.Slice {
			extType = extType.Elem() // *[]T -> []T for repeated fields
		}
	}

	// Reconstruct the legacy enum full name, which is an odd mixture of the
	// proto package name with the Go type name.
	var enumName string
	if t.Kind() == pref.EnumKind {
		// Derive Go type name.
		// For legacy enums, unwrap the wrapper to get the underlying Go type.
		et := t.EnumType().(pref.EnumType)
		var ev interface{} = et.New(0)
		if u, ok := ev.(pvalue.Unwrapper); ok {
			ev = u.ProtoUnwrap()
		}
		enumName = reflect.TypeOf(ev).Name()

		// Derive the proto package name.
		// For legacy enums, obtain the proto package from the raw descriptor.
		var protoPkg string
		if fd := parentFileDescriptor(et); fd != nil {
			protoPkg = string(fd.Package())
		}
		if ed, ok := ev.(enumV1); ok && protoPkg == "" {
			b, _ := ed.EnumDescriptor()
			protoPkg = loadFileDesc(b).GetPackage()
		}

		if protoPkg != "" {
			enumName = protoPkg + "." + enumName
		}
	}

	// Derive the proto file that the extension was declared within.
	var filename string
	if fd := parentFileDescriptor(t); fd != nil {
		filename = fd.Path()
	}

	// Construct and return a v1 ExtensionDesc.
	d := &papi.ExtensionDesc{
		Type:          t,
		ExtendedType:  parent,
		ExtensionType: reflect.Zero(extType).Interface(),
		Field:         int32(t.Number()),
		Name:          string(t.FullName()),
		Tag:           ptag.Marshal(t, enumName),
		Filename:      filename,
	}
	extensionDescCache.Store(t, d)
	return d
}

// extensionTypeFromDesc converts a v1 protoapi.ExtensionDesc to a
// v2 protoreflect.ExtensionType. The returned descriptor type takes ownership
// of the input extension desc. The input must not be mutated so long as the
// returned type is still in use.
func extensionTypeFromDesc(d *papi.ExtensionDesc) pref.ExtensionType {
	// Fast-path: check whether an extension type is already nested within.
	if d.Type != nil {
		// Cache descriptor for future extensionDescFromType operation.
		// This assumes that there is only one legacy protoapi.ExtensionDesc
		// that wraps any given specific protoreflect.ExtensionType.
		extensionDescCache.LoadOrStore(d.Type, d)
		return d.Type
	}

	// Fast-path: check the cache for whether this ExtensionType has already
	// been converted from a legacy descriptor.
	dk := extensionDescKeyOf(d)
	if t, ok := extensionTypeCache.Load(dk); ok {
		return t.(pref.ExtensionType)
	}

	// Derive basic field information from the struct tag.
	t := reflect.TypeOf(d.ExtensionType)
	isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
	isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
	if isOptional || isRepeated {
		t = t.Elem()
	}
	f := ptag.Unmarshal(d.Tag, t)

	// Construct a v2 ExtensionType.
	conv := pvalue.NewLegacyConverter(t, f.Kind, Export{})
	xd, err := ptype.NewExtension(&ptype.StandaloneExtension{
		FullName:     pref.FullName(d.Name),
		Number:       pref.FieldNumber(d.Field),
		Cardinality:  f.Cardinality,
		Kind:         f.Kind,
		Default:      f.Default,
		Options:      f.Options,
		EnumType:     conv.EnumType,
		MessageType:  conv.MessageType,
		ExtendedType: Export{}.MessageTypeOf(d.ExtendedType),
	})
	if err != nil {
		panic(err)
	}
	xt := pimpl.Export{}.ExtensionTypeOf(xd, reflect.Zero(t).Interface())

	// Cache the conversion for both directions.
	extensionDescCache.Store(xt, d)
	extensionTypeCache.Store(dk, xt)
	return xt
}

// extensionTypeOf returns a protoreflect.ExtensionType where the GoType
// is the underlying v1 Go type instead of the wrapper types used to present
// v1 Go types as if they satisfied the v2 API.
//
// This function is only valid if xd.Kind is an enum or message.
func extensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType {
	// Step 1: Create an ExtensionType where GoType is the wrapper type.
	conv := pvalue.NewLegacyConverter(t, xd.Kind(), Export{})
	xt := ptype.GoExtension(xd, conv.EnumType, conv.MessageType)

	// Step 2: Wrap ExtensionType such that GoType presents the legacy Go type.
	xt2 := &extensionType{ExtensionType: xt}
	if xd.Cardinality() != pref.Repeated {
		xt2.typ = t
		xt2.new = func() interface{} {
			return xt.New().(pvalue.Unwrapper).ProtoUnwrap()
		}
		xt2.valueOf = func(v interface{}) pref.Value {
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			if xd.Kind() == pref.EnumKind {
				return xt.ValueOf(Export{}.EnumOf(v))
			} else {
				return xt.ValueOf(Export{}.MessageOf(v))
			}
		}
		xt2.interfaceOf = func(v pref.Value) interface{} {
			return xt.InterfaceOf(v).(pvalue.Unwrapper).ProtoUnwrap()
		}
	} else {
		xt2.typ = reflect.PtrTo(reflect.SliceOf(t))
		xt2.new = func() interface{} {
			return reflect.New(xt2.typ.Elem()).Interface()
		}
		xt2.valueOf = func(v interface{}) pref.Value {
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			return pref.ValueOf(pvalue.ListOf(v, conv))
		}
		xt2.interfaceOf = func(pv pref.Value) interface{} {
			v := pv.List().(pvalue.Unwrapper).ProtoUnwrap()
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			return v
		}
	}
	return xt2
}

type extensionType struct {
	pref.ExtensionType
	typ         reflect.Type
	new         func() interface{}
	valueOf     func(interface{}) pref.Value
	interfaceOf func(pref.Value) interface{}
}

func (x *extensionType) GoType() reflect.Type                 { return x.typ }
func (x *extensionType) New() interface{}                     { return x.new() }
func (x *extensionType) ValueOf(v interface{}) pref.Value     { return x.valueOf(v) }
func (x *extensionType) InterfaceOf(v pref.Value) interface{} { return x.interfaceOf(v) }
func (x *extensionType) Format(s fmt.State, r rune)           { pfmt.FormatDesc(s, r, x) }
<commit_msg>internal/legacy: fix ExtensionTypeFromDesc<commit_after>// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package legacy

import (
	"fmt"
	"reflect"
	"sync"

	papi "github.com/golang/protobuf/protoapi"
	ptag "github.com/golang/protobuf/v2/internal/encoding/tag"
	pimpl "github.com/golang/protobuf/v2/internal/impl"
	pfmt "github.com/golang/protobuf/v2/internal/typefmt"
	pvalue "github.com/golang/protobuf/v2/internal/value"
	pref "github.com/golang/protobuf/v2/reflect/protoreflect"
	ptype "github.com/golang/protobuf/v2/reflect/prototype"
)

// extensionDescKey is a comparable version of protoapi.ExtensionDesc
// suitable for use as a key in a map.
type extensionDescKey struct {
	typeV2        pref.ExtensionType
	extendedType  reflect.Type
	extensionType reflect.Type
	field         int32
	name          string
	tag           string
	filename      string
}

func extensionDescKeyOf(d *papi.ExtensionDesc) extensionDescKey {
	return extensionDescKey{
		d.Type,
		reflect.TypeOf(d.ExtendedType),
		reflect.TypeOf(d.ExtensionType),
		d.Field, d.Name, d.Tag, d.Filename,
	}
}

var (
	extensionTypeCache sync.Map // map[extensionDescKey]protoreflect.ExtensionType
	extensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoapi.ExtensionDesc
)

// extensionDescFromType converts a v2 protoreflect.ExtensionType to a
// v1 protoapi.ExtensionDesc. The returned ExtensionDesc must not be mutated.
func extensionDescFromType(t pref.ExtensionType) *papi.ExtensionDesc {
	// Fast-path: check the cache for whether this ExtensionType has already
	// been converted to a legacy descriptor.
	if d, ok := extensionDescCache.Load(t); ok {
		return d.(*papi.ExtensionDesc)
	}

	// Determine the parent type if possible.
	var parent papi.Message
	if mt, ok := t.ExtendedType().(pref.MessageType); ok {
		// Create a new parent message and unwrap it if possible.
		mv := mt.New()
		t := reflect.TypeOf(mv)
		if mv, ok := mv.(pvalue.Unwrapper); ok {
			t = reflect.TypeOf(mv.ProtoUnwrap())
		}

		// Check whether the message implements the legacy v1 Message interface.
		mz := reflect.Zero(t).Interface()
		if mz, ok := mz.(papi.Message); ok {
			parent = mz
		}
	}

	// Determine the v1 extension type, which is unfortunately not the same as
	// the v2 ExtensionType.GoType.
	extType := t.GoType()
	switch extType.Kind() {
	case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
		extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
	case reflect.Ptr:
		if extType.Elem().Kind() == reflect.Slice {
			extType = extType.Elem() // *[]T -> []T for repeated fields
		}
	}

	// Reconstruct the legacy enum full name, which is an odd mixture of the
	// proto package name with the Go type name.
	var enumName string
	if t.Kind() == pref.EnumKind {
		// Derive Go type name.
		// For legacy enums, unwrap the wrapper to get the underlying Go type.
		et := t.EnumType().(pref.EnumType)
		var ev interface{} = et.New(0)
		if u, ok := ev.(pvalue.Unwrapper); ok {
			ev = u.ProtoUnwrap()
		}
		enumName = reflect.TypeOf(ev).Name()

		// Derive the proto package name.
		// For legacy enums, obtain the proto package from the raw descriptor.
		var protoPkg string
		if fd := parentFileDescriptor(et); fd != nil {
			protoPkg = string(fd.Package())
		}
		if ed, ok := ev.(enumV1); ok && protoPkg == "" {
			b, _ := ed.EnumDescriptor()
			protoPkg = loadFileDesc(b).GetPackage()
		}

		if protoPkg != "" {
			enumName = protoPkg + "." + enumName
		}
	}

	// Derive the proto file that the extension was declared within.
	var filename string
	if fd := parentFileDescriptor(t); fd != nil {
		filename = fd.Path()
	}

	// Construct and return a v1 ExtensionDesc.
	d := &papi.ExtensionDesc{
		Type:          t,
		ExtendedType:  parent,
		ExtensionType: reflect.Zero(extType).Interface(),
		Field:         int32(t.Number()),
		Name:          string(t.FullName()),
		Tag:           ptag.Marshal(t, enumName),
		Filename:      filename,
	}
	extensionDescCache.Store(t, d)
	return d
}

// extensionTypeFromDesc converts a v1 protoapi.ExtensionDesc to a
// v2 protoreflect.ExtensionType. The returned descriptor type takes ownership
// of the input extension desc. The input must not be mutated so long as the
// returned type is still in use.
func extensionTypeFromDesc(d *papi.ExtensionDesc) pref.ExtensionType {
	// Fast-path: check whether an extension type is already nested within.
	if d.Type != nil {
		// Cache descriptor for future extensionDescFromType operation.
		// This assumes that there is only one legacy protoapi.ExtensionDesc
		// that wraps any given specific protoreflect.ExtensionType.
		extensionDescCache.LoadOrStore(d.Type, d)
		return d.Type
	}

	// Fast-path: check the cache for whether this ExtensionType has already
	// been converted from a legacy descriptor.
	dk := extensionDescKeyOf(d)
	if t, ok := extensionTypeCache.Load(dk); ok {
		return t.(pref.ExtensionType)
	}

	// Derive basic field information from the struct tag.
	t := reflect.TypeOf(d.ExtensionType)
	isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
	isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
	if isOptional || isRepeated {
		t = t.Elem()
	}
	f := ptag.Unmarshal(d.Tag, t)

	// Construct a v2 ExtensionType.
	conv := pvalue.NewLegacyConverter(t, f.Kind, Export{})
	xd, err := ptype.NewExtension(&ptype.StandaloneExtension{
		FullName:     pref.FullName(d.Name),
		Number:       pref.FieldNumber(d.Field),
		Cardinality:  f.Cardinality,
		Kind:         f.Kind,
		Default:      f.Default,
		Options:      f.Options,
		EnumType:     conv.EnumType,
		MessageType:  conv.MessageType,
		ExtendedType: pimpl.Export{}.MessageTypeOf(d.ExtendedType),
	})
	if err != nil {
		panic(err)
	}
	var zv interface{}
	switch xd.Kind() {
	case pref.EnumKind, pref.MessageKind, pref.GroupKind:
		zv = reflect.Zero(t).Interface()
	}
	xt := pimpl.Export{}.ExtensionTypeOf(xd, zv)

	// Cache the conversion for both directions.
	extensionDescCache.Store(xt, d)
	extensionTypeCache.Store(dk, xt)
	return xt
}

// extensionTypeOf returns a protoreflect.ExtensionType where the GoType
// is the underlying v1 Go type instead of the wrapper types used to present
// v1 Go types as if they satisfied the v2 API.
//
// This function is only valid if xd.Kind is an enum or message.
func extensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType {
	// Step 1: Create an ExtensionType where GoType is the wrapper type.
	conv := pvalue.NewLegacyConverter(t, xd.Kind(), Export{})
	xt := ptype.GoExtension(xd, conv.EnumType, conv.MessageType)

	// Step 2: Wrap ExtensionType such that GoType presents the legacy Go type.
	xt2 := &extensionType{ExtensionType: xt}
	if xd.Cardinality() != pref.Repeated {
		xt2.typ = t
		xt2.new = func() interface{} {
			return xt.New().(pvalue.Unwrapper).ProtoUnwrap()
		}
		xt2.valueOf = func(v interface{}) pref.Value {
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			if xd.Kind() == pref.EnumKind {
				return xt.ValueOf(Export{}.EnumOf(v))
			} else {
				return xt.ValueOf(Export{}.MessageOf(v))
			}
		}
		xt2.interfaceOf = func(v pref.Value) interface{} {
			return xt.InterfaceOf(v).(pvalue.Unwrapper).ProtoUnwrap()
		}
	} else {
		xt2.typ = reflect.PtrTo(reflect.SliceOf(t))
		xt2.new = func() interface{} {
			return reflect.New(xt2.typ.Elem()).Interface()
		}
		xt2.valueOf = func(v interface{}) pref.Value {
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			return pref.ValueOf(pvalue.ListOf(v, conv))
		}
		xt2.interfaceOf = func(pv pref.Value) interface{} {
			v := pv.List().(pvalue.Unwrapper).ProtoUnwrap()
			if reflect.TypeOf(v) != xt2.typ {
				panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
			}
			return v
		}
	}
	return xt2
}

type extensionType struct {
	pref.ExtensionType
	typ         reflect.Type
	new         func() interface{}
	valueOf     func(interface{}) pref.Value
	interfaceOf func(pref.Value) interface{}
}

func (x *extensionType) GoType() reflect.Type                 { return x.typ }
func (x *extensionType) New() interface{}                     { return x.new() }
func (x *extensionType) ValueOf(v interface{}) pref.Value     { return x.valueOf(v) }
func (x *extensionType) InterfaceOf(v pref.Value) interface{} { return x.interfaceOf(v) }
func (x *extensionType) Format(s fmt.State, r rune)           { pfmt.FormatDesc(s, r, x) }
<|endoftext|>


<commit_before>package redisqueue

import (
	"errors"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/antihax/evedata/internal/gobcoder"

	"github.com/garyburd/redigo/redis"
)

const (
	Priority_Lowest = iota
	Priority_Low    = iota
	Priority_Normal = iota
	Priority_High   = iota
	Priority_Urgent = iota
)

// RedisQueue operation queue to CCP APIs
type RedisQueue struct {
	redisPool   *redis.Pool
	key         string
	queueScript *redis.Script
}

// Work to be performed
type Work struct {
	Operation string      `json:"operation"`
	Parameter interface{} `json:"parameters"`
}

// NewRedisQueue creates a new work queue with an existing
// redigo pool and key name.
func NewRedisQueue(r *redis.Pool, key string) *RedisQueue {

	rq := &RedisQueue{redisPool: r, key: key, queueScript: redis.NewScript(0, priorityQueueScript)}
	conn := r.Get()
	defer conn.Close()

	err := rq.queueScript.Load(conn)
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("Loaded script %s\n", rq.queueScript.Hash())

	return rq
}

// Size returns number of elements in the queue
func (hq *RedisQueue) Size() (int, error) {
	r := hq.redisPool.Get()
	defer r.Close()
	return redis.Int(hq.queueScript.Do(r, "count", hq.key))
}

// ScaledSize  returns number of elements in the queue scaled by priority
func (hq *RedisQueue) ScaledSize() (int, error) {
	r := hq.redisPool.Get()
	defer r.Close()

	low, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Low, Priority_Low))
	if err != nil {
		return 0, err
	}
	norm, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Normal, Priority_Normal))
	if err != nil {
		return 0, err
	}
	high, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_High, Priority_High))
	if err != nil {
		return 0, err
	}
	urg, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Urgent, Priority_Urgent))
	if err != nil {
		return 0, err
	}

	return low + (norm * 64) + (high * 1024) + (urg * 8192), nil
}

// QueueWork adds work to the queue
func (hq *RedisQueue) QueueWork(work []Work, priority int) error {
	// Get a redis connection from the pool
	conn := hq.redisPool.Get()
	defer conn.Close()

	// Pipeline our work to the connection.
	for i := range work {
		b, err := gobcoder.GobEncoder(work[i])
		if err != nil {
			return err
		}
		if _, err := hq.queueScript.Do(conn, "push", hq.key, b, priority); err != nil {
			return err
		}
	}

	return nil
}

// GetWork retreives items from the queue
func (hq *RedisQueue) GetWork() (*Work, error) {
	// Get a redis connection from the pool
	conn := hq.redisPool.Get()
	defer conn.Close()

	var (
		w   Work
		v   interface{}
		err error
	)

	// Poll until we get data.
	for {
		v, err = hq.queueScript.Do(conn, "pop", hq.key)
		if err != nil {
			return nil, err
		}
		if v == nil {
			time.Sleep(time.Millisecond * 100)
			continue
		}
		break
	}

	b, ok := v.([]byte)
	if !ok {
		return nil, errors.New("empty result")
	}

	// Decode the data back into its structure
	if err := gobcoder.GobDecoder(b, &w); err != nil {
		return nil, err
	}

	return &w, nil
}

// CheckWorkCompleted takes a key and checks if the ID has been completed to prevent duplicates
func (hq *RedisQueue) CheckWorkCompleted(key string, id interface{}) bool {
	conn := hq.redisPool.Get()
	defer conn.Close()
	found, err := redis.Bool(conn.Do("SISMEMBER", key, id))
	if err != nil {
		log.Println(err)
	}
	return found
}

// CheckWorkCompletedInBulk takes a key and checks if the list of IDs has completed
func (hq *RedisQueue) CheckWorkCompletedInBulk(key string, id []int64) ([]bool, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []bool

	conn.Send("MULTI")
	for _, i := range id {
		conn.Send("SISMEMBER", key, i)
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.Bool(r, nil)
		found = append(found, b)
	}

	return found, nil
}

// SetWorkCompleted takes a key and sets if the ID has been completed to prevent duplicates
func (hq *RedisQueue) SetWorkCompleted(key string, id interface{}) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SADD", key, id)
	return err
}

// SetWorkCompleted takes a key and sets if the ID has been completed to prevent duplicates
func (hq *RedisQueue) SetWorkCompletedInBulk(key string, ids []int64) error {
	conn := hq.redisPool.Get()
	defer conn.Close()

	for _, id := range ids {
		err := conn.Send("SADD", key, id)
		if err != nil {
			log.Println(err)
			return err
		}
	}

	err := conn.Flush()
	if err != nil {
		log.Println(err)
		return err
	}

	return nil
}

// CheckWorkExpired takes a key and checks if the ID has expired
func (hq *RedisQueue) CheckWorkExpired(key string, id interface{}) bool {
	conn := hq.redisPool.Get()
	defer conn.Close()
	found, _ := redis.Bool(conn.Do("GET", fmt.Sprintf("%s:%v", key, id)))

	return found
}

// CheckWorkExpiredInBulk takes a key and checks if the list of IDs has expired
func (hq *RedisQueue) CheckWorkExpiredInBulk(key string, id []int64) ([]bool, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []bool

	conn.Send("MULTI")
	for _, i := range id {
		conn.Send("GET", fmt.Sprintf("%s:%d", key, i))
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.Bool(r, nil)
		found = append(found, b)
	}

	return found, nil
}

// SetWorkExpire takes a key and sets if the ID has failed to prevent multiple failed
func (hq *RedisQueue) SetWorkExpire(key string, id interface{}, seconds int) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SETEX", fmt.Sprintf("%s:%v", key, id), seconds, true)
	return err
}

// SetCache takes a key and sets the string for the ID
func (hq *RedisQueue) SetCache(key string, id int32, value string) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SET", key+strconv.FormatInt(int64(id), 10), value)
	return err
}

// GetCache takes a key and gets the string for the ID
func (hq *RedisQueue) GetCache(key string, id int32) (string, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	return redis.String(conn.Do("GET", key+strconv.FormatInt(int64(id), 10)))
}

// SetCacheInBulk takes keys and sets the strings for the ID
func (hq *RedisQueue) SetCacheInBulk(key string, ids []int32, strings []string) error {
	conn := hq.redisPool.Get()
	defer conn.Close()

	for i := range ids {
		err := conn.Send("SET", key, key+strconv.FormatInt(int64(ids[i]), 10), strings[i])
		if err != nil {
			log.Println(err)
			return err
		}
	}

	err := conn.Flush()
	if err != nil {
		log.Println(err)
		return err
	}

	return nil
}

// GetCacheInBulk takes a key and checks if the list of IDs has expired
func (hq *RedisQueue) GetCacheInBulk(key string, id []int32) ([]string, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []string

	conn.Send("MULTI")
	for _, i := range id {
		if err := conn.Send("GET", key+strconv.FormatInt(int64(i), 10)); err != nil {
			log.Println(err)
			return nil, err
		}
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.String(r, nil)
		found = append(found, b)
	}

	return found, nil
}
<commit_msg>Don't reload the script if it exists<commit_after>package redisqueue

import (
	"errors"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/antihax/evedata/internal/gobcoder"

	"github.com/garyburd/redigo/redis"
)

const (
	Priority_Lowest = iota
	Priority_Low    = iota
	Priority_Normal = iota
	Priority_High   = iota
	Priority_Urgent = iota
)

// RedisQueue operation queue to CCP APIs
type RedisQueue struct {
	redisPool   *redis.Pool
	key         string
	queueScript *redis.Script
}

// Work to be performed
type Work struct {
	Operation string      `json:"operation"`
	Parameter interface{} `json:"parameters"`
}

// NewRedisQueue creates a new work queue with an existing
// redigo pool and key name.
func NewRedisQueue(r *redis.Pool, key string) *RedisQueue {

	rq := &RedisQueue{redisPool: r, key: key, queueScript: redis.NewScript(0, priorityQueueScript)}
	conn := r.Get()
	defer conn.Close()

	exists, err := redis.Ints(conn.Do("SCRIPT", "EXISTS", rq.queueScript.Hash()))
	if err != nil {
		log.Fatalln(err)
	}

	if exists[0] != 1 {
		log.Printf("Load script %s\n", rq.queueScript.Hash())
		err := rq.queueScript.Load(conn)
		if err != nil {
			log.Fatalln(err)
		}
	}

	log.Printf("Script %s Ready\n", rq.queueScript.Hash())

	return rq
}

// Size returns number of elements in the queue
func (hq *RedisQueue) Size() (int, error) {
	r := hq.redisPool.Get()
	defer r.Close()
	return redis.Int(hq.queueScript.Do(r, "count", hq.key))
}

// ScaledSize  returns number of elements in the queue scaled by priority
func (hq *RedisQueue) ScaledSize() (int, error) {
	r := hq.redisPool.Get()
	defer r.Close()

	low, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Low, Priority_Low))
	if err != nil {
		return 0, err
	}
	norm, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Normal, Priority_Normal))
	if err != nil {
		return 0, err
	}
	high, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_High, Priority_High))
	if err != nil {
		return 0, err
	}
	urg, err := redis.Int(hq.queueScript.Do(r, "count", hq.key, Priority_Urgent, Priority_Urgent))
	if err != nil {
		return 0, err
	}

	return low + (norm * 64) + (high * 1024) + (urg * 8192), nil
}

// QueueWork adds work to the queue
func (hq *RedisQueue) QueueWork(work []Work, priority int) error {
	// Get a redis connection from the pool
	conn := hq.redisPool.Get()
	defer conn.Close()

	// Pipeline our work to the connection.
	for i := range work {
		b, err := gobcoder.GobEncoder(work[i])
		if err != nil {
			return err
		}
		if _, err := hq.queueScript.Do(conn, "push", hq.key, b, priority); err != nil {
			return err
		}
	}

	return nil
}

// GetWork retreives items from the queue
func (hq *RedisQueue) GetWork() (*Work, error) {
	// Get a redis connection from the pool
	conn := hq.redisPool.Get()
	defer conn.Close()

	var (
		w   Work
		v   interface{}
		err error
	)

	// Poll until we get data.
	for {
		v, err = hq.queueScript.Do(conn, "pop", hq.key)
		if err != nil {
			return nil, err
		}
		if v == nil {
			time.Sleep(time.Millisecond * 100)
			continue
		}
		break
	}

	b, ok := v.([]byte)
	if !ok {
		return nil, errors.New("empty result")
	}

	// Decode the data back into its structure
	if err := gobcoder.GobDecoder(b, &w); err != nil {
		return nil, err
	}

	return &w, nil
}

// CheckWorkCompleted takes a key and checks if the ID has been completed to prevent duplicates
func (hq *RedisQueue) CheckWorkCompleted(key string, id interface{}) bool {
	conn := hq.redisPool.Get()
	defer conn.Close()
	found, err := redis.Bool(conn.Do("SISMEMBER", key, id))
	if err != nil {
		log.Println(err)
	}
	return found
}

// CheckWorkCompletedInBulk takes a key and checks if the list of IDs has completed
func (hq *RedisQueue) CheckWorkCompletedInBulk(key string, id []int64) ([]bool, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []bool

	conn.Send("MULTI")
	for _, i := range id {
		conn.Send("SISMEMBER", key, i)
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.Bool(r, nil)
		found = append(found, b)
	}

	return found, nil
}

// SetWorkCompleted takes a key and sets if the ID has been completed to prevent duplicates
func (hq *RedisQueue) SetWorkCompleted(key string, id interface{}) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SADD", key, id)
	return err
}

// SetWorkCompleted takes a key and sets if the ID has been completed to prevent duplicates
func (hq *RedisQueue) SetWorkCompletedInBulk(key string, ids []int64) error {
	conn := hq.redisPool.Get()
	defer conn.Close()

	for _, id := range ids {
		err := conn.Send("SADD", key, id)
		if err != nil {
			log.Println(err)
			return err
		}
	}

	err := conn.Flush()
	if err != nil {
		log.Println(err)
		return err
	}

	return nil
}

// CheckWorkExpired takes a key and checks if the ID has expired
func (hq *RedisQueue) CheckWorkExpired(key string, id interface{}) bool {
	conn := hq.redisPool.Get()
	defer conn.Close()
	found, _ := redis.Bool(conn.Do("GET", fmt.Sprintf("%s:%v", key, id)))

	return found
}

// CheckWorkExpiredInBulk takes a key and checks if the list of IDs has expired
func (hq *RedisQueue) CheckWorkExpiredInBulk(key string, id []int64) ([]bool, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []bool

	conn.Send("MULTI")
	for _, i := range id {
		conn.Send("GET", fmt.Sprintf("%s:%d", key, i))
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.Bool(r, nil)
		found = append(found, b)
	}

	return found, nil
}

// SetWorkExpire takes a key and sets if the ID has failed to prevent multiple failed
func (hq *RedisQueue) SetWorkExpire(key string, id interface{}, seconds int) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SETEX", fmt.Sprintf("%s:%v", key, id), seconds, true)
	return err
}

// SetCache takes a key and sets the string for the ID
func (hq *RedisQueue) SetCache(key string, id int32, value string) error {
	conn := hq.redisPool.Get()
	defer conn.Close()
	_, err := conn.Do("SET", key+strconv.FormatInt(int64(id), 10), value)
	return err
}

// GetCache takes a key and gets the string for the ID
func (hq *RedisQueue) GetCache(key string, id int32) (string, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	return redis.String(conn.Do("GET", key+strconv.FormatInt(int64(id), 10)))
}

// SetCacheInBulk takes keys and sets the strings for the ID
func (hq *RedisQueue) SetCacheInBulk(key string, ids []int32, strings []string) error {
	conn := hq.redisPool.Get()
	defer conn.Close()

	for i := range ids {
		err := conn.Send("SET", key, key+strconv.FormatInt(int64(ids[i]), 10), strings[i])
		if err != nil {
			log.Println(err)
			return err
		}
	}

	err := conn.Flush()
	if err != nil {
		log.Println(err)
		return err
	}

	return nil
}

// GetCacheInBulk takes a key and checks if the list of IDs has expired
func (hq *RedisQueue) GetCacheInBulk(key string, id []int32) ([]string, error) {
	conn := hq.redisPool.Get()
	defer conn.Close()
	var found []string

	conn.Send("MULTI")
	for _, i := range id {
		if err := conn.Send("GET", key+strconv.FormatInt(int64(i), 10)); err != nil {
			log.Println(err)
			return nil, err
		}
	}

	res, err := conn.Do("EXEC")
	if err != nil {
		log.Println(err)
		return nil, err
	}

	for _, r := range res.([]interface{}) {
		b, _ := redis.String(r, nil)
		found = append(found, b)
	}

	return found, nil
}
<|endoftext|>


<commit_before>package sdk

import (
	"bytes"
	"io/ioutil"
	"math/rand"
	"net/http"
	"time"

	"github.com/smartystreets/clock"
)

// RetryClient sends failed requests multiple times depending on the parameters passed to NewRetryClient.
type RetryClient struct {
	inner      HTTPClient
	maxRetries int
	sleeper    *clock.Sleeper
	rand       *rand.Rand
}

func NewRetryClient(inner HTTPClient, maxRetries int, rand *rand.Rand) HTTPClient {
	if maxRetries == 0 {
		return inner
	}
	return &RetryClient{
		inner:      inner,
		maxRetries: maxRetries,
		rand:       rand,
	}
}

func (r *RetryClient) Do(request *http.Request) (*http.Response, error) {
	if request.Method == "POST" {
		return r.doBufferedPost(request)
	}
	return r.doGet(request)
}

func (r *RetryClient) doGet(request *http.Request) (response *http.Response, err error) {
	for attempt := 0; r.backOff(attempt); attempt++ {
		if response, err = r.inner.Do(request); err == nil && response.StatusCode == http.StatusOK {
			break
		}
	}
	return response, err
}

func (r *RetryClient) doBufferedPost(request *http.Request) (response *http.Response, err error) {
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, err
	}

	for attempt := 0; r.backOff(attempt); attempt++ {
		request.Body = ioutil.NopCloser(bytes.NewReader(body))
		if response, err = r.inner.Do(request); err == nil && response.StatusCode == http.StatusOK {
			break
		}
	}
	return response, err
}

func (r *RetryClient) backOff(attempt int) bool {
	if attempt == 0 {
		return true
	}
	if attempt > r.maxRetries {
		return false
	}
	backOffCap := min(maxBackOffDuration, 2<<attempt)
	backOff := time.Second * time.Duration(r.rand.Intn(backOffCap))
	r.sleeper.Sleep(backOff)
	return true
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

const maxBackOffDurationInSeconds = 10
const maxBackOffDuration = 10
<commit_msg>Protect access to random source to support concurrent client usage.<commit_after>package sdk

import (
	"bytes"
	"io/ioutil"
	"math/rand"
	"net/http"
	"sync"
	"time"

	"github.com/smartystreets/clock"
)

// RetryClient sends failed requests multiple times depending on the parameters passed to NewRetryClient.
type RetryClient struct {
	inner      HTTPClient
	maxRetries int
	sleeper    *clock.Sleeper
	lock       *sync.Mutex
	rand       *rand.Rand
}

func NewRetryClient(inner HTTPClient, maxRetries int, rand *rand.Rand) HTTPClient {
	if maxRetries == 0 {
		return inner
	}
	return &RetryClient{
		inner:      inner,
		maxRetries: maxRetries,
		lock:       new(sync.Mutex),
		rand:       rand,
	}
}

func (r *RetryClient) Do(request *http.Request) (*http.Response, error) {
	if request.Method == "POST" {
		return r.doBufferedPost(request)
	}
	return r.doGet(request)
}

func (r *RetryClient) doGet(request *http.Request) (response *http.Response, err error) {
	for attempt := 0; r.backOff(attempt); attempt++ {
		if response, err = r.inner.Do(request); err == nil && response.StatusCode == http.StatusOK {
			break
		}
	}
	return response, err
}

func (r *RetryClient) doBufferedPost(request *http.Request) (response *http.Response, err error) {
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, err
	}

	for attempt := 0; r.backOff(attempt); attempt++ {
		request.Body = ioutil.NopCloser(bytes.NewReader(body))
		if response, err = r.inner.Do(request); err == nil && response.StatusCode == http.StatusOK {
			break
		}
	}
	return response, err
}

func (r *RetryClient) backOff(attempt int) bool {
	if attempt == 0 {
		return true
	}
	if attempt > r.maxRetries {
		return false
	}
	backOffCap := min(maxBackOffDuration, 2<<attempt)
	backOff := time.Second * time.Duration(r.random(backOffCap))
	r.sleeper.Sleep(backOff)
	return true
}
func (r *RetryClient) random(cap int) int {
	r.lock.Lock()
	defer r.lock.Unlock()
	return r.rand.Intn(cap)
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

const maxBackOffDuration = 10
<|endoftext|>


<commit_before>package toystore

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
	"github.com/rlayte/dive"
)

type Toystore struct {
	dive *dive.Node
	port int
	data Store
}

type Store interface {
	Get(string) (string, bool)
	Put(string, string)
}

func (t *Toystore) Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value, ok := t.data.Get(key)

	log.Printf("GET - %s : %s", key, value, t.dive.Members)

	if !ok {
		w.Header().Set("Status", "404")
		fmt.Fprint(w, "Not found")
		return
	} else {
		fmt.Fprint(w, value)
	}
}

func (t *Toystore) Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value := r.FormValue("data")
	t.data.Put(key, value)
}

func (t *Toystore) Serve() {
	router := httprouter.New()

	router.GET("/:key", t.Get)
	router.POST("/:key", t.Put)

	log.Println("Running server on port", t.port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", t.port), router))
}

func New(port int, store Store, seed string) *Toystore {
	return &Toystore{
		dive: dive.NewNode(port+10, seed),
		port: port,
		data: store,
	}
}
<commit_msg>Configure dive<commit_after>package toystore

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/Charlesetc/dive"
	"github.com/julienschmidt/httprouter"
)

type Toystore struct {
	dive *dive.Node
	port int
	data Store
}

type Store interface {
	Get(string) (string, bool)
	Put(string, string)
}

func (t *Toystore) Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value, ok := t.data.Get(key)

	log.Printf("GET - %s : %s", key, value, t.dive.Members)

	if !ok {
		w.Header().Set("Status", "404")
		fmt.Fprint(w, "Not found")
		return
	} else {
		fmt.Fprint(w, value)
	}
}

func (t *Toystore) Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value := r.FormValue("data")
	t.data.Put(key, value)
}

func (t *Toystore) Serve() {
	router := httprouter.New()

	router.GET("/:key", t.Get)
	router.POST("/:key", t.Put)

	log.Println("Running server on port", t.port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", t.port), router))
}

func New(port int, store Store, seed string) *Toystore {
	dive.PingInterval = time.Second

	return &Toystore{
		dive: dive.NewNode(port+10, seed),
		port: port,
		data: store,
	}
}
<|endoftext|>


<commit_before>/*
 *
 * Copyright 2020 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.
 *
 */

// Binary client for xDS interop tests.
package main

import (
	"context"
	"flag"
	"fmt"
	"net"
	"sync"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
	testpb "google.golang.org/grpc/interop/grpc_testing"
	"google.golang.org/grpc/peer"
)

type statsWatcherKey struct {
	startID int32
	endID   int32
}

type statsWatcher struct {
	rpcsByPeer    map[string]int32
	numFailures   int32
	remainingRpcs int32
	c             chan *testpb.SimpleResponse
}

var (
	numChannels   = flag.Int("num_channels", 1, "Num of channels")
	printResponse = flag.Bool("print_response", false, "Write RPC response to stdout")
	qps           = flag.Int("qps", 1, "QPS per channel")
	rpcTimeout    = flag.Duration("rpc_timeout", 10*time.Second, "Per RPC timeout")
	server        = flag.String("server", "localhost:8080", "Address of server to connect to")
	statsPort     = flag.Int("stats_port", 8081, "Port to expose peer distribution stats service")

	mu               sync.Mutex
	currentRequestID int32
	watchers         = make(map[statsWatcherKey]*statsWatcher)
)

type statsService struct{}

// Wait for the next LoadBalancerStatsRequest.GetNumRpcs to start and complete,
// and return the distribution of remote peers. This is essentially a clientside
// LB reporting mechanism that is designed to be queried by an external test
// driver when verifying that the client is distributing RPCs as expected.
func (s *statsService) GetClientStats(ctx context.Context, in *testpb.LoadBalancerStatsRequest) (*testpb.LoadBalancerStatsResponse, error) {
	mu.Lock()
	watcherKey := statsWatcherKey{currentRequestID, currentRequestID + in.GetNumRpcs()}
	watcher, ok := watchers[watcherKey]
	if !ok {
		watcher = &statsWatcher{
			rpcsByPeer:    make(map[string]int32),
			numFailures:   0,
			remainingRpcs: in.GetNumRpcs(),
			c:             make(chan *testpb.SimpleResponse),
		}
		watchers[watcherKey] = watcher
	}
	mu.Unlock()

	ctx, cancel := context.WithTimeout(ctx, time.Duration(in.GetTimeoutSec())*time.Second)
	defer cancel()

	defer func() {
		mu.Lock()
		delete(watchers, watcherKey)
		mu.Unlock()
	}()

	// Wait until the requested RPCs have all been recorded or timeout occurs.
	for {
		select {
		case r := <-watcher.c:
			if r != nil {
				watcher.rpcsByPeer[(*r).GetServerId()]++
			} else {
				watcher.numFailures++
			}
			watcher.remainingRpcs--
			if watcher.remainingRpcs == 0 {
				return &testpb.LoadBalancerStatsResponse{NumFailures: watcher.numFailures + watcher.remainingRpcs, RpcsByPeer: watcher.rpcsByPeer}, nil
			}
		case <-ctx.Done():
			grpclog.Info("Timed out, returning partial stats")
			return &testpb.LoadBalancerStatsResponse{NumFailures: watcher.numFailures + watcher.remainingRpcs, RpcsByPeer: watcher.rpcsByPeer}, nil
		}
	}
}

func main() {
	flag.Parse()

	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *statsPort))
	if err != nil {
		grpclog.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	defer s.Stop()
	testpb.RegisterLoadBalancerStatsServiceServer(s, &statsService{})
	go s.Serve(lis)

	clients := make([]testpb.TestServiceClient, *numChannels)
	for i := 0; i < *numChannels; i++ {
		conn, err := grpc.DialContext(context.Background(), *server, grpc.WithInsecure(), grpc.WithBlock())
		if err != nil {
			grpclog.Fatalf("Fail to dial: %v", err)
		}
		defer conn.Close()
		clients[i] = testpb.NewTestServiceClient(conn)
	}
	ticker := time.NewTicker(time.Second / time.Duration(*qps**numChannels))
	defer ticker.Stop()
	sendRPCs(clients, ticker)
}

func sendRPCs(clients []testpb.TestServiceClient, ticker *time.Ticker) {
	var i int
	for range ticker.C {
		go func(i int) {
			c := clients[i]
			ctx, cancel := context.WithTimeout(context.Background(), *rpcTimeout)
			p := new(peer.Peer)
			mu.Lock()
			savedRequestID := currentRequestID
			currentRequestID++
			savedWatchers := []*statsWatcher{}
			for key, value := range watchers {
				if key.startID <= savedRequestID && savedRequestID < key.endID {
					savedWatchers = append(savedWatchers, value)
				}
			}
			mu.Unlock()
			r, err := c.UnaryCall(ctx, &testpb.SimpleRequest{FillServerId: true}, grpc.Peer(p))

			success := err == nil
			cancel()

			for _, watcher := range savedWatchers {
				watcher.c <- r
			}

			if success && *printResponse {
				fmt.Printf("Greeting: Hello world, this is %s, from %v\n", r.GetHostname(), p.Addr)
			}
		}(i)
		i = (i + 1) % len(clients)
	}
}
<commit_msg>test: include xDS package in test client dependencies (#3407)<commit_after>/*
 *
 * Copyright 2020 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.
 *
 */

// Binary client for xDS interop tests.
package main

import (
	"context"
	"flag"
	"fmt"
	"net"
	"sync"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
	testpb "google.golang.org/grpc/interop/grpc_testing"
	"google.golang.org/grpc/peer"
	_ "google.golang.org/grpc/xds/experimental"
)

type statsWatcherKey struct {
	startID int32
	endID   int32
}

type statsWatcher struct {
	rpcsByPeer    map[string]int32
	numFailures   int32
	remainingRpcs int32
	c             chan *testpb.SimpleResponse
}

var (
	numChannels   = flag.Int("num_channels", 1, "Num of channels")
	printResponse = flag.Bool("print_response", false, "Write RPC response to stdout")
	qps           = flag.Int("qps", 1, "QPS per channel")
	rpcTimeout    = flag.Duration("rpc_timeout", 10*time.Second, "Per RPC timeout")
	server        = flag.String("server", "localhost:8080", "Address of server to connect to")
	statsPort     = flag.Int("stats_port", 8081, "Port to expose peer distribution stats service")

	mu               sync.Mutex
	currentRequestID int32
	watchers         = make(map[statsWatcherKey]*statsWatcher)
)

type statsService struct{}

// Wait for the next LoadBalancerStatsRequest.GetNumRpcs to start and complete,
// and return the distribution of remote peers. This is essentially a clientside
// LB reporting mechanism that is designed to be queried by an external test
// driver when verifying that the client is distributing RPCs as expected.
func (s *statsService) GetClientStats(ctx context.Context, in *testpb.LoadBalancerStatsRequest) (*testpb.LoadBalancerStatsResponse, error) {
	mu.Lock()
	watcherKey := statsWatcherKey{currentRequestID, currentRequestID + in.GetNumRpcs()}
	watcher, ok := watchers[watcherKey]
	if !ok {
		watcher = &statsWatcher{
			rpcsByPeer:    make(map[string]int32),
			numFailures:   0,
			remainingRpcs: in.GetNumRpcs(),
			c:             make(chan *testpb.SimpleResponse),
		}
		watchers[watcherKey] = watcher
	}
	mu.Unlock()

	ctx, cancel := context.WithTimeout(ctx, time.Duration(in.GetTimeoutSec())*time.Second)
	defer cancel()

	defer func() {
		mu.Lock()
		delete(watchers, watcherKey)
		mu.Unlock()
	}()

	// Wait until the requested RPCs have all been recorded or timeout occurs.
	for {
		select {
		case r := <-watcher.c:
			if r != nil {
				watcher.rpcsByPeer[(*r).GetServerId()]++
			} else {
				watcher.numFailures++
			}
			watcher.remainingRpcs--
			if watcher.remainingRpcs == 0 {
				return &testpb.LoadBalancerStatsResponse{NumFailures: watcher.numFailures + watcher.remainingRpcs, RpcsByPeer: watcher.rpcsByPeer}, nil
			}
		case <-ctx.Done():
			grpclog.Info("Timed out, returning partial stats")
			return &testpb.LoadBalancerStatsResponse{NumFailures: watcher.numFailures + watcher.remainingRpcs, RpcsByPeer: watcher.rpcsByPeer}, nil
		}
	}
}

func main() {
	flag.Parse()

	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *statsPort))
	if err != nil {
		grpclog.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	defer s.Stop()
	testpb.RegisterLoadBalancerStatsServiceServer(s, &statsService{})
	go s.Serve(lis)

	clients := make([]testpb.TestServiceClient, *numChannels)
	for i := 0; i < *numChannels; i++ {
		conn, err := grpc.DialContext(context.Background(), *server, grpc.WithInsecure(), grpc.WithBlock())
		if err != nil {
			grpclog.Fatalf("Fail to dial: %v", err)
		}
		defer conn.Close()
		clients[i] = testpb.NewTestServiceClient(conn)
	}
	ticker := time.NewTicker(time.Second / time.Duration(*qps**numChannels))
	defer ticker.Stop()
	sendRPCs(clients, ticker)
}

func sendRPCs(clients []testpb.TestServiceClient, ticker *time.Ticker) {
	var i int
	for range ticker.C {
		go func(i int) {
			c := clients[i]
			ctx, cancel := context.WithTimeout(context.Background(), *rpcTimeout)
			p := new(peer.Peer)
			mu.Lock()
			savedRequestID := currentRequestID
			currentRequestID++
			savedWatchers := []*statsWatcher{}
			for key, value := range watchers {
				if key.startID <= savedRequestID && savedRequestID < key.endID {
					savedWatchers = append(savedWatchers, value)
				}
			}
			mu.Unlock()
			r, err := c.UnaryCall(ctx, &testpb.SimpleRequest{FillServerId: true}, grpc.Peer(p))

			success := err == nil
			cancel()

			for _, watcher := range savedWatchers {
				watcher.c <- r
			}

			if success && *printResponse {
				fmt.Printf("Greeting: Hello world, this is %s, from %v\n", r.GetHostname(), p.Addr)
			}
		}(i)
		i = (i + 1) % len(clients)
	}
}
<|endoftext|>


<commit_before>package toystore

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/Charlesetc/dive"
	"github.com/julienschmidt/httprouter"
)

type Toystore struct {
	dive *dive.Node
	port int
	data Store
}

type Store interface {
	Get(string) (string, bool)
	Put(string, string)
}

func (t *Toystore) Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value, ok := t.data.Get(key)

	log.Printf("GET - %s : %s", key, value, t.dive.Members)

	if !ok {
		w.Header().Set("Status", "404")
		fmt.Fprint(w, "Not found")
		return
	} else {
		fmt.Fprint(w, value)
	}
}

func (t *Toystore) Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value := r.FormValue("data")
	t.data.Put(key, value)
}

func (t *Toystore) Serve() {
	router := httprouter.New()

	router.GET("/:key", t.Get)
	router.POST("/:key", t.Put)

	log.Println("Running server on port", t.port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", t.port), router))
}

func New(port int, store Store, seed string) *Toystore {
	dive.PingInterval = time.Second

	return &Toystore{
		dive: dive.NewNode(port+10, seed),
		port: port,
		data: store,
	}
}
<commit_msg>Create ring from member nodes on get/put requests<commit_after>package toystore

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/charlesetc/circle"
	"github.com/charlesetc/dive"
	"github.com/julienschmidt/httprouter"
)

type Toystore struct {
	dive *dive.Node
	port int
	data Store
	ring *circle.Circle
}

type Store interface {
	Get(string) (string, bool)
	Put(string, string)
}

func (t *Toystore) updateMembers() {
	addresses := []string{t.dive.Address()}

	for _, member := range t.dive.Members {
		addresses = append(addresses, member.Address)
	}

	t.ring = circle.CircleFromList(addresses)
}

func (t *Toystore) Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	t.updateMembers()

	key := params.ByName("key")
	value, ok := t.data.Get(key)
	lookup := t.ring.KeyAddress([]byte(key))
	address, _ := lookup()

	log.Printf("GET - %s : %s", key, value)
	log.Println("Key address", string(address), t.ring)

	if !ok {
		w.Header().Set("Status", "404")
		fmt.Fprint(w, "Not found")
		return
	} else {
		fmt.Fprint(w, value)
	}
}

func (t *Toystore) Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	t.updateMembers()

	key := params.ByName("key")
	value := r.FormValue("data")

	log.Printf("PUT - %s : %s", key, value)

	t.data.Put(key, value)
}

func (t *Toystore) Serve() {
	router := httprouter.New()

	router.GET("/:key", t.Get)
	router.POST("/:key", t.Put)

	log.Println("Running server on port", t.port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", t.port), router))
}

func New(port int, store Store, seed string) *Toystore {
	dive.PingInterval = time.Second

	return &Toystore{
		dive: dive.NewNode(port+10, seed),
		port: port,
		data: store,
	}
}
<|endoftext|>


<commit_before>package workspace

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestHasMetadata(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exerciseA := Exercise{Root: ws, Track: "bogus-track", Slug: "apple"}
	exerciseB := Exercise{Root: ws, Track: "bogus-track", Slug: "banana"}

	err = os.MkdirAll(filepath.Dir(exerciseA.MetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)
	err = os.MkdirAll(filepath.Dir(exerciseB.MetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(exerciseA.MetadataFilepath(), []byte{}, os.FileMode(0600))
	assert.NoError(t, err)

	ok, err := exerciseA.HasMetadata()
	assert.NoError(t, err)
	assert.True(t, ok)

	ok, err = exerciseB.HasMetadata()
	assert.NoError(t, err)
	assert.False(t, ok)
}

func TestNewFromDir(t *testing.T) {
	dir := filepath.Join("something", "another", "whatever", "the-track", "the-exercise")

	exercise := NewExerciseFromDir(dir)
	assert.Equal(t, filepath.Join("something", "another", "whatever"), exercise.Root)
	assert.Equal(t, "the-track", exercise.Track)
	assert.Equal(t, "the-exercise", exercise.Slug)
}

func TestMigrateLegacyMetadataFileWithoutLegacy(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "no-legacy"}
	metadataFilepath := exercise.MetadataFilepath()
	err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasMetadata()
	assert.True(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Nil(t, err)
	assert.Equal(t, "", stderr)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacy(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "legacy"}
	metadataFilepath := exercise.MetadataFilepath()
	legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
	err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasLegacyMetadata()
	assert.True(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.False(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Equal(t, fmt.Sprintf("\nMigrated metadata to %s\n", metadataFilepath), stderr)
	assert.NoError(t, err)
	ok, _ = exercise.HasLegacyMetadata()
	assert.False(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacyAndModern(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "both-legacy-and-modern"}
	metadataFilepath := exercise.MetadataFilepath()
	legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
	err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)
	err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasLegacyMetadata()
	assert.True(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Equal(t, fmt.Sprintf("\nRemoved legacy metadata: %s\n", legacyMetadataFilepath), stderr)
	assert.NoError(t, err)
	ok, _ = exercise.HasLegacyMetadata()
	assert.False(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}
<commit_msg>Add test for HasLegacyMetadata<commit_after>package workspace

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestHasMetadata(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exerciseA := Exercise{Root: ws, Track: "bogus-track", Slug: "apple"}
	exerciseB := Exercise{Root: ws, Track: "bogus-track", Slug: "banana"}

	err = os.MkdirAll(filepath.Dir(exerciseA.MetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)
	err = os.MkdirAll(filepath.Dir(exerciseB.MetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(exerciseA.MetadataFilepath(), []byte{}, os.FileMode(0600))
	assert.NoError(t, err)

	ok, err := exerciseA.HasMetadata()
	assert.NoError(t, err)
	assert.True(t, ok)

	ok, err = exerciseB.HasMetadata()
	assert.NoError(t, err)
	assert.False(t, ok)
}

func TestHasLegacyMetadata(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exerciseA := Exercise{Root: ws, Track: "bogus-track", Slug: "apple"}
	exerciseB := Exercise{Root: ws, Track: "bogus-track", Slug: "banana"}

	err = os.MkdirAll(filepath.Dir(exerciseA.LegacyMetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)
	err = os.MkdirAll(filepath.Dir(exerciseB.LegacyMetadataFilepath()), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(exerciseA.LegacyMetadataFilepath(), []byte{}, os.FileMode(0600))
	assert.NoError(t, err)

	ok, err := exerciseA.HasLegacyMetadata()
	assert.NoError(t, err)
	assert.True(t, ok)

	ok, err = exerciseB.HasLegacyMetadata()
	assert.NoError(t, err)
	assert.False(t, ok)
}

func TestNewFromDir(t *testing.T) {
	dir := filepath.Join("something", "another", "whatever", "the-track", "the-exercise")

	exercise := NewExerciseFromDir(dir)
	assert.Equal(t, filepath.Join("something", "another", "whatever"), exercise.Root)
	assert.Equal(t, "the-track", exercise.Track)
	assert.Equal(t, "the-exercise", exercise.Slug)
}

func TestMigrateLegacyMetadataFileWithoutLegacy(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "no-legacy"}
	metadataFilepath := exercise.MetadataFilepath()
	err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasMetadata()
	assert.True(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Nil(t, err)
	assert.Equal(t, "", stderr)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacy(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "legacy"}
	metadataFilepath := exercise.MetadataFilepath()
	legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
	err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasLegacyMetadata()
	assert.True(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.False(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Equal(t, fmt.Sprintf("\nMigrated metadata to %s\n", metadataFilepath), stderr)
	assert.NoError(t, err)
	ok, _ = exercise.HasLegacyMetadata()
	assert.False(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacyAndModern(t *testing.T) {
	ws, err := ioutil.TempDir("", "fake-workspace")
	defer os.RemoveAll(ws)
	assert.NoError(t, err)

	exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "both-legacy-and-modern"}
	metadataFilepath := exercise.MetadataFilepath()
	legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
	err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)
	err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
	assert.NoError(t, err)

	err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
	assert.NoError(t, err)
	ok, _ := exercise.HasLegacyMetadata()
	assert.True(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)

	stderr, err := exercise.MigrateLegacyMetadataFile()

	assert.Equal(t, fmt.Sprintf("\nRemoved legacy metadata: %s\n", legacyMetadataFilepath), stderr)
	assert.NoError(t, err)
	ok, _ = exercise.HasLegacyMetadata()
	assert.False(t, ok)
	ok, _ = exercise.HasMetadata()
	assert.True(t, ok)
}
<|endoftext|>


<commit_before>package ts3sqlib

import (
	"bufio"
	"fmt"
	"log"
	"net"
	"os"
	"strings"
	"sync"
)

const (
	stdPort string = ":10011"
)

//SqConn contains the connection to a ts3 server.
type SqConn struct {
	conn       net.Conn
	logger     *log.Logger
	sendMutex  *sync.Mutex
	receiving  bool
	recvNotify chan string
	recvChan   chan string
	closed     chan bool

	WelcomeMsg string
}

//Dial creates a new SqConn and connects to the ts3 server with the given
//address and returns a pointer to it and an error.
//If the logger is nil, then the standard logger is used.
func Dial(address string, logger *log.Logger) (conn *SqConn, err error) {
	conn = nil

	if !strings.Contains(address, ":") {
		address += stdPort
	}

	connection, err := net.Dial("tcp", address)
	if err != nil {
		return
	}

	/*if logger == nil {
		logger = log.New(os.Stderr, "", log.LstdFlags)
	}*/

	conn = &SqConn{
		conn:       connection,
		logger:     logger,
		sendMutex:  &sync.Mutex{},
		receiving:  true,
		recvNotify: make(chan string),
		recvChan:   make(chan string),
		closed:     make(chan bool),
		WelcomeMsg: "",
	}

	go conn.recv() //goroutine that splits the incoming messages into notify
	//and normal messages.

	if !strings.Contains(<-conn.recvChan, "TS3") {
		conn = nil
		err = fmt.Errorf("no connection to a ts3-server-query")
		return
	}

	conn.WelcomeMsg = <-conn.recvChan

	return
}

//TODO: remove this function...
func (c *SqConn) RecvTest() {
	c.logger = log.New(os.Stderr, "", log.LstdFlags)
	_, _ = c.Send("use 1\n")
	/*c.logger.Println(answer)
	if err != nil {
		c.logger.Println(err)
	}*/

	_, _ = c.Send("quit\n")

	_, _ = c.Send("help login\n")

	c.Close()
}

func (c *SqConn) recv() {
	var err error
	line := ""
	scan := bufio.NewScanner(c.conn)

	for c.receiving {
		//read line
		if !scan.Scan() {
			if err = scan.Err(); err != nil && c.logger != nil {
				c.logger.Print(err)
			}
			c.receiving = false
			c.closed <- true
			break
		}

		line = scan.Text()
		line = strings.Replace(line, string([]byte{13}), "", -1)

		if err = scan.Err(); err != nil {
			if c.logger != nil {
				c.logger.Print(err)
			}
			continue
		}

		//decide if its notify or not and put it to the correct channel
		if strings.HasPrefix(line, "notify") {
			c.recvNotify <- line
		} else {
			c.recvChan <- line
		}
	}
}

//RecvNotify returns a notify message or blocks until one arrives.
func (c *SqConn) RecvNotify() (answer string, err error) {
	answer = ""
	err = nil

	answer = <-c.recvNotify
	return
}

//Close closes the connection to the ts3 server.
func (c *SqConn) Close() {
	c.receiving = false
	c.conn.Close()
}

//Send sends a message to the server and returns the answer and an error.
func (c *SqConn) Send(msg string) (answer string, err error) {
	c.sendMutex.Lock()
	defer c.sendMutex.Unlock()

	if !c.receiving {
		answer = ""
		err = fmt.Errorf("connection closed")
		return
	}

	if c.logger != nil {
		c.logger.Print("send: ", msg)
	}

	answer = ""

	_, err = c.conn.Write([]byte(msg)) //If the return value is smaller than
	if err != nil {                    //the length of msg, it's an error.
		return
	}

	line := ""
	err = nil

	for !isError(line) && c.receiving {
		select {
		case line = <-c.recvChan:
			if isError(line) {
				err = toError(line)
				break
			}

			answer += line + "\n"

		case <-c.closed:
			break
		}
	}

	if err == nil {
		err = NewError(-1, "connection closed", "")
	}

	if MsgEndError.equals(err) {
		err = nil
	}

	//logging
	if c.logger != nil {
		if answer != "" && answer != "\n" {
			c.logger.Println(answer)
		}
		if err != nil {
			c.logger.Println(err)
		}
	}

	return
}
<commit_msg>deleting testing function<commit_after>package ts3sqlib

import (
	"bufio"
	"fmt"
	"log"
	"net"
	"strings"
	"sync"
)

const (
	stdPort string = ":10011"
)

//SqConn contains the connection to a ts3 server.
type SqConn struct {
	conn       net.Conn
	logger     *log.Logger
	sendMutex  *sync.Mutex
	receiving  bool
	recvNotify chan string
	recvChan   chan string
	closed     chan bool

	WelcomeMsg string
}

//Dial creates a new SqConn and connects to the ts3 server with the given
//address and returns a pointer to it and an error.
//If the logger is nil, then the standard logger is used.
func Dial(address string, logger *log.Logger) (conn *SqConn, err error) {
	conn = nil

	if !strings.Contains(address, ":") {
		address += stdPort
	}

	connection, err := net.Dial("tcp", address)
	if err != nil {
		return
	}

	/*if logger == nil {
		logger = log.New(os.Stderr, "", log.LstdFlags)
	}*/

	conn = &SqConn{
		conn:       connection,
		logger:     logger,
		sendMutex:  &sync.Mutex{},
		receiving:  true,
		recvNotify: make(chan string),
		recvChan:   make(chan string),
		closed:     make(chan bool),
		WelcomeMsg: "",
	}

	go conn.recv() //goroutine that splits the incoming messages into notify
	//and normal messages.

	if !strings.Contains(<-conn.recvChan, "TS3") {
		conn = nil
		err = fmt.Errorf("no connection to a ts3-server-query")
		return
	}

	conn.WelcomeMsg = <-conn.recvChan

	return
}

func (c *SqConn) recv() {
	var err error
	line := ""
	scan := bufio.NewScanner(c.conn)

	for c.receiving {
		//read line
		if !scan.Scan() {
			if err = scan.Err(); err != nil && c.logger != nil {
				c.logger.Print(err)
			}
			c.receiving = false
			c.closed <- true
			break
		}

		line = scan.Text()
		line = strings.Replace(line, string([]byte{13}), "", -1)

		if err = scan.Err(); err != nil {
			if c.logger != nil {
				c.logger.Print(err)
			}
			continue
		}

		//decide if its notify or not and put it to the correct channel
		if strings.HasPrefix(line, "notify") {
			c.recvNotify <- line
		} else {
			c.recvChan <- line
		}
	}
}

//RecvNotify returns a notify message or blocks until one arrives.
func (c *SqConn) RecvNotify() (answer string, err error) {
	answer = ""
	err = nil

	answer = <-c.recvNotify
	return
}

//Close closes the connection to the ts3 server.
func (c *SqConn) Close() {
	c.receiving = false
	c.conn.Close()
}

//Send sends a message to the server and returns the answer and an error.
func (c *SqConn) Send(msg string) (answer string, err error) {
	c.sendMutex.Lock()
	defer c.sendMutex.Unlock()

	if !c.receiving {
		answer = ""
		err = fmt.Errorf("connection closed")
		return
	}

	if c.logger != nil {
		c.logger.Print("send: ", msg)
	}

	answer = ""

	_, err = c.conn.Write([]byte(msg)) //If the return value is smaller than
	if err != nil {                    //the length of msg, it's an error.
		return
	}

	line := ""
	err = nil

	for !isError(line) && c.receiving {
		select {
		case line = <-c.recvChan:
			if isError(line) {
				err = toError(line)
				break
			}

			answer += line + "\n"

		case <-c.closed:
			break
		}
	}

	if err == nil {
		err = NewError(-1, "connection closed", "")
	}

	if MsgEndError.equals(err) {
		err = nil
	}

	//logging
	if c.logger != nil {
		if answer != "" && answer != "\n" {
			c.logger.Println(answer)
		}
		if err != nil {
			c.logger.Println(err)
		}
	}

	return
}
<|endoftext|>