-
Notifications
You must be signed in to change notification settings - Fork 59
/
readconfig.go
145 lines (117 loc) · 3.07 KB
/
readconfig.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
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
)
// ReadConfig constitutes config from env variables
type ReadConfig struct {
}
const DefaultMaxReconnect = 120
const DefaultReconnectDelay = time.Second * 2
func (ReadConfig) Read() (QueueWorkerConfig, error) {
cfg := QueueWorkerConfig{
AckWait: time.Second * 30,
MaxInflight: 1,
}
if val, exists := os.LookupEnv("faas_nats_address"); exists {
cfg.NatsAddress = val
} else {
cfg.NatsAddress = "nats"
}
if value, exists := os.LookupEnv("faas_nats_port"); exists {
val, err := strconv.Atoi(value)
if err != nil {
log.Println("converting faas_nats_port to int error:", err)
} else {
cfg.NatsPort = val
}
} else {
cfg.NatsPort = 4222
}
if val, exists := os.LookupEnv("faas_nats_cluster_name"); exists {
cfg.NatsClusterName = val
} else {
cfg.NatsClusterName = "faas-cluster"
}
if val, exists := os.LookupEnv("faas_nats_queue_group"); exists && val != "" {
cfg.NatsQueueGroup = val
} else {
cfg.NatsQueueGroup = "faas"
}
if val, exists := os.LookupEnv("faas_gateway_address"); exists {
cfg.GatewayAddress = val
} else {
cfg.GatewayAddress = "gateway"
}
if value, exists := os.LookupEnv("faas_gateway_port"); exists {
val, err := strconv.Atoi(value)
if err != nil {
return QueueWorkerConfig{}, fmt.Errorf("converting faas_gateway_port %s to int error: %s", value, err)
}
cfg.GatewayPort = val
} else {
cfg.GatewayPort = 8080
}
if val, exists := os.LookupEnv("faas_print_body"); exists {
if val == "1" || val == "true" {
cfg.DebugPrintBody = true
} else {
cfg.DebugPrintBody = false
}
}
if val, exists := os.LookupEnv("write_debug"); exists {
if val == "1" || val == "true" {
cfg.WriteDebug = true
} else {
cfg.WriteDebug = false
}
}
cfg.MaxReconnect = DefaultMaxReconnect
if value, exists := os.LookupEnv("faas_max_reconnect"); exists {
val, err := strconv.Atoi(value)
if err != nil {
log.Println("converting faas_max_reconnect to int error:", err)
} else {
cfg.MaxReconnect = val
}
}
cfg.ReconnectDelay = DefaultReconnectDelay
if value, exists := os.LookupEnv("faas_reconnect_delay"); exists {
reconnectDelayVal, durationErr := time.ParseDuration(value)
if durationErr != nil {
log.Println("parse env var: faas_reconnect_delay as time.Duration error:", durationErr)
} else {
cfg.ReconnectDelay = reconnectDelayVal
}
}
if val, exists := os.LookupEnv("ack_wait"); exists {
ackWaitVal, durationErr := time.ParseDuration(val)
if durationErr != nil {
log.Println("ack_wait error:", durationErr)
} else {
cfg.AckWait = ackWaitVal
}
}
return cfg, nil
}
type QueueWorkerConfig struct {
NatsAddress string
NatsPort int
NatsClusterName string
NatsQueueGroup string
GatewayAddress string
FunctionSuffix string
GatewayPort int
MaxInflight int
MaxReconnect int
AckWait time.Duration
ReconnectDelay time.Duration
DebugPrintBody bool
WriteDebug bool
}
func (q QueueWorkerConfig) GatewayAddressURL() string {
return fmt.Sprintf("%s:%d", q.GatewayAddress, q.GatewayPort)
}