-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgcslock.go
240 lines (204 loc) · 7.4 KB
/
gcslock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2023 The 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 gcslock acquires a forward-looking lock leveraging a file in a Google
// Cloud Storage bucket. Unlike a mutex, the lock is TTL-based instead of "held"
// like a traditional mutex.
//
// Compared to other mutexes, this is intended to be a long-lived lock. The
// minimum granularity is "seconds" and most consumers will acquire a lock for
// "minutes" or "hours". Because of clock skew and network latency, granularity
// below 1s is not a supported use case.
package gcslock
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"cloud.google.com/go/storage"
"github.com/sethvargo/go-retry"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
const (
// userAgent is the unqiue identifier for upstream API calls.
userAgent = "gcslock/1.0 (+https://github.com/sethvargo/go-gcslock)"
// defaultCacheControl is the default value for the Cache-Control header.
defaultCacheControl = "private, no-cache, no-store, no-transform, max-age=0"
// defaultChunkSize is the default chunking size. Files are metadata-only, so
// we intentionally make this very small.
defaultChunkSize = 1024
// notBeforeKey is the metadata key where the not-before timestamp is stored.
notBeforeKey = "nbf"
)
// Lockable is the interface that defines how to manage a lock with Google Cloud
// Storage.
type Lockable interface {
Acquire(ctx context.Context, ttl time.Duration) error
Close(ctx context.Context) error
}
var _ error = (*LockHeldError)(nil)
// LockHeldError is a specific error returned when a lock is alread held.
type LockHeldError struct {
nbf int64
}
// NewLockHeldError creates an instance of a LockHeldError.
func NewLockHeldError(nbf int64) *LockHeldError {
return &LockHeldError{
nbf: nbf,
}
}
// Error implements the error interface.
func (e *LockHeldError) Error() string {
return "lock held until " + e.NotBefore().Format(time.RFC3339)
}
// NotBefore returns the UTC Unix timestamp of when the lock expires.
func (e *LockHeldError) NotBefore() time.Time {
return time.Unix(e.nbf, 0).UTC()
}
// Is implements the error comparison interface.
func (e *LockHeldError) Is(err error) bool {
var terr *LockHeldError
return errors.As(err, &terr)
}
// Verify that the Lock implements the interface.
var _ Lockable = (*Lock)(nil)
// Lock represents a remote forward-looking lock in Google Cloud Storage.
type Lock struct {
client *storage.Client
bucket string
object string
retryPolicy retry.Backoff
}
// New creates a new distributed locking handler on the specific object in
// Google Cloud. It does create the lock until Acquire is called.
func New(ctx context.Context, bucket, object string, opts ...option.ClientOption) (*Lock, error) {
// Append our user agent, but make it first so that subsequent options can
// override it.
opts = append([]option.ClientOption{option.WithUserAgent(userAgent)}, opts...)
// Create the Google Cloud Storage client.
client, err := storage.NewClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create storage client: %w", err)
}
// Set a default retry policy. This is for failed API calls, not for failed
// lock attempts.
retryPolicy := retry.WithMaxRetries(5, retry.NewFibonacci(50*time.Millisecond))
return &Lock{
client: client,
bucket: bucket,
object: object,
retryPolicy: retryPolicy,
}, nil
}
// Acquire attempts to acquire the lock. It returns [ErrLockHeld] if the lock is
// already held. Callers can cast the error type to get more specific
// information like the TTL expiration time:
//
// if err := lock.Acquire(ctx, 5*time.Minute); err != nil {
// var lockErr *ErrLockHeld
// if errors.As(err, &lockErr) {
// log.Printf("lock is held until %s", lockErr.NotBefore())
// }
// }
//
// It automatically retries transient upstream API errors, but returns
// immediately for errors that are irrecoverable.
func (l *Lock) Acquire(ctx context.Context, ttl time.Duration) error {
now := time.Now().UTC()
if err := retry.Do(ctx, l.retryPolicy, func(ctx context.Context) error {
return l.tryAcquire(ctx, now, ttl)
}); err != nil {
return fmt.Errorf("failed to acquire lock: %w", err)
}
return nil
}
// Close terminates the client connection. It does not delete the lock.
func (l *Lock) Close(_ context.Context) error {
if err := l.client.Close(); err != nil {
return fmt.Errorf("failed to close storage client: %w", err)
}
return nil
}
// tryAcquire is the internal implementation of [Acquire] that actually creates
// and updates the lock.
func (l *Lock) tryAcquire(ctx context.Context, now time.Time, ttl time.Duration) error {
now = now.Truncate(time.Second)
ttl = ttl.Truncate(time.Second)
objHandle := l.client.Bucket(l.bucket).Object(l.object)
// Try to get the attributes on the object.
attrs, err := objHandle.Attrs(ctx)
if err != nil && !errors.Is(err, storage.ErrObjectNotExist) {
return fmt.Errorf("failed to get storage object: %w", err)
}
// If we found the object, check if the lock is valid and held.
if attrs != nil && attrs.Metadata != nil {
nbf, ok := attrs.Metadata[notBeforeKey]
if !ok {
nbf = "0"
}
nbfUnix, err := strconv.ParseInt(nbf, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse nbf as an integer: %w", err)
}
if nbfUnix >= now.Unix() {
return NewLockHeldError(nbfUnix)
}
}
// If we got this far, it means the lock object either does not exist, or it
// exists but is past the TTL. Therefore we need to create/update the file,
// taking into account the possibility that competing processes are fighting
// for the lock.
// If generation and metageneration are 0, then we should only create the
// object if it does not exist. Otherwise, we should only perform an update if
// the metagenerations match.
var conds storage.Conditions
if attrs == nil {
// The object did not exist, so ensure it does not exist when we write.
conds = storage.Conditions{
DoesNotExist: true,
}
} else {
// The object exists, so set metadata to ensure we catch a race.
conds = storage.Conditions{
GenerationMatch: attrs.Generation,
MetagenerationMatch: attrs.Metageneration,
}
}
w := objHandle.If(conds).NewWriter(ctx)
w.CacheControl = defaultCacheControl
w.ChunkSize = defaultChunkSize
w.SendCRC32C = true
if w.Metadata == nil {
w.Metadata = make(map[string]string)
}
w.Metadata[notBeforeKey] = strconv.FormatInt(now.Add(ttl).Unix(), 10)
// Write the metadata back to the object.
if err := w.Close(); err != nil {
var googleErr *googleapi.Error
if errors.As(err, &googleErr) {
switch googleErr.Code {
case http.StatusNotFound:
// The object was deleted between when we read attributes and now.
return retry.RetryableError(err)
case http.StatusPreconditionFailed:
// The object was modified between when we read attributes and now.
return retry.RetryableError(err)
}
}
return fmt.Errorf("failed to update object: %w", err)
}
return nil
}