-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcs_config.go
89 lines (70 loc) · 2.36 KB
/
gcs_config.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
package gcs_event_store
import (
"context"
"time"
"github.com/honestbank/event-driver/utils/compression"
)
type Operation string
const (
ListContents Operation = "ListContents" // operation that lists all content associated with a given key
ReadContent Operation = "ReadContent" // operation that reads content associated with a key-source pair
WriteContent Operation = "WriteContent" //operation to writes content associated with a key-source pair
)
type GCSConfig struct {
Bucket string
Compressor compression.Compressor
Folder *string
ReadPolicy ReadPolicy
Timeout Timeout
}
type Timeout struct {
Default *time.Duration // the default timeout for all operations
Operation map[Operation]time.Duration // the timeout of each operation - this overrides the default timeout
}
// Config creates a default configuration, that
// - doesn't do compression/decompression when write & read to GCS
// - takes the earliest created object if there are multiple under the same key/source/ path
// - enforces universal 30s timeout in GCS requests
func Config(bucket string) *GCSConfig {
halfMinute := 30 * time.Second
return &GCSConfig{
Bucket: bucket,
Compressor: compression.Noop(),
ReadPolicy: TakeFirstCreated(),
Timeout: Timeout{
Default: &halfMinute,
Operation: make(map[Operation]time.Duration),
},
}
}
func (c *GCSConfig) WithCompressor(compressor compression.Compressor) *GCSConfig {
c.Compressor = compressor
return c
}
func (c *GCSConfig) WithFolder(folder string) *GCSConfig {
c.Folder = &folder
return c
}
func (c *GCSConfig) WithReadPolicy(readPolicy ReadPolicy) *GCSConfig {
c.ReadPolicy = readPolicy
return c
}
func (c *GCSConfig) WithTimeout(timeout Timeout) *GCSConfig {
c.Timeout = timeout
return c
}
// NewContextWithTimeout generates a new context.Context with timeout from the parent context.
// If neither operation-specific timeout nor default timeout is found, return parent context without any operation.
func (c *GCSConfig) NewContextWithTimeout(
parent context.Context,
operation Operation) (context.Context, context.CancelFunc) {
if listContentsTimeout, isConfigured := c.Timeout.Operation[operation]; isConfigured {
return context.WithTimeout(parent, listContentsTimeout)
}
if c.Timeout.Default != nil {
return context.WithTimeout(parent, *c.Timeout.Default)
}
return parent, noop
}
func noop() {
}