-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.go
More file actions
142 lines (118 loc) · 3.02 KB
/
Copy pathproxy.go
File metadata and controls
142 lines (118 loc) · 3.02 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
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
/*
* Licensed Materials - Property of tenxcloud.com
* (C) Copyright 2019 TenxCloud. All Rights Reserved.
* 2019-03-20 @author lizhen
*/
package serviced
import (
"reflect"
"sync"
)
type TransparentProxy interface {
Call(method string, arguments ...interface{}) (returnValues []interface{}, err error)
}
type proxy struct {
Service reflect.Type
Kernel Kernel
Serializer Serializer
Invocations Invocations
InvocationQueue InvocationQueue
}
func (p proxy) ensureMethod(method string) (err error) {
if _, exist := p.Service.MethodByName(method); !exist {
err = ErrNoSuchMethod
}
return
}
func (p proxy) prepareArguments(arguments []interface{}) (objects []object, err error) {
objects, err = interfaceToObject(p.Serializer, arguments)
return
}
func buildRequestMessage(service, method string) *Message {
return &Message{
RequestID: NewUUID(service, method),
IsResponse: false,
Service: service,
Method: method,
}
}
func (p proxy) prepareReturnValues(response *Message) (returnValues []interface{}, err error) {
returnValues, err = objectToInterface(p.Kernel, p.Serializer, response.ReturnValues)
return
}
func (p proxy) Call(method string, arguments ...interface{}) (returnValues []interface{}, err error) {
if err = p.ensureMethod(method); err != nil {
return
}
request := buildRequestMessage(p.Service.String(), method)
var objects []object
if objects, err = p.prepareArguments(arguments); err != nil {
return
}
request.Arguments = objects
responseChannel := p.Invocations.New(request.RequestID)
defer p.Invocations.Remove(request.RequestID)
p.InvocationQueue.In() <- request
response := <-responseChannel
returnValues, err = p.prepareReturnValues(response)
return
}
var (
i = &invocations{holder: new(sync.Map)}
q = &queue{holder: make(chan *Message, 100)}
)
type invocationsFactory struct {
}
func (invocationsFactory) New() (s interface{}, err error) {
s = i
return
}
type invocationQueueFactory struct {
}
func (invocationQueueFactory) New() (s interface{}, err error) {
s = q
return
}
type Invocations interface {
New(requestID string) (response <-chan *Message)
Get(requestID string) (response chan<- *Message, err error)
Remove(requestID string)
}
type InvocationQueue interface {
Out() (out <-chan *Message)
In() (in chan<- *Message)
Close()
}
type invocations struct {
holder *sync.Map
}
func (i invocations) New(requestID string) (response <-chan *Message) {
r := make(chan *Message)
i.holder.Store(requestID, r)
response = r
return
}
func (i invocations) Get(requestID string) (response chan<- *Message, err error) {
invocation, exist := i.holder.Load(requestID)
if !exist {
err = ErrNoSuchInvocation
return
}
response = invocation.(chan *Message)
return
}
func (i invocations) Remove(requestID string) {
i.holder.Delete(requestID)
}
type queue struct {
holder chan *Message
}
func (q queue) Out() (out <-chan *Message) {
return q.holder
}
func (q queue) In() (in chan<- *Message) {
return q.holder
}
func (q queue) Close() {
close(q.holder)
}