-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: s8sg <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type DefaultStateStore struct { | ||
flowName string | ||
requestId string | ||
keyDecorator func(string) string | ||
cache map[string]string | ||
mux sync.Mutex | ||
} | ||
|
||
// Configure the StateStore with flow name and request ID | ||
func (ss *DefaultStateStore) Configure(flowName string, requestId string) { | ||
ss.keyDecorator = func(key string) string { | ||
dKey := flowName + "-" + requestId + "-" + key | ||
return dKey | ||
} | ||
} | ||
|
||
// Initialize the StateStore (called only once in a request span) | ||
func (ss *DefaultStateStore) Init() error { | ||
ss.cache = make(map[string]string) | ||
return nil | ||
} | ||
|
||
// Set a value (override existing, or create one) | ||
func (ss *DefaultStateStore) Set(key string, value string) error { | ||
ss.mux.Lock() | ||
ss.cache[ss.keyDecorator(key)] = value | ||
ss.mux.Unlock() | ||
return nil | ||
} | ||
|
||
// Get a value | ||
func (ss *DefaultStateStore) Get(key string) (string, error) { | ||
ss.mux.Lock() | ||
value, ok := ss.cache[ss.keyDecorator(key)] | ||
ss.mux.Unlock() | ||
if !ok { | ||
return "", fmt.Errorf("key not found") | ||
} | ||
return value, nil | ||
} | ||
|
||
// Compare and Update a value | ||
func (ss *DefaultStateStore) Update(key string, oldValue string, newValue string) error { | ||
ss.mux.Lock() | ||
value, ok := ss.cache[ss.keyDecorator(key)] | ||
if !ok { | ||
ss.mux.Unlock() | ||
return fmt.Errorf("key not found") | ||
} | ||
if value != oldValue { | ||
ss.mux.Unlock() | ||
return fmt.Errorf("value doesn't match") | ||
} | ||
ss.cache[ss.keyDecorator(key)] = newValue | ||
ss.mux.Unlock() | ||
return nil | ||
} | ||
|
||
// Cleanup all the resources in StateStore (called only once in a request span) | ||
func (ss *DefaultStateStore) Cleanup() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters