1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

move all the buffer references to util/buf

This commit is contained in:
Asim Aslam 2019-07-28 19:33:24 +01:00
parent f2669e7b1e
commit 1db98ee0f0
9 changed files with 25 additions and 64 deletions

View File

@ -1,14 +0,0 @@
package grpc
import (
"bytes"
)
type buffer struct {
*bytes.Buffer
}
func (b *buffer) Close() error {
b.Buffer.Reset()
return nil
}

View File

@ -2,7 +2,6 @@
package grpc package grpc
import ( import (
"bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
@ -19,6 +18,7 @@ import (
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
"github.com/micro/go-micro/util/buf"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding"
@ -491,7 +491,8 @@ func (g *grpcClient) Publish(ctx context.Context, p client.Message, opts ...clie
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
b := &buffer{bytes.NewBuffer(nil)} b := buf.New(nil)
if err := cf(b).Write(&codec.Message{Type: codec.Event}, p.Payload()); err != nil { if err := cf(b).Write(&codec.Message{Type: codec.Event}, p.Payload()); err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }

View File

@ -1,7 +1,6 @@
package client package client
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"os" "os"
@ -18,6 +17,7 @@ import (
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
"github.com/micro/go-micro/util/buf"
) )
type rpcClient struct { type rpcClient struct {
@ -538,7 +538,10 @@ func (r *rpcClient) Publish(ctx context.Context, msg Message, opts ...PublishOpt
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
b := &buffer{bytes.NewBuffer(nil)}
// new buffer
b := buf.New(nil)
if err := cf(b).Write(&codec.Message{ if err := cf(b).Write(&codec.Message{
Target: topic, Target: topic,
Type: codec.Event, Type: codec.Event,

View File

@ -1,14 +0,0 @@
package server
import (
"bytes"
)
type buffer struct {
*bytes.Buffer
}
func (b *buffer) Close() error {
b.Buffer.Reset()
return nil
}

View File

@ -1,14 +0,0 @@
package grpc
import (
"bytes"
)
type buffer struct {
*bytes.Buffer
}
func (b *buffer) Close() error {
b.Buffer.Reset()
return nil
}

View File

@ -12,6 +12,7 @@ import (
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"github.com/micro/go-micro/util/buf"
) )
const ( const (
@ -204,7 +205,7 @@ func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broke
req = req.Elem() req = req.Elem()
} }
b := &buffer{bytes.NewBuffer(msg.Body)} b := buf.New(bytes.NewBuffer(msg.Body))
co := cf(b) co := cf(b)
defer co.Close() defer co.Close()

View File

@ -11,6 +11,7 @@ import (
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/util/buf"
) )
const ( const (
@ -210,7 +211,7 @@ func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handle
req = req.Elem() req = req.Elem()
} }
b := &buffer{bytes.NewBuffer(msg.Body)} b := buf.New(bytes.NewBuffer(msg.Body))
co := cf(b) co := cf(b)
defer co.Close() defer co.Close()

View File

@ -14,16 +14,13 @@ import (
"time" "time"
maddr "github.com/micro/go-micro/util/addr" maddr "github.com/micro/go-micro/util/addr"
"github.com/micro/go-micro/util/buf"
mnet "github.com/micro/go-micro/util/net" mnet "github.com/micro/go-micro/util/net"
mls "github.com/micro/go-micro/util/tls" mls "github.com/micro/go-micro/util/tls"
"golang.org/x/net/http2" "golang.org/x/net/http2"
"golang.org/x/net/http2/h2c" "golang.org/x/net/http2/h2c"
) )
type buffer struct {
io.ReadWriter
}
type httpTransport struct { type httpTransport struct {
opts Options opts Options
} }
@ -65,10 +62,6 @@ type httpTransportListener struct {
listener net.Listener listener net.Listener
} }
func (b *buffer) Close() error {
return nil
}
func (h *httpTransportClient) Local() string { func (h *httpTransportClient) Local() string {
return h.local return h.local
} }
@ -84,11 +77,8 @@ func (h *httpTransportClient) Send(m *Message) error {
header.Set(k, v) header.Set(k, v)
} }
reqB := bytes.NewBuffer(m.Body) b := buf.New(bytes.NewBuffer(m.Body))
defer reqB.Reset() defer b.Close()
buf := &buffer{
reqB,
}
req := &http.Request{ req := &http.Request{
Method: "POST", Method: "POST",
@ -97,8 +87,8 @@ func (h *httpTransportClient) Send(m *Message) error {
Host: h.addr, Host: h.addr,
}, },
Header: header, Header: header,
Body: buf, Body: b,
ContentLength: int64(reqB.Len()), ContentLength: int64(b.Len()),
Host: h.addr, Host: h.addr,
} }

View File

@ -1,4 +1,4 @@
package client package buf
import ( import (
"bytes" "bytes"
@ -12,3 +12,10 @@ func (b *buffer) Close() error {
b.Buffer.Reset() b.Buffer.Reset()
return nil return nil
} }
func New(b *bytes.Buffer) *buffer {
if b == nil {
b = bytes.NewBuffer(nil)
}
return &buffer{b}
}