GLS Feature Usage
Goroutine local storage for implicitly pass context
Server side enable request context backup
- Option on
- Use the server option
WithContextBackup; - The first parameter
enableindicates that the GLS is enabled; - The second option,
async, means to enable asynchronous implicitly pass-through (indicating that the context in the asynchronous call to go func () is also transparent fallback)
- Use the server option
svr := xxx.NewServer(new(XXXImpl), server.WithContextBackup(true, true))
- Adjust localsession management options by environment variables
- First, enable
WithContextBackupon the Server side. - Configure
CLOUDWEGO_SESSION_CONFIG_KEY ``= [{Whether to enable asynchronous pass-through}] [, {Global sharding number}] [, {GC interval}] in environment variables, all three options are optional, null means use default value- Ex:
true,10,1hmeans, turn on asynchronous + sharding 10 buckets + 1 hour GC interval
- Ex:
- First, enable
Client start request context fallback
- Option on
- Use the client option
WithContextBackup; - The parameter handler represents the backup logic BackupHandler customized by the business.
- Use the client option
func(prev, cur context.Context) (ctx context.Context, backup bool)
Prevparameter represents the context of the backupCurparameter represents the context obtained by the current clientCtxreturn value represents the final context where the user completes processingBackupreturn value indicates whether to continue localsession built-in fallback backup , mainly metainfo Persistent KVS pass-through at present
var expectedKey interface{}
cli := xxx.NewClient(serverName, client.WithContextBackup(func(prev, cur context.Context) (ctx context.Context, backup bool) {
if v := cur.Value(expectedKey); v != nil {
// expectedKey exists, no need for recover context
return cur, false
}
// expectedKey doesn't exists, need recover context from prev
ctx = context.WithValue(cur, expectedKey, prev.Value(expectedKey))
return ctx, true
})
Last modified
May 8, 2024
: style: prettier lint fix (#1069) (dff8f175d9)