mirror of
https://github.com/go-micro/go-micro.git
synced 2025-02-04 18:21:53 +02:00
4929a7c16e
Remove missing gRPC example from README.md (#2112) Delete docker.yml Delete Dockerfile update plugins version & remove replace (#2118) * update memory registry plugins version & remove replace * update plugins version & remove replace Co-authored-by: 申法宽 <shenfakuan@163.com> update client/grpc plugins version & remove replace (#2119) * update memory registry plugins version & remove replace * update plugins version & remove replace * update plugins/client/grpc/v3 version Co-authored-by: 申法宽 <shenfakuan@163.com> update etcd version (#2120) update mod version update update pulgin registry mod version (#2121) * update etcd version * update mod version * update fix store delete support for tls on http plugin (#2126) improve code quality (#2128) * Fix inefficient string comparison * Fix unnecessary calls to Printf * Canonicalize header key * Replace `t.Sub(time.Now())` with `time.Until` * Remove unnecessary blank (_) identifier * Remove unnecessary use of slice * Remove unnecessary comparison with bool Update README.md Update README.md remove network package update quic go mod remove indirects update etcd mod version Update registry plugins mod version (#2130) * update etcd version * update mod version * update * update etcd mod version Update README.md Update README.md Update README.md fixing etcd stack in getToken (#2145) when provide username and password, etcd will try to get auth token from server if server is unavailble, etcd client will stack in when dial timeout is set, it will return err instead of stack in Update README.md add http demo; http client can call http server; http client can call rpc server (#2149) Add etcd to default registries when plugin is loaded (#2150) Co-authored-by: Andrew Jones <andrew@gotoblink.com> Update README.md make rpcClient compatible with 32bit arm systems (#2156) On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically. Only the first word in an allocated struct can be relied upon to be 64-bit aligned. optimize the process of switching grpc error to micro error (#2158) Fix util/log/log.Infof format didn't work (#2160) Co-authored-by: Cui Gang <cuigang@yunpbx.com> fixing string field contains invalid UTF-8 issue (#2164) fix k8s api memory leak (#2166) fix http No release Broker (#2167) * Update http.go Exit before deregister is executed * Create http.go Exit before deregister is executed fix: "Solve the problem that the resources have not been fully released due to early exit" (#2168) * Update http.go Exit before deregister is executed * Create http.go Exit before deregister is executed * Solve the problem that the resources have not been fully released due to early exit * Optimize some code * Optimize some code fix service default logger (#2171) * Update http.go Exit before deregister is executed * Create http.go Exit before deregister is executed * Solve the problem that the resources have not been fully released due to early exit * Optimize some code * Optimize some code * Optimize some code * fix service default logger Update README.md get k8s pod (#2173) Update README.md fix:field (#2176) * get k8s pod * fix: filed * field Update README.md add rmq message properties (#2177) Co-authored-by: dtitov <dtitov@might24.ru> Update README.md grpc server add RegisterCheck (#2178) fix 404 bug (#2179) fix undefined: err (#2181) Add registry and config/source plugins based on nacos/v2 (#2182) * Add registry plugins implement by nacos/v2 * Add config/source plugins implement by nacos/v2 support hystrix fallback (#2183) Windows event log plugin (#2180) * add rmq message properties * eventlog start * start eventlog * windows event logger * readme * readme Co-authored-by: dtitov <dtitov@might24.ru> support etcd auth with env args (#2184) * support etcd auth with env args set default registry address with env arg instead of 127.0.0.1 * fixing MICRO_REGISTRY_ADDRESS may empty issue update mod version
246 lines
5.6 KiB
Go
246 lines
5.6 KiB
Go
package stomp
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/asim/go-micro/v3/broker"
|
|
"github.com/asim/go-micro/v3/cmd"
|
|
"github.com/go-stomp/stomp/v3"
|
|
"github.com/go-stomp/stomp/v3/frame"
|
|
)
|
|
|
|
type rbroker struct {
|
|
opts broker.Options
|
|
stompConn *stomp.Conn
|
|
}
|
|
|
|
// init registers the STOMP broker
|
|
func init() {
|
|
cmd.DefaultBrokers["stomp"] = NewBroker
|
|
}
|
|
|
|
// stompHeaderToMap converts STOMP header to broker friendly header
|
|
func stompHeaderToMap(h *frame.Header) map[string]string {
|
|
m := map[string]string{}
|
|
for i := 0; i < h.Len(); i++ {
|
|
k, v := h.GetAt(i)
|
|
m[k] = v
|
|
}
|
|
return m
|
|
}
|
|
|
|
// defaults sets 'sane' STOMP default
|
|
func (r *rbroker) defaults() {
|
|
ConnectTimeout(30 * time.Second)(&r.opts)
|
|
VirtualHost("/")(&r.opts)
|
|
}
|
|
|
|
func (r *rbroker) Options() broker.Options {
|
|
if r.opts.Context == nil {
|
|
r.opts.Context = context.Background()
|
|
}
|
|
return r.opts
|
|
}
|
|
|
|
func (r *rbroker) Address() string {
|
|
if len(r.opts.Addrs) > 0 {
|
|
return r.opts.Addrs[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (r *rbroker) Connect() error {
|
|
connectTimeOut := r.Options().Context.Value(connectTimeoutKey{}).(time.Duration)
|
|
|
|
// Decode address
|
|
url, err := url.Parse(r.Address())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Make sure we are stomp
|
|
if url.Scheme != "stomp" {
|
|
return fmt.Errorf("Expected stomp:// protocol but was %s", url.Scheme)
|
|
}
|
|
|
|
// Retrieve user/pass if present
|
|
stompOpts := []func(*stomp.Conn) error{}
|
|
if url.User != nil && url.User.Username() != "" {
|
|
password, _ := url.User.Password()
|
|
stompOpts = append(stompOpts, stomp.ConnOpt.Login(url.User.Username(), password))
|
|
}
|
|
|
|
// Dial
|
|
netConn, err := net.DialTimeout("tcp", url.Host, connectTimeOut)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to dial %s: %v", url.Host, err)
|
|
}
|
|
|
|
// Set connect options
|
|
if auth, ok := r.Options().Context.Value(authKey{}).(*authRecord); ok && auth != nil {
|
|
stompOpts = append(stompOpts, stomp.ConnOpt.Login(auth.username, auth.password))
|
|
}
|
|
if headers, ok := r.Options().Context.Value(connectHeaderKey{}).(map[string]string); ok && headers != nil {
|
|
for k, v := range headers {
|
|
stompOpts = append(stompOpts, stomp.ConnOpt.Header(k, v))
|
|
}
|
|
}
|
|
if host, ok := r.Options().Context.Value(vHostKey{}).(string); ok && host != "" {
|
|
log.Printf("Adding host: %s", host)
|
|
stompOpts = append(stompOpts, stomp.ConnOpt.Host(host))
|
|
}
|
|
|
|
// STOMP Connect
|
|
r.stompConn, err = stomp.Connect(netConn, stompOpts...)
|
|
if err != nil {
|
|
netConn.Close()
|
|
return fmt.Errorf("Failed to connect to %s: %v", url.Host, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *rbroker) Disconnect() error {
|
|
return r.stompConn.Disconnect()
|
|
}
|
|
|
|
func (r *rbroker) Init(opts ...broker.Option) error {
|
|
r.defaults()
|
|
|
|
for _, o := range opts {
|
|
o(&r.opts)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *rbroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
|
if r.stompConn == nil {
|
|
return errors.New("not connected")
|
|
}
|
|
|
|
// Set options
|
|
stompOpt := make([]func(*frame.Frame) error, 0, len(msg.Header))
|
|
for k, v := range msg.Header {
|
|
stompOpt = append(stompOpt, stomp.SendOpt.Header(k, v))
|
|
}
|
|
|
|
bOpt := broker.PublishOptions{}
|
|
for _, o := range opts {
|
|
o(&bOpt)
|
|
}
|
|
if withReceipt, ok := r.Options().Context.Value(receiptKey{}).(bool); ok && withReceipt {
|
|
stompOpt = append(stompOpt, stomp.SendOpt.Receipt)
|
|
}
|
|
if withoutContentLength, ok := r.Options().Context.Value(suppressContentLengthKey{}).(bool); ok && withoutContentLength {
|
|
stompOpt = append(stompOpt, stomp.SendOpt.NoContentLength)
|
|
}
|
|
|
|
// Send
|
|
if err := r.stompConn.Send(
|
|
topic,
|
|
"",
|
|
msg.Body,
|
|
stompOpt...); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
var ackSuccess bool
|
|
|
|
if r.stompConn == nil {
|
|
return nil, errors.New("not connected")
|
|
}
|
|
|
|
// Set options
|
|
stompOpt := make([]func(*frame.Frame) error, 0, len(opts))
|
|
bOpt := broker.SubscribeOptions{
|
|
AutoAck: true,
|
|
}
|
|
for _, o := range opts {
|
|
o(&bOpt)
|
|
}
|
|
// Make sure context is setup
|
|
if bOpt.Context == nil {
|
|
bOpt.Context = context.Background()
|
|
}
|
|
|
|
ctx := bOpt.Context
|
|
if subscribeContext, ok := ctx.Value(subscribeContextKey{}).(context.Context); ok && subscribeContext != nil {
|
|
ctx = subscribeContext
|
|
}
|
|
|
|
if durableQueue, ok := ctx.Value(durableQueueKey{}).(bool); ok && durableQueue {
|
|
stompOpt = append(stompOpt, stomp.SubscribeOpt.Header("persistent", "true"))
|
|
}
|
|
|
|
if headers, ok := ctx.Value(subscribeHeaderKey{}).(map[string]string); ok && len(headers) > 0 {
|
|
for k, v := range headers {
|
|
stompOpt = append(stompOpt, stomp.SubscribeOpt.Header(k, v))
|
|
}
|
|
}
|
|
|
|
if bval, ok := ctx.Value(ackSuccessKey{}).(bool); ok && bval {
|
|
bOpt.AutoAck = false
|
|
ackSuccess = true
|
|
}
|
|
|
|
var ackMode stomp.AckMode
|
|
if bOpt.AutoAck {
|
|
ackMode = stomp.AckAuto
|
|
} else {
|
|
ackMode = stomp.AckClientIndividual
|
|
}
|
|
|
|
// Subscribe now
|
|
sub, err := r.stompConn.Subscribe(topic, ackMode, stompOpt...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Process messages
|
|
go func() {
|
|
for msg := range sub.C {
|
|
go func(msg *stomp.Message) {
|
|
// Transform message
|
|
m := &broker.Message{
|
|
Header: stompHeaderToMap(msg.Header),
|
|
Body: msg.Body,
|
|
}
|
|
p := &publication{msg: msg, m: m, topic: topic, broker: r}
|
|
// Handle the publication
|
|
p.err = handler(p)
|
|
if p.err == nil && !bOpt.AutoAck && ackSuccess {
|
|
msg.Conn.Ack(msg)
|
|
}
|
|
}(msg)
|
|
}
|
|
}()
|
|
|
|
// Return subs
|
|
return &subscriber{sub: sub, topic: topic, opts: bOpt}, nil
|
|
}
|
|
|
|
func (r *rbroker) String() string {
|
|
return "stomp"
|
|
}
|
|
|
|
// NewBroker returns a STOMP broker
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
r := &rbroker{
|
|
opts: broker.Options{
|
|
Context: context.Background(),
|
|
},
|
|
}
|
|
r.Init(opts...)
|
|
return r
|
|
}
|