| 1 | // Package transfer collects together adapters for uploading and downloading LFS content |
| 2 | // NOTE: Subject to change, do not rely on this package from outside git-lfs source |
| 3 | package tq |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 11 | "github.com/git-lfs/git-lfs/v3/tools" |
| 12 | "github.com/git-lfs/git-lfs/v3/tr" |
| 13 | ) |
| 14 | |
| 15 | type Direction int |
| 16 | |
| 17 | const ( |
| 18 | Upload = Direction(iota) |
| 19 | Download = Direction(iota) |
| 20 | Checkout = Direction(iota) |
| 21 | ) |
| 22 | |
| 23 | // Progress returns a string containing the operation in progress. |
| 24 | func (d Direction) Progress() string { |
| 25 | switch d { |
| 26 | case Checkout: |
| 27 | return tr.Tr.Get("Checking out LFS objects") |
| 28 | case Download: |
| 29 | return tr.Tr.Get("Downloading LFS objects") |
| 30 | case Upload: |
| 31 | return tr.Tr.Get("Uploading LFS objects") |
| 32 | default: |
| 33 | return "<unknown>" |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func (d Direction) String() string { |
| 38 | switch d { |
| 39 | case Checkout: |
| 40 | return "checkout" |
| 41 | case Download: |
| 42 | return "download" |
| 43 | case Upload: |
| 44 | return "upload" |
| 45 | default: |
| 46 | return "<unknown>" |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | type Transfer struct { |
| 51 | Name string `json:"name,omitempty"` |
| 52 | Oid string `json:"oid,omitempty"` |
| 53 | Size int64 `json:"size"` |
| 54 | Authenticated bool `json:"authenticated,omitempty"` |
| 55 | Actions ActionSet `json:"actions,omitempty"` |
| 56 | Links ActionSet `json:"_links,omitempty"` |
| 57 | Error *ObjectError `json:"error,omitempty"` |
| 58 | Path string `json:"path,omitempty"` |
| 59 | Missing bool `json:"-"` |
| 60 | } |
| 61 | |
| 62 | func (t *Transfer) Rel(name string) (*Action, error) { |
| 63 | a, err := t.Actions.Get(name) |
| 64 | if a != nil || err != nil { |
| 65 | return a, err |
| 66 | } |
| 67 | |
| 68 | if t.Links != nil { |
| 69 | a, err := t.Links.Get(name) |
| 70 | if a != nil || err != nil { |
| 71 | return a, err |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return nil, nil |
| 76 | } |
| 77 | |
| 78 | type ObjectError struct { |
| 79 | Code int `json:"code"` |
| 80 | Message string `json:"message"` |
| 81 | } |
| 82 | |
| 83 | func (e *ObjectError) Error() string { |
| 84 | return fmt.Sprintf("[%d] %s", e.Code, e.Message) |
| 85 | } |
| 86 | |
| 87 | // newTransfer returns a copy of the given Transfer, with the name and path |
| 88 | // values set. |
| 89 | func newTransfer(tr *Transfer, name string, path string) *Transfer { |
| 90 | t := &Transfer{ |
| 91 | Name: name, |
| 92 | Path: path, |
| 93 | Oid: tr.Oid, |
| 94 | Size: tr.Size, |
| 95 | Authenticated: tr.Authenticated, |
| 96 | Actions: make(ActionSet), |
| 97 | } |
| 98 | |
| 99 | if tr.Error != nil { |
| 100 | t.Error = &ObjectError{ |
| 101 | Code: tr.Error.Code, |
| 102 | Message: tr.Error.Message, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | for rel, action := range tr.Actions { |
| 107 | t.Actions[rel] = &Action{ |
| 108 | Href: action.Href, |
| 109 | Header: action.Header, |
| 110 | ExpiresAt: action.ExpiresAt, |
| 111 | ExpiresIn: action.ExpiresIn, |
| 112 | Id: action.Id, |
| 113 | Token: action.Token, |
| 114 | createdAt: action.createdAt, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if tr.Links != nil { |
| 119 | t.Links = make(ActionSet) |
| 120 | |
| 121 | for rel, link := range tr.Links { |
| 122 | t.Links[rel] = &Action{ |
| 123 | Href: link.Href, |
| 124 | Header: link.Header, |
| 125 | ExpiresAt: link.ExpiresAt, |
| 126 | ExpiresIn: link.ExpiresIn, |
| 127 | Id: link.Id, |
| 128 | Token: link.Token, |
| 129 | createdAt: link.createdAt, |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return t |
| 135 | } |
| 136 | |
| 137 | type Action struct { |
| 138 | Href string `json:"href"` |
| 139 | Header map[string]string `json:"header,omitempty"` |
| 140 | ExpiresAt time.Time `json:"expires_at,omitempty"` |
| 141 | ExpiresIn int `json:"expires_in,omitempty"` |
| 142 | Id string `json:"-"` |
| 143 | Token string `json:"-"` |
| 144 | |
| 145 | createdAt time.Time |
| 146 | } |
| 147 | |
| 148 | func (a *Action) IsExpiredWithin(d time.Duration) (time.Time, bool) { |
| 149 | return tools.IsExpiredAtOrIn(a.createdAt, d, a.ExpiresAt, time.Duration(a.ExpiresIn)*time.Second) |
| 150 | } |
| 151 | |
| 152 | type ActionSet map[string]*Action |
| 153 | |
| 154 | const ( |
| 155 | // objectExpirationToTransfer is the duration we expect to have passed |
| 156 | // from the time that the object's expires_at (or expires_in) property |
| 157 | // is checked to when the transfer is executed. |
| 158 | objectExpirationToTransfer = 5 * time.Second |
| 159 | ) |
| 160 | |
| 161 | func (as ActionSet) Get(rel string) (*Action, error) { |
| 162 | a, ok := as[rel] |
| 163 | if !ok { |
| 164 | return nil, nil |
| 165 | } |
| 166 | |
| 167 | if at, expired := a.IsExpiredWithin(objectExpirationToTransfer); expired { |
| 168 | return nil, errors.NewRetriableError(&ActionExpiredErr{Rel: rel, At: at}) |
| 169 | } |
| 170 | |
| 171 | return a, nil |
| 172 | } |
| 173 | |
| 174 | type ActionExpiredErr struct { |
| 175 | Rel string |
| 176 | At time.Time |
| 177 | } |
| 178 | |
| 179 | func (e ActionExpiredErr) Error() string { |
| 180 | return tr.Tr.Get("action %q expires at %s", |
| 181 | e.Rel, e.At.In(time.Local).Format(time.RFC822)) |
| 182 | } |
| 183 | |
| 184 | func IsActionExpiredError(err error) bool { |
| 185 | if _, ok := err.(*ActionExpiredErr); ok { |
| 186 | return true |
| 187 | } |
| 188 | return false |
| 189 | } |
| 190 | |
| 191 | // NewAdapterFunc creates new instances of Adapter. Code that wishes |
| 192 | // to provide new Adapter instances should pass an implementation of this |
| 193 | // function to RegisterNewTransferAdapterFunc() on a *Manifest. |
| 194 | // name and dir are to provide context if one func implements many instances |
| 195 | type NewAdapterFunc func(name string, dir Direction) Adapter |
| 196 | |
| 197 | type ProgressCallback func(name string, totalSize, readSoFar int64, readSinceLast int) error |
| 198 | |
| 199 | type AdapterConfig interface { |
| 200 | APIClient() *lfsapi.Client |
| 201 | ConcurrentTransfers() int |
| 202 | Remote() string |
| 203 | } |
| 204 | |
| 205 | type adapterConfig struct { |
| 206 | apiClient *lfsapi.Client |
| 207 | concurrentTransfers int |
| 208 | remote string |
| 209 | } |
| 210 | |
| 211 | func (c *adapterConfig) ConcurrentTransfers() int { |
| 212 | return c.concurrentTransfers |
| 213 | } |
| 214 | |
| 215 | func (c *adapterConfig) APIClient() *lfsapi.Client { |
| 216 | return c.apiClient |
| 217 | } |
| 218 | |
| 219 | func (c *adapterConfig) Remote() string { |
| 220 | return c.remote |
| 221 | } |
| 222 | |
| 223 | // Adapter is implemented by types which can upload and/or download LFS |
| 224 | // file content to a remote store. Each Adapter accepts one or more requests |
| 225 | // which it may schedule and parallelise in whatever way it chooses, clients of |
| 226 | // this interface will receive notifications of progress and completion asynchronously. |
| 227 | // TransferAdapters support transfers in one direction; if an implementation |
| 228 | // provides support for upload and download, it should be instantiated twice, |
| 229 | // advertising support for each direction separately. |
| 230 | // Note that Adapter only implements the actual upload/download of content |
| 231 | // itself; organising the wider process including calling the API to get URLs, |
| 232 | // handling progress reporting and retries is the job of the core TransferQueue. |
| 233 | // This is so that the orchestration remains core & standard but Adapter |
| 234 | // can be changed to physically transfer to different hosts with less code. |
| 235 | type Adapter interface { |
| 236 | // Name returns the name of this adapter, which is the same for all instances |
| 237 | // of this type of adapter |
| 238 | Name() string |
| 239 | // Direction returns whether this instance is an upload or download instance |
| 240 | // Adapter instances can only be one or the other, although the same |
| 241 | // type may be instantiated for each direction |
| 242 | Direction() Direction |
| 243 | // Begin a new batch of uploads or downloads. Call this first, followed by one |
| 244 | // or more Add calls. The passed in callback will receive updates on progress. |
| 245 | Begin(cfg AdapterConfig, cb ProgressCallback) error |
| 246 | // Add queues a download/upload, which will complete asynchronously and |
| 247 | // notify the callbacks given to Begin() |
| 248 | Add(transfers ...*Transfer) (results <-chan TransferResult) |
| 249 | // Indicate that all transfers have been scheduled and resources can be released |
| 250 | // once the queued items have completed. |
| 251 | // This call blocks until all items have been processed |
| 252 | End() |
| 253 | } |
| 254 | |
| 255 | // Result of a transfer returned through CompletionChannel() |
| 256 | type TransferResult struct { |
| 257 | Transfer *Transfer |
| 258 | // This will be non-nil if there was an error transferring this item |
| 259 | Error error |
| 260 | } |