forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathappctx.go
More file actions
102 lines (82 loc) · 2.71 KB
/
appctx.go
File metadata and controls
102 lines (82 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package appctx
import (
"sync"
)
// A Key type is used as a key for storing values in the application context.
type Key int
type InitType int
const (
// AppCtxInvokeErrorTraceDataKey is used for storing deferred invoke error cause header value.
// Only used by xray. TODO refactor xray interface so it doesn't use appctx
AppCtxInvokeErrorTraceDataKey Key = iota
// AppCtxRuntimeReleaseKey is used for storing runtime release information (parsed from User_Agent Http header string).
AppCtxRuntimeReleaseKey
// AppCtxInteropServerKey is used to store a reference to the interop server.
AppCtxInteropServerKey
// AppCtxResponseSenderKey is used to store a reference to the response sender
AppCtxResponseSenderKey
// AppCtxFirstFatalErrorKey is used to store first unrecoverable error message encountered to propagate it to slicer with DONE(errortype) or DONEFAIL(errortype)
AppCtxFirstFatalErrorKey
// AppCtxInitType is used to store the init type (init caching or plain INIT)
AppCtxInitType
// AppCtxSandbox type is used to store the sandbox type (SandboxClassic or SandboxPreWarmed)
AppCtxSandboxType
)
// Possible values for AppCtxInitType key
const (
Init InitType = iota
InitCaching
)
// ApplicationContext is an application scope context.
type ApplicationContext interface {
Store(key Key, value interface{})
Load(key Key) (value interface{}, ok bool)
Delete(key Key)
GetOrDefault(key Key, defaultValue interface{}) interface{}
StoreIfNotExists(key Key, value interface{}) interface{}
}
type applicationContext struct {
mux *sync.Mutex
m map[Key]interface{}
}
func (appCtx *applicationContext) Store(key Key, value interface{}) {
appCtx.mux.Lock()
defer appCtx.mux.Unlock()
appCtx.m[key] = value
}
func (appCtx *applicationContext) StoreIfNotExists(key Key, value interface{}) interface{} {
appCtx.mux.Lock()
defer appCtx.mux.Unlock()
existing, found := appCtx.m[key]
if found {
return existing
}
appCtx.m[key] = value
return nil
}
func (appCtx *applicationContext) Load(key Key) (value interface{}, ok bool) {
appCtx.mux.Lock()
defer appCtx.mux.Unlock()
value, ok = appCtx.m[key]
return
}
func (appCtx *applicationContext) Delete(key Key) {
appCtx.mux.Lock()
defer appCtx.mux.Unlock()
delete(appCtx.m, key)
}
func (appCtx *applicationContext) GetOrDefault(key Key, defaultValue interface{}) interface{} {
if value, ok := appCtx.Load(key); ok {
return value
}
return defaultValue
}
// NewApplicationContext returns a new instance of application context.
func NewApplicationContext() ApplicationContext {
return &applicationContext{
mux: &sync.Mutex{},
m: make(map[Key]interface{}),
}
}