|
| 1 | +/*************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2026, Pelican Project, Morgridge Institute for Research |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 6 | + * may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ***************************************************************/ |
| 18 | + |
| 19 | +package origin_serve |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/spf13/afero" |
| 26 | + "golang.org/x/time/rate" |
| 27 | + |
| 28 | + "github.com/pelicanplatform/pelican/byte_rate" |
| 29 | +) |
| 30 | + |
| 31 | +type rateLimitedFs struct { |
| 32 | + afero.Fs |
| 33 | + limiter *rate.Limiter |
| 34 | +} |
| 35 | + |
| 36 | +type rateLimitedFile struct { |
| 37 | + afero.File |
| 38 | + limiter *rate.Limiter |
| 39 | +} |
| 40 | + |
| 41 | +func newRateLimitedFs(fs afero.Fs, rateLimit byte_rate.ByteRate) afero.Fs { |
| 42 | + if rateLimit <= 0 { |
| 43 | + return fs |
| 44 | + } |
| 45 | + limit := int(rateLimit) |
| 46 | + limiter := rate.NewLimiter(rate.Limit(limit), limit) |
| 47 | + return &rateLimitedFs{Fs: fs, limiter: limiter} |
| 48 | +} |
| 49 | + |
| 50 | +func (r *rateLimitedFs) Open(name string) (afero.File, error) { |
| 51 | + file, err := r.Fs.Open(name) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return &rateLimitedFile{File: file, limiter: r.limiter}, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (r *rateLimitedFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { |
| 59 | + file, err := r.Fs.OpenFile(name, flag, perm) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + return &rateLimitedFile{File: file, limiter: r.limiter}, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (f *rateLimitedFile) Read(p []byte) (int, error) { |
| 67 | + n, err := f.File.Read(p) |
| 68 | + if n > 0 { |
| 69 | + if waitErr := f.waitN(n); waitErr != nil { |
| 70 | + return n, waitErr |
| 71 | + } |
| 72 | + } |
| 73 | + return n, err |
| 74 | +} |
| 75 | + |
| 76 | +func (f *rateLimitedFile) Write(p []byte) (int, error) { |
| 77 | + n, err := f.File.Write(p) |
| 78 | + if n > 0 { |
| 79 | + if waitErr := f.waitN(n); waitErr != nil { |
| 80 | + return n, waitErr |
| 81 | + } |
| 82 | + } |
| 83 | + return n, err |
| 84 | +} |
| 85 | + |
| 86 | +func (f *rateLimitedFile) ReadAt(p []byte, off int64) (int, error) { |
| 87 | + n, err := f.File.ReadAt(p, off) |
| 88 | + if n > 0 { |
| 89 | + if waitErr := f.waitN(n); waitErr != nil { |
| 90 | + return n, waitErr |
| 91 | + } |
| 92 | + } |
| 93 | + return n, err |
| 94 | +} |
| 95 | + |
| 96 | +func (f *rateLimitedFile) WriteAt(p []byte, off int64) (int, error) { |
| 97 | + n, err := f.File.WriteAt(p, off) |
| 98 | + if n > 0 { |
| 99 | + if waitErr := f.waitN(n); waitErr != nil { |
| 100 | + return n, waitErr |
| 101 | + } |
| 102 | + } |
| 103 | + return n, err |
| 104 | +} |
| 105 | + |
| 106 | +func (f *rateLimitedFile) waitN(n int) error { |
| 107 | + remaining := n |
| 108 | + for remaining > 0 { |
| 109 | + burst := f.limiter.Burst() |
| 110 | + if burst <= 0 { |
| 111 | + burst = 1 |
| 112 | + } |
| 113 | + step := remaining |
| 114 | + if step > burst { |
| 115 | + step = burst |
| 116 | + } |
| 117 | + if err := f.limiter.WaitN(context.Background(), step); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + remaining -= step |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
0 commit comments