netchan.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2013-2018 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. package nats
  14. import (
  15. "errors"
  16. "reflect"
  17. )
  18. // This allows the functionality for network channels by binding send and receive Go chans
  19. // to subjects and optionally queue groups.
  20. // Data will be encoded and decoded via the EncodedConn and its associated encoders.
  21. // BindSendChan binds a channel for send operations to NATS.
  22. func (c *EncodedConn) BindSendChan(subject string, channel interface{}) error {
  23. chVal := reflect.ValueOf(channel)
  24. if chVal.Kind() != reflect.Chan {
  25. return ErrChanArg
  26. }
  27. go chPublish(c, chVal, subject)
  28. return nil
  29. }
  30. // Publish all values that arrive on the channel until it is closed or we
  31. // encounter an error.
  32. func chPublish(c *EncodedConn, chVal reflect.Value, subject string) {
  33. for {
  34. val, ok := chVal.Recv()
  35. if !ok {
  36. // Channel has most likely been closed.
  37. return
  38. }
  39. if e := c.Publish(subject, val.Interface()); e != nil {
  40. // Do this under lock.
  41. c.Conn.mu.Lock()
  42. defer c.Conn.mu.Unlock()
  43. if c.Conn.Opts.AsyncErrorCB != nil {
  44. // FIXME(dlc) - Not sure this is the right thing to do.
  45. // FIXME(ivan) - If the connection is not yet closed, try to schedule the callback
  46. if c.Conn.isClosed() {
  47. go c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e)
  48. } else {
  49. c.Conn.ach.push(func() { c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e) })
  50. }
  51. }
  52. return
  53. }
  54. }
  55. }
  56. // BindRecvChan binds a channel for receive operations from NATS.
  57. func (c *EncodedConn) BindRecvChan(subject string, channel interface{}) (*Subscription, error) {
  58. return c.bindRecvChan(subject, _EMPTY_, channel)
  59. }
  60. // BindRecvQueueChan binds a channel for queue-based receive operations from NATS.
  61. func (c *EncodedConn) BindRecvQueueChan(subject, queue string, channel interface{}) (*Subscription, error) {
  62. return c.bindRecvChan(subject, queue, channel)
  63. }
  64. // Internal function to bind receive operations for a channel.
  65. func (c *EncodedConn) bindRecvChan(subject, queue string, channel interface{}) (*Subscription, error) {
  66. chVal := reflect.ValueOf(channel)
  67. if chVal.Kind() != reflect.Chan {
  68. return nil, ErrChanArg
  69. }
  70. argType := chVal.Type().Elem()
  71. cb := func(m *Msg) {
  72. var oPtr reflect.Value
  73. if argType.Kind() != reflect.Ptr {
  74. oPtr = reflect.New(argType)
  75. } else {
  76. oPtr = reflect.New(argType.Elem())
  77. }
  78. if err := c.Enc.Decode(m.Subject, m.Data, oPtr.Interface()); err != nil {
  79. c.Conn.err = errors.New("nats: Got an error trying to unmarshal: " + err.Error())
  80. if c.Conn.Opts.AsyncErrorCB != nil {
  81. c.Conn.ach.push(func() { c.Conn.Opts.AsyncErrorCB(c.Conn, m.Sub, c.Conn.err) })
  82. }
  83. return
  84. }
  85. if argType.Kind() != reflect.Ptr {
  86. oPtr = reflect.Indirect(oPtr)
  87. }
  88. // This is a bit hacky, but in this instance we may be trying to send to a closed channel.
  89. // and the user does not know when it is safe to close the channel.
  90. defer func() {
  91. // If we have panicked, recover and close the subscription.
  92. if r := recover(); r != nil {
  93. m.Sub.Unsubscribe()
  94. }
  95. }()
  96. // Actually do the send to the channel.
  97. chVal.Send(oPtr)
  98. }
  99. return c.Conn.subscribe(subject, queue, cb, nil)
  100. }