Debian Codegraph @

netchan.go

raw
1 // Copyright 2013-2023 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package nats
15
16 import (
17 "errors"
18 "reflect"
19 )
20
21 // This allows the functionality for network channels by binding send and receive Go chans
22 // to subjects and optionally queue groups.
23 // Data will be encoded and decoded via the EncodedConn and its associated encoders.
24
25 // BindSendChan binds a channel for send operations to NATS.
26 //
27 // Deprecated: Encoded connections are no longer supported.
28 func (c *EncodedConn) BindSendChan(subject string, channel any) error {
29 chVal := reflect.ValueOf(channel)
30 if chVal.Kind() != reflect.Chan {
31 return ErrChanArg
32 }
33 go chPublish(c, chVal, subject)
34 return nil
35 }
36
37 // Publish all values that arrive on the channel until it is closed or we
38 // encounter an error.
39 func chPublish(c *EncodedConn, chVal reflect.Value, subject string) {
40 for {
41 val, ok := chVal.Recv()
42 if !ok {
43 // Channel has most likely been closed.
44 return
45 }
46 if e := c.Publish(subject, val.Interface()); e != nil {
47 // Do this under lock.
48 c.Conn.mu.Lock()
49 defer c.Conn.mu.Unlock()
50
51 if c.Conn.Opts.AsyncErrorCB != nil {
52 // FIXME(dlc) - Not sure this is the right thing to do.
53 // FIXME(ivan) - If the connection is not yet closed, try to schedule the callback
54 if c.Conn.isClosed() {
55 go c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e)
56 } else {
57 c.Conn.ach.push(func() { c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e) })
58 }
59 }
60 return
61 }
62 }
63 }
64
65 // BindRecvChan binds a channel for receive operations from NATS.
66 //
67 // Deprecated: Encoded connections are no longer supported.
68 func (c *EncodedConn) BindRecvChan(subject string, channel any) (*Subscription, error) {
69 return c.bindRecvChan(subject, _EMPTY_, channel)
70 }
71
72 // BindRecvQueueChan binds a channel for queue-based receive operations from NATS.
73 //
74 // Deprecated: Encoded connections are no longer supported.
75 func (c *EncodedConn) BindRecvQueueChan(subject, queue string, channel any) (*Subscription, error) {
76 return c.bindRecvChan(subject, queue, channel)
77 }
78
79 // Internal function to bind receive operations for a channel.
80 func (c *EncodedConn) bindRecvChan(subject, queue string, channel any) (*Subscription, error) {
81 chVal := reflect.ValueOf(channel)
82 if chVal.Kind() != reflect.Chan {
83 return nil, ErrChanArg
84 }
85 argType := chVal.Type().Elem()
86
87 cb := func(m *Msg) {
88 var oPtr reflect.Value
89 if argType.Kind() != reflect.Ptr {
90 oPtr = reflect.New(argType)
91 } else {
92 oPtr = reflect.New(argType.Elem())
93 }
94 if err := c.Enc.Decode(m.Subject, m.Data, oPtr.Interface()); err != nil {
95 c.Conn.err = errors.New("nats: Got an error trying to unmarshal: " + err.Error())
96 if c.Conn.Opts.AsyncErrorCB != nil {
97 c.Conn.ach.push(func() { c.Conn.Opts.AsyncErrorCB(c.Conn, m.Sub, c.Conn.err) })
98 }
99 return
100 }
101 if argType.Kind() != reflect.Ptr {
102 oPtr = reflect.Indirect(oPtr)
103 }
104 // This is a bit hacky, but in this instance we may be trying to send to a closed channel.
105 // and the user does not know when it is safe to close the channel.
106 defer func() {
107 // If we have panicked, recover and close the subscription.
108 if r := recover(); r != nil {
109 m.Sub.Unsubscribe()
110 }
111 }()
112 // Actually do the send to the channel.
113 chVal.Send(oPtr)
114 }
115
116 return c.Conn.subscribe(subject, queue, cb, nil, nil, false, nil)
117 }