Debian Codegraph @

go/pkg/electron/endpoint.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package electron
21
22 import (
23 "fmt"
24 "io"
25 "github.com/apache/qpid-proton/go/pkg/proton"
26 )
27
28 // Closed is an alias for io.EOF. It is returned as an error when an endpoint
29 // was closed cleanly.
30 var Closed = io.EOF
31
32 // EOF is an alias for io.EOF. It is returned as an error when an endpoint
33 // was closed cleanly.
34 var EOF = io.EOF
35
36 // Endpoint is the local end of a communications channel to the remote peer
37 // process. The following interface implement Endpoint: Connection, Session,
38 // Sender and Receiver.
39 //
40 // You can create an endpoint with functions on Container, Connection and
41 // Session. You can accept incoming endpoints from the remote peer using
42 // Connection.Incoming()
43 //
44 type Endpoint interface {
45 // Close an endpoint and signal an error to the remote end if error != nil.
46 Close(error)
47
48 // String is a human readable identifier, useful for debugging and logging.
49 String() string
50
51 // Error returns nil if the endpoint is open, otherwise returns an error.
52 // Error() == Closed means the endpoint was closed without error.
53 Error() error
54
55 // Connection is the connection associated with this endpoint.
56 Connection() Connection
57
58 // Done returns a channel that will close when the endpoint closes.
59 // After Done() has closed, Error() will return the reason for closing.
60 Done() <-chan struct{}
61
62 // Sync() waits for the remote peer to confirm the endpoint is active or
63 // reject it with an error. You can call it immediately on new endpoints
64 // for more predictable error handling.
65 //
66 // AMQP is an asynchronous protocol. It is legal to create an endpoint and
67 // start using it without waiting for confirmation. This avoids a needless
68 // delay in the non-error case and throughput by "assuming the best".
69 //
70 // However if there *is* an error, these "optimistic" actions will fail. The
71 // endpoint and its children will be closed with an error. The error will only
72 // be detected when you try to use one of these endpoints or call Sync()
73 Sync() error
74 }
75
76 type endpointInternal interface {
77 // Called in handler goroutine when endpoint is remotely closed.
78 closed(err error) error
79 wakeSync()
80 }
81
82 // Base implementation for Endpoint
83 type endpoint struct {
84 err proton.ErrorHolder
85 str string // String() return value.
86 done chan struct{}
87 active chan struct{}
88 }
89
90 func (e *endpoint) init(s string) {
91 e.str = s
92 e.done = make(chan struct{})
93 e.active = make(chan struct{})
94 }
95
96 // Called in proton goroutine on remote open.
97 func (e *endpoint) wakeSync() {
98 select { // Close active channel if not already closed.
99 case <-e.active:
100 default:
101 close(e.active)
102 }
103 }
104
105 // Called in proton goroutine (from handler) on a Closed or Disconnected event.
106 //
107 // Set err if there is not already an error on the endpoint.
108 // Return Error()
109 func (e *endpoint) closed(err error) error {
110 select {
111 case <-e.done:
112 // Already closed
113 default:
114 e.err.Set(err)
115 e.err.Set(Closed)
116 e.wakeSync() // Make sure we wake up Sync()
117 close(e.done)
118 }
119 return e.Error()
120 }
121
122 func (e *endpoint) String() string { return e.str }
123
124 func (e *endpoint) Error() error { return e.err.Get() }
125
126 func (e *endpoint) Done() <-chan struct{} { return e.done }
127
128 func (e *endpoint) Sync() error {
129 <-e.active
130 return e.Error()
131 }
132
133 // Call in proton goroutine to initiate closing an endpoint locally
134 // handler will complete the close when remote end closes.
135 func localClose(ep proton.Endpoint, err error) {
136 if ep.State().LocalActive() {
137 proton.CloseError(ep, err)
138 }
139 }
140
141 // Incoming is the interface for incoming endpoints, see Connection.Incoming()
142 //
143 // Call Incoming.Accept() to open the endpoint or Incoming.Reject() to close it
144 // with optional error
145 //
146 // Implementing types are *IncomingConnection, *IncomingSession, *IncomingSender
147 // and *IncomingReceiver. Each type provides methods to examine the incoming
148 // endpoint request and set configuration options for the local endpoint
149 // before calling Accept() or Reject()
150 type Incoming interface {
151 // Accept and open the endpoint.
152 Accept() Endpoint
153
154 // Reject the endpoint with an error
155 Reject(error)
156
157 // wait for and call the accept function, call in proton goroutine.
158 wait() error
159 pEndpoint() proton.Endpoint
160 }
161
162 type incoming struct {
163 pep proton.Endpoint
164 acceptCh chan func() error
165 }
166
167 func makeIncoming(e proton.Endpoint) incoming {
168 return incoming{pep: e, acceptCh: make(chan func() error)}
169 }
170
171 func (in *incoming) String() string { return fmt.Sprintf("%s: %s", in.pep.Type(), in.pep) }
172 func (in *incoming) Reject(err error) { in.acceptCh <- func() error { return err } }
173
174 // Call in proton goroutine, wait for and call the accept function.
175 func (in *incoming) wait() error { return (<-in.acceptCh)() }
176
177 func (in *incoming) pEndpoint() proton.Endpoint { return in.pep }
178
179 // Called in app goroutine to send an accept function to proton and return the resulting endpoint.
180 func (in *incoming) accept(f func() Endpoint) Endpoint {
181 done := make(chan Endpoint)
182 in.acceptCh <- func() error {
183 ep := f()
184 done <- ep
185 return nil
186 }
187 return <-done
188 }