Debian Codegraph @

go/pkg/electron/session.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 "github.com/apache/qpid-proton/go/pkg/proton"
24 )
25
26 // Session is an AMQP session, it contains Senders and Receivers.
27 type Session interface {
28 Endpoint
29
30 // Sender opens a new sender.
31 Sender(...LinkOption) (Sender, error)
32
33 // Receiver opens a new Receiver.
34 Receiver(...LinkOption) (Receiver, error)
35 }
36
37 type session struct {
38 endpoint
39 pSession proton.Session
40 connection *connection
41 incomingCapacity, outgoingWindow uint
42 }
43
44 // SessionOption can be passed when creating a Session
45 type SessionOption func(*session)
46
47 // IncomingCapacity returns a Session Option that sets the size (in bytes) of
48 // the session's incoming data buffer.
49 func IncomingCapacity(bytes uint) SessionOption {
50 return func(s *session) { s.incomingCapacity = bytes }
51 }
52
53 // OutgoingWindow returns a Session Option that sets the outgoing window size (in frames).
54 func OutgoingWindow(frames uint) SessionOption {
55 return func(s *session) { s.outgoingWindow = frames }
56 }
57
58 // in proton goroutine
59 func newSession(c *connection, es proton.Session, setting ...SessionOption) *session {
60 s := &session{
61 connection: c,
62 pSession: es,
63 }
64 s.endpoint.init(es.String())
65 for _, set := range setting {
66 set(s)
67 }
68 c.handler.sessions[s.pSession] = s
69 s.pSession.SetIncomingCapacity(s.incomingCapacity)
70 s.pSession.SetOutgoingWindow(s.outgoingWindow)
71 s.pSession.Open()
72 return s
73 }
74
75 func (s *session) Connection() Connection { return s.connection }
76 func (s *session) pEndpoint() proton.Endpoint { return s.pSession }
77 func (s *session) engine() *proton.Engine { return s.connection.engine }
78
79 func (s *session) Close(err error) {
80 _ = s.engine().Inject(func() {
81 if s.Error() == nil {
82 localClose(s.pSession, err)
83 }
84 })
85 }
86
87 func (s *session) Sender(setting ...LinkOption) (snd Sender, err error) {
88 err = s.engine().InjectWait(func() error {
89 if s.Error() != nil {
90 return s.Error()
91 }
92 l, err2 := makeLocalLink(s, true, setting...)
93 if err2 == nil {
94 snd = newSender(l)
95 }
96 return err2
97 })
98 return
99 }
100
101 func (s *session) Receiver(setting ...LinkOption) (rcv Receiver, err error) {
102 err = s.engine().InjectWait(func() error {
103 if s.Error() != nil {
104 return s.Error()
105 }
106 l, err2 := makeLocalLink(s, false, setting...)
107 if err2 == nil {
108 rcv = newReceiver(l)
109 }
110 return err2
111 })
112 return
113 }
114
115 // IncomingSender is sent on the Connection.Incoming() channel when there is an
116 // incoming request to open a session.
117 type IncomingSession struct {
118 incoming
119 h *handler
120 pSession proton.Session
121 incomingCapacity, outgoingWindow uint
122 }
123
124 func newIncomingSession(h *handler, ps proton.Session) *IncomingSession {
125 return &IncomingSession{incoming: makeIncoming(ps), h: h, pSession: ps}
126 }
127
128 // SetIncomingCapacity sets the session buffer capacity of an incoming session in bytes.
129 func (in *IncomingSession) SetIncomingCapacity(bytes uint) { in.incomingCapacity = bytes }
130
131 // SetOutgoingWindow sets the session outgoing window of an incoming session in frames.
132 func (in *IncomingSession) SetOutgoingWindow(frames uint) { in.outgoingWindow = frames }
133
134 // Accept an incoming session endpoint.
135 func (in *IncomingSession) Accept() Endpoint {
136 return in.accept(func() Endpoint {
137 return newSession(in.h.connection, in.pSession, IncomingCapacity(in.incomingCapacity), OutgoingWindow(in.outgoingWindow))
138 })
139 }