-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathsingle.go
129 lines (112 loc) · 3.47 KB
/
single.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
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package peer
import (
"context"
"fmt"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/api/x/introspection"
"go.uber.org/yarpc/pkg/lifecycle"
)
// Single implements the Chooser interface for a single peer
type Single struct {
once *lifecycle.Once
t peer.Transport
pid peer.Identifier
p peer.Peer
err error
boundOnFinish func(error)
}
// NewSingle creates a static Chooser with a single Peer
func NewSingle(pid peer.Identifier, transport peer.Transport) *Single {
s := &Single{
once: lifecycle.NewOnce(),
pid: pid,
t: transport,
}
s.boundOnFinish = s.onFinish
return s
}
// Transport returns the transport to which this peer is attached.
func (s *Single) Transport() peer.Transport {
return s.t
}
// Choose returns the single peer
func (s *Single) Choose(ctx context.Context, _ *transport.Request) (peer.Peer, func(error), error) {
if err := s.once.WaitUntilRunning(ctx); err != nil {
return nil, nil, err
}
s.p.StartRequest()
return s.p, s.boundOnFinish, s.err
}
func (s *Single) onFinish(_ error) {
s.p.EndRequest()
}
// NotifyStatusChanged receives notifications from the transport when the peer
// connects, disconnects, accepts a request, and so on.
func (s *Single) NotifyStatusChanged(_ peer.Identifier) {
}
// Start is a noop
func (s *Single) Start() error {
return s.once.Start(s.start)
}
func (s *Single) start() error {
p, err := s.t.RetainPeer(s.pid, s)
s.p = p
s.err = err
return err
}
// Stop is a noop
func (s *Single) Stop() error {
return s.once.Stop(s.stop)
}
func (s *Single) stop() error {
return s.t.ReleasePeer(s.pid, s)
}
// IsRunning is a noop
func (s *Single) IsRunning() bool {
return true
}
// Introspect returns a ChooserStatus with a single PeerStatus.
func (s *Single) Introspect() introspection.ChooserStatus {
if !s.once.IsRunning() {
return introspection.ChooserStatus{
Name: "Single",
Peers: []introspection.PeerStatus{
{
Identifier: s.pid.Identifier(),
State: "uninitialized",
},
},
}
}
peerStatus := s.p.Status()
peer := introspection.PeerStatus{
Identifier: s.p.Identifier(),
State: fmt.Sprintf("%s, %d pending request(s)",
peerStatus.ConnectionStatus.String(),
peerStatus.PendingRequestCount),
}
return introspection.ChooserStatus{
Name: "Single",
Peers: []introspection.PeerStatus{peer},
}
}