Commit dce7b71
fix: resolve thread-safety issues in uptoken package (#171)
* fix: resolve thread-safety issues in uptoken package
Fixes #169
This commit addresses data races in both signer and parser implementations:
1. signer.onceGetUpToken: Added sync.Once to protect concurrent upToken generation
- Added onceUpToken sync.Once field
- Added upTokenErr field to store initialization errors
- Ensures token is generated exactly once across all goroutines
2. parser.GetPutPolicy: Added sync.Once to protect json.Unmarshal
- Added oncePutPolicy sync.Once field
- Added putPolicyErr field to store parsing errors
- Prevents concurrent access to putPolicy field
3. parser.GetAccessKey: Added sync.Once for thread-safe access
- Added onceAccessKey sync.Once field
- Added accessKeyErr field to store errors
- Protects accessKey field from concurrent writes
4. parser.onceGetSplits: Added sync.Once for thread-safe parsing
- Added onceSplits sync.Once field
- Added splitsValid field to track parsing result
- Ensures splits are parsed exactly once
All lazy initialization patterns now use sync.Once which guarantees:
- Thread-safe initialization
- Exactly-once execution
- Proper memory barriers
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: zhangzqs <34616640+zhangzqs@users.noreply.github.com>
* refactor: remove error fields from structs, use mutex instead of sync.Once
Replace sync.Once + error fields pattern with sync.Mutex + initialization flags.
This removes all xxxErr fields (upTokenErr, putPolicyErr, accessKeyErr) from
the structs while maintaining thread-safety through mutex locks.
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: zhangzqs <34616640+zhangzqs@users.noreply.github.com>
* refactor: use sync.Once pattern for thread-safe lazy initialization
- Replace sync.Mutex + boolean flags with sync.Once + error fields
- Ensures initialization happens exactly once across all goroutines
- Properly captures and returns errors from initialization
- Reduces code complexity and improves readability
- Eliminates double-checked locking race conditions
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: YangSen-qn <49863787+YangSen-qn@users.noreply.github.com>
* refactor: remove error fields and enable retry on failure
- Remove credentialsErr, upTokenErr, putPolicyErr, accessKeyErr, splitsErr fields
- Replace sync.Once with sync.Mutex for retry capability
- Check if value was successfully obtained (non-empty/non-nil)
- Allow retry if previous attempt failed to get value
- Maintain thread-safety through mutex locks
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: YangSen-qn <49863787+YangSen-qn@users.noreply.github.com>
---------
Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: zhangzqs <34616640+zhangzqs@users.noreply.github.com>
Co-authored-by: yangsen <yang_sen_ys@163.com>
Co-authored-by: YangSen-qn <49863787+YangSen-qn@users.noreply.github.com>1 parent ae71d53 commit dce7b71
1 file changed
+106
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
41 | 40 | | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
63 | | - | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 72 | + | |
89 | 73 | | |
| 74 | + | |
90 | 75 | | |
91 | 76 | | |
92 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
93 | 80 | | |
94 | | - | |
| 81 | + | |
95 | 82 | | |
| 83 | + | |
96 | 84 | | |
97 | 85 | | |
98 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
99 | 94 | | |
100 | | - | |
| 95 | + | |
| 96 | + | |
101 | 97 | | |
102 | 98 | | |
103 | 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 | + | |
104 | 130 | | |
105 | 131 | | |
106 | 132 | | |
107 | 133 | | |
108 | 134 | | |
109 | | - | |
| 135 | + | |
| 136 | + | |
110 | 137 | | |
| 138 | + | |
111 | 139 | | |
112 | 140 | | |
113 | | - | |
114 | | - | |
115 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
116 | 146 | | |
| 147 | + | |
117 | 148 | | |
118 | 149 | | |
119 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
120 | 156 | | |
121 | | - | |
122 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
123 | 165 | | |
124 | 166 | | |
125 | | - | |
| 167 | + | |
| 168 | + | |
126 | 169 | | |
| 170 | + | |
127 | 171 | | |
128 | 172 | | |
129 | | - | |
130 | | - | |
131 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
132 | 185 | | |
133 | | - | |
| 186 | + | |
| 187 | + | |
134 | 188 | | |
135 | 189 | | |
136 | 190 | | |
137 | | - | |
| 191 | + | |
| 192 | + | |
138 | 193 | | |
139 | | - | |
| 194 | + | |
| 195 | + | |
140 | 196 | | |
| 197 | + | |
| 198 | + | |
141 | 199 | | |
142 | 200 | | |
143 | 201 | | |
144 | 202 | | |
145 | 203 | | |
146 | | - | |
| 204 | + | |
147 | 205 | | |
148 | | - | |
149 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
150 | 214 | | |
151 | 215 | | |
152 | 216 | | |
| |||
0 commit comments