-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
141 lines (126 loc) · 3.02 KB
/
client.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
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package hprose_go_nats
import (
"errors"
"fmt"
"github.com/hprose/hprose-golang/rpc"
"github.com/nats-io/go-nats"
"github.com/vlorc/timer"
"net/url"
"sync"
"sync/atomic"
)
type NatsClient struct {
rpc.BaseClient
conn *nats.Conn
url *url.URL
uri string
opt *NatsOption
queue chan *nats.Msg
pool *Pool
lock sync.Mutex
send atomic.Value
}
func NewClient(opt *NatsOption) rpc.Client {
client := &NatsClient{
queue: make(chan *nats.Msg, opt.queue),
pool: NewPool(),
opt: opt,
}
client.InitBaseClient()
client.SetURIList(opt.uri)
client.send.Store(sendErrNotConnect)
client.BaseClient.SendAndReceive = client.SendAndReceive
go client.worker()
return client
}
func (nc *NatsClient) worker() {
for {
msg, ok := <-nc.queue
if !ok {
break
}
if nil != msg {
nc.pool.Push(NewSequence(msg.Subject), msg.Data, nil)
}
}
}
func (nc *NatsClient) sendRequest(data []byte, ctx *rpc.ClientContext) ([]byte, error) {
req := nc.pool.Get(nc.opt.ttl)
defer nc.pool.Put(req)
if err := nc.conn.PublishRequest(nc.opt.topic, nc.opt.id+req.String(), data); nil != err {
nc.pool.Remove(req.Id())
return nil, err
}
return req.Response()
}
func (nc *NatsClient) ready() *NatsClient {
if nc.url == nc.BaseClient.URL() {
return nc
}
nc.lock.Lock()
defer nc.lock.Unlock()
if nc.url != nc.BaseClient.URL() {
nc.init()
nc.url = nc.BaseClient.URL()
}
return nc
}
func (nc *NatsClient) SendAndReceive(data []byte, ctx *rpc.ClientContext) ([]byte, error) {
return nc.ready().send.Load().(func([]byte, *rpc.ClientContext) ([]byte, error))(data, ctx)
}
func (nc *NatsClient) shutdown() {
if nil != nc.conn {
conn := nc.conn
nc.conn = nil
nc.pool.Wheel().Add(timer.NewTimerTable(func() { conn.Close() }, nc.opt.delay))
}
}
func (nc *NatsClient) Close() {
nc.send.Store(sendErrClosed)
nc.shutdown()
}
func (nc *NatsClient) init() {
if nil == nc.BaseClient.URL() {
nc.send.Store(sendErrIllegalUrl)
return
}
if nc.uri == nc.BaseClient.URI() {
return
}
nc.connect()
}
func (nc *NatsClient) connect() {
var err error
defer func() {
if it := recover(); nil != it {
err = errors.New(fmt.Sprint(it))
}
if nil != err {
nc.send.Store(func([]byte, *rpc.ClientContext) ([]byte, error) {
return nil, err
})
}
}()
nc.shutdown()
nc.opt.uri = nc.opt.uri[:0]
Uri(nc.BaseClient.URI())(nc.opt)
if nc.conn, err = nats.Connect(nc.opt.uri[0], nc.opt.options...); nil != err {
return
}
if _, err = nc.conn.ChanSubscribe(nc.opt.id+".*", nc.queue); nil != err {
return
}
nc.uri = nc.BaseClient.URI()
nc.send.Store(nc.sendRequest)
}
func sendErrNotConnect([]byte, *rpc.ClientContext) ([]byte, error) {
return nil, ErrNotConnect
}
func sendErrClosed([]byte, *rpc.ClientContext) ([]byte, error) {
return nil, ErrClosed
}
func sendErrIllegalUrl([]byte, *rpc.ClientContext) ([]byte, error) {
return nil, ErrIllegalUrl
}