Skip to content

Commit

Permalink
feat: update config using env (#3)
Browse files Browse the repository at this point in the history
* feat: add export API

* test

* check env when init

* not implicitly init default manager

* update readme

* update README.md

* update

* check enable

* simplify backup option

* seperate backup handler

* add BackupHandler type

* back: pass BackupHandler every time

* rename
  • Loading branch information
AsterDY authored Aug 2, 2023
1 parent 66bf42e commit dd21ab6
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 201 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LocalSession

## Introduction
LocalSession is used to **implicitly** manage and transmit context **within** or **between** goroutines.
LocalSession is used to **implicitly** manage and transmit context **within** or **between** goroutines. In canonical way, Go recommands developers to explicitly pass `context.Context` between functions to ensure the downstream callee get desired information from upstream. However this is tedious and ineffecient, resulting in many developers forget (or just don't want) to follow this practice. We have found many cases like that, especially in framework. Therefore, we design and implement a way to implicitly pass application context from root caller to end callee, without troubling intermediate implementation to always bring context.

## Usage
### Session
Expand Down Expand Up @@ -68,10 +68,10 @@ func GetDataX() {
}
```

We provides a `defaultManager` to manage session between different goroutines, thus you don't need to make a SessionManager by your own.
We provide a globally default manager to manage session between different goroutines, as long as you set `InitDefaultManager()` first.

### Explicitly Transmit Async Context (Recommended)
You can use `Go()` or `GoSession()` to initiatively transmit your context to other goroutines.
You can use `Go()` or `GoSession()` to explicitly transmit your context to other goroutines.

```go

Expand All @@ -82,6 +82,11 @@ import (
. "github.com/cloudwego/localsession"
)

func init() {
// initialize default manager first
InitDefaultManager(DefaultManagerOptions())
}

func GetCurSession() Session {
s, ok := CurSession()
if !ok {
Expand Down Expand Up @@ -196,28 +201,23 @@ func main() {
You can also set option `EnableImplicitlyTransmitAsync` as true to transparently transmit context. Once the option is enabled, every goroutine will inherit their parent's session.
```go
func ExampleSessionCtx_EnableImplicitlyTransmitAsync() {
// rest DefaultManager with new Options
// EnableImplicitlyTransmitAsync must be true
ResetDefaultManager(ManagerOptions{
ShardNumber: 10,
EnableImplicitlyTransmitAsync: true,
GCInterval: time.Hour,
})

// WARNING: pprof.Do() must be called before BindSession(),
// WARNING: if you want to use `pprof.Do()`, it must be called before `BindSession()`,
// otherwise transparently transmitting session will be dysfunctional
labels := pprof.Labels("c", "d")
pprof.Do(context.Background(), labels, func(ctx context.Context){})
// labels := pprof.Labels("c", "d")
// pprof.Do(context.Background(), labels, func(ctx context.Context){})

s := NewSessionMap(map[interface{}]interface{}{
"a": "b",
})
BindSession(s)

// WARNING: pprof.Do() must be called before BindSession(),
// otherwise transparently transmitting session will be dysfunctional
// labels := pprof.Labels("c", "d")
// pprof.Do(context.Background(), labels, func(ctx context.Context){})

wg := sync.WaitGroup{}
wg.Add(3)
go func() {
Expand Down
Loading

0 comments on commit dd21ab6

Please sign in to comment.