-
Notifications
You must be signed in to change notification settings - Fork 12
/
sharedptr_internal.go
178 lines (151 loc) · 3.53 KB
/
sharedptr_internal.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package resilience
import (
"context"
"time"
logrus2 "github.com/searKing/golang/third_party/github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
type sharedPtr struct {
// New optionally specifies a function to generate
// a value when Get would otherwise return nil.
// It may not be changed concurrently with calls to Get.
// readonly
New func() (Ptr, error)
*logrus2.FieldLogger
// to judge whether Get&Construct is timeout
// readonly
ConstructTimeout time.Duration
// MaxDuration for retry if tasks failed
// readonly
TaskMaxRetryDuration time.Duration
// ctx is either the client or server context. It should only
// be modified via copying the whole Request using WithContext.
// It is unexported to prevent people from using Context wrong
// and mutating the contexts held by callers of the same request.
ctx context.Context
x Ptr
taskC chan *Task
tasks map[string]*Task
eventC chan Event
}
func newSharedPtrSafe(ctx context.Context, new func() (Ptr, error), l logrus.FieldLogger) *sharedPtr {
return &sharedPtr{
New: new,
FieldLogger: logrus2.New(l),
TaskMaxRetryDuration: DefaultResilienceTaskMaxRetryDuration,
ConstructTimeout: DefaultResilienceConstructTimeout,
ctx: ctx,
}
}
//
// The returned context is always non-nil; it defaults to the
// background context.
func (sp *sharedPtr) Context() context.Context {
if sp.ctx != nil {
return sp.ctx
}
return context.Background()
}
func (sp *sharedPtr) InShutdown() bool {
select {
case <-sp.Context().Done():
return true
default:
return false
}
}
// std::shared_ptr.release()
func (sp *sharedPtr) Release() Ptr {
x := sp.x
sp.x = nil
return x
}
// std::shared_ptr.reset()
func (sp *sharedPtr) Reset(ptr Ptr) {
sp.resetPtr(ptr)
}
// std::shared_ptr.get()
func (sp *sharedPtr) Get() Ptr {
return sp.x
}
// reset ptr and ready to start again
func (sp *sharedPtr) resetPtr(ptr Ptr) {
oldPtr := sp.Release()
if oldPtr != nil {
oldPtr.Close()
}
sp.x = ptr
return
}
func (sp *sharedPtr) GetTaskC() chan *Task {
if sp.taskC == nil {
sp.taskC = make(chan *Task)
}
return sp.taskC
}
func (sp *sharedPtr) Event() chan Event {
if sp.eventC == nil {
sp.eventC = make(chan Event)
}
return sp.eventC
}
func (sp *sharedPtr) RemoveAllTask() {
for _, id := range sp.TaskIds() {
sp.RemoveTaskById(id, true)
}
}
func (sp *sharedPtr) TaskIds() []string {
var ids []string
for id := range sp.tasks {
ids = append(ids, id)
}
return ids
}
func (sp *sharedPtr) SnapTasks() map[string]*Task {
tasks := sp.tasks
sp.tasks = nil
return tasks
}
func (sp *sharedPtr) GetTasks() map[string]*Task {
return sp.tasks
}
func (sp *sharedPtr) GetTaskById(id string) (task *Task, has bool) {
task, has = sp.tasks[id]
return
}
func (sp *sharedPtr) AddTask(task *Task) (has bool) {
if task == nil {
return
}
if sp.tasks == nil {
sp.tasks = make(map[string]*Task)
}
sp.tasks[task.ID()] = task
return
}
func (sp *sharedPtr) RemoveTask(task *Task) {
if task == nil {
return
}
sp.RemoveTaskById(task.ID(), true)
return
}
func (sp *sharedPtr) RemoveAllTasks() {
for _, id := range sp.TaskIds() {
sp.RemoveTaskById(id, true)
}
}
func (sp *sharedPtr) RemoveTaskById(id string, cancel bool) {
t, has := sp.tasks[id]
if !has || t == nil {
return
}
if cancel && t.CancelFn != nil {
t.CancelFn()
}
delete(sp.tasks, t.ID())
return
}