feat(spanner): independent-affinity-keys#8153
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements affinity key support for multiplexed sessions in the Spanner client. It introduces logic to generate and attach a UUID as an affinity key to snapshots and transactions, which is then included in gRPC request headers (x-grpc-gcp-affinity-key). The changes also include unbinding logic during commit and rollback operations. Review feedback suggests using 'import type' to maintain type safety for the Spanner class without circular dependencies and recommends avoiding direct mutation of configuration objects to prevent unintended side effects during request execution.
| this.request = (config: any, callback: Function) => { | ||
| if (this._affinityKey) { | ||
| config.headers = config.headers || {}; | ||
| config.headers['x-grpc-gcp-affinity-key'] = this._affinityKey; | ||
| } | ||
| return session.request(config, callback); | ||
| }; | ||
|
|
||
| this.requestStream = (config: any) => { | ||
| if (this._affinityKey) { | ||
| config.headers = config.headers || {}; | ||
| config.headers['x-grpc-gcp-affinity-key'] = this._affinityKey; | ||
| } | ||
| return session.requestStream(config); | ||
| }; |
There was a problem hiding this comment.
The current implementation mutates the config object by directly modifying its headers property. This can lead to unexpected side effects if the caller reuses the configuration object for multiple requests. It is safer to create a shallow copy of the configuration and its headers to ensure immutability of the input.
this.request = (config: any, callback: Function) => {
const requestConfig = this._affinityKey
? Object.assign({}, config, {
headers: Object.assign({}, config.headers, {
'x-grpc-gcp-affinity-key': this._affinityKey,
}),
})
: config;
return session.request(requestConfig, callback);
};
this.requestStream = (config: any) => {
const requestConfig = this._affinityKey
? Object.assign({}, config, {
headers: Object.assign({}, config.headers, {
'x-grpc-gcp-affinity-key': this._affinityKey,
}),
})
: config;
return session.requestStream(requestConfig);
};200d880 to
48edbb9
Compare
48edbb9 to
5cab403
Compare
do not review[experimental PR]