fixes for safe conversation and avoid panics (#1213)

* fixes for safe convertation

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

* fix client publish panic

If broker connect returns error we dont check it status and use
it later to publish message, mostly this is unexpected because
broker connection failed and we cant use it.
Also proposed solution have benefit - we flag connection status
only when we have succeseful broker connection

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

* api/handler/broker: fix possible broker publish panic

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-02-19 02:05:38 +03:00
parent 5b60299d47
commit b00167520c

15
grpc.go
View File

@ -6,7 +6,7 @@ import (
"crypto/tls"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
"github.com/micro/go-micro/v2/broker"
@ -24,9 +24,9 @@ import (
)
type grpcClient struct {
once sync.Once
opts client.Options
pool *pool
once atomic.Value
}
func init() {
@ -570,9 +570,12 @@ func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...clie
body = b
}
g.once.Do(func() {
g.opts.Broker.Connect()
})
if !g.once.Load().(bool) {
if err = g.opts.Broker.Connect(); err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}
g.once.Store(true)
}
topic := p.Topic()
@ -641,9 +644,9 @@ func newClient(opts ...client.Option) client.Client {
}
rc := &grpcClient{
once: sync.Once{},
opts: options,
}
rc.once.Store(false)
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())