Skip to content

Commit

Permalink
add rate limiter to node CSI
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiaxun committed Mar 4, 2024
1 parent ed7857b commit aae6b87
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/csi_driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/util"
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook"
"golang.org/x/net/context"
"golang.org/x/time/rate"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -60,6 +61,7 @@ type nodeServer struct {
mounter mount.Interface
volumeLocks *util.VolumeLocks
k8sClients clientset.Interface
limiter rate.Limiter
}

func newNodeServer(driver *GCSDriver, mounter mount.Interface) csi.NodeServer {
Expand All @@ -69,6 +71,7 @@ func newNodeServer(driver *GCSDriver, mounter mount.Interface) csi.NodeServer {
mounter: mounter,
volumeLocks: util.NewVolumeLocks(),
k8sClients: driver.config.K8sClients,
limiter: *rate.NewLimiter(rate.Every(time.Second), 5),
}
}

Expand All @@ -85,6 +88,11 @@ func (s *nodeServer) NodeGetCapabilities(_ context.Context, _ *csi.NodeGetCapabi
}

func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
// Rate limit NodePublishVolume calls to avoid kube API throttling.
if s.limiter.Wait(ctx) != nil {
return nil, status.Errorf(codes.Aborted, "NodePublishVolume request is aborted due to rate limit")
}

// Validate arguments
bucketName := req.GetVolumeId()
vc := req.GetVolumeContext()
Expand Down

0 comments on commit aae6b87

Please sign in to comment.