repo stringlengths 5 67 | path stringlengths 4 218 | func_name stringlengths 0 151 | original_string stringlengths 52 373k | language stringclasses 6 values | code stringlengths 52 373k | code_tokens listlengths 10 512 | docstring stringlengths 3 47.2k | docstring_tokens listlengths 3 234 | sha stringlengths 40 40 | url stringlengths 85 339 | partition stringclasses 3 values |
|---|---|---|---|---|---|---|---|---|---|---|---|
micro/go-plugins | codec/msgpackrpc/rpc.go | EncodeMsg | func (n *Notification) EncodeMsg(w *msgp.Writer) error {
var bm msgp.Encodable
if n.Body != nil {
var ok bool
bm, ok = n.Body.(msgp.Encodable)
if !ok {
return ErrNotEncodable
}
}
var err error
if err = w.WriteArrayHeader(NotificationPackSize); err != nil {
return err
}
if err = w.WriteInt(NotificationType); err != nil {
return err
}
if err = w.WriteString(n.Method); err != nil {
return err
}
// No body to encode. Write a zero-length params array.
if bm == nil {
return w.WriteArrayHeader(0)
}
// 1-item array containing the body.
if err = w.WriteArrayHeader(1); err != nil {
return err
}
return msgp.Encode(w, bm)
} | go | func (n *Notification) EncodeMsg(w *msgp.Writer) error {
var bm msgp.Encodable
if n.Body != nil {
var ok bool
bm, ok = n.Body.(msgp.Encodable)
if !ok {
return ErrNotEncodable
}
}
var err error
if err = w.WriteArrayHeader(NotificationPackSize); err != nil {
return err
}
if err = w.WriteInt(NotificationType); err != nil {
return err
}
if err = w.WriteString(n.Method); err != nil {
return err
}
// No body to encode. Write a zero-length params array.
if bm == nil {
return w.WriteArrayHeader(0)
}
// 1-item array containing the body.
if err = w.WriteArrayHeader(1); err != nil {
return err
}
return msgp.Encode(w, bm)
} | [
"func",
"(",
"n",
"*",
"Notification",
")",
"EncodeMsg",
"(",
"w",
"*",
"msgp",
".",
"Writer",
")",
"error",
"{",
"var",
"bm",
"msgp",
".",
"Encodable",
"\n",
"if",
"n",
".",
"Body",
"!=",
"nil",
"{",
"var",
"ok",
"bool",
"\n",
"bm",
",",
"ok",
"=",
"n",
".",
"Body",
".",
"(",
"msgp",
".",
"Encodable",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"ErrNotEncodable",
"\n",
"}",
"\n",
"}",
"\n",
"var",
"err",
"error",
"\n",
"if",
"err",
"=",
"w",
".",
"WriteArrayHeader",
"(",
"NotificationPackSize",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"err",
"=",
"w",
".",
"WriteInt",
"(",
"NotificationType",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"err",
"=",
"w",
".",
"WriteString",
"(",
"n",
".",
"Method",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"bm",
"==",
"nil",
"{",
"return",
"w",
".",
"WriteArrayHeader",
"(",
"0",
")",
"\n",
"}",
"\n",
"if",
"err",
"=",
"w",
".",
"WriteArrayHeader",
"(",
"1",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"msgp",
".",
"Encode",
"(",
"w",
",",
"bm",
")",
"\n",
"}"
] | // EncodeMsg encodes the notification to writer. The body is expected to
// be an encodable type. | [
"EncodeMsg",
"encodes",
"the",
"notification",
"to",
"writer",
".",
"The",
"body",
"is",
"expected",
"to",
"be",
"an",
"encodable",
"type",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/msgpackrpc/rpc.go#L292-L328 | train |
micro/go-plugins | registry/kubernetes/client/mock/utils.go | Stop | func (w *mockWatcher) Stop() {
select {
case <-w.stop:
return
default:
close(w.stop)
close(w.results)
}
} | go | func (w *mockWatcher) Stop() {
select {
case <-w.stop:
return
default:
close(w.stop)
close(w.results)
}
} | [
"func",
"(",
"w",
"*",
"mockWatcher",
")",
"Stop",
"(",
")",
"{",
"select",
"{",
"case",
"<-",
"w",
".",
"stop",
":",
"return",
"\n",
"default",
":",
"close",
"(",
"w",
".",
"stop",
")",
"\n",
"close",
"(",
"w",
".",
"results",
")",
"\n",
"}",
"\n",
"}"
] | // Stop closes any channels | [
"Stop",
"closes",
"any",
"channels"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/mock/utils.go#L19-L27 | train |
micro/go-plugins | broker/redis/redis.go | recv | func (s *subscriber) recv() {
// Close the connection once the subscriber stops receiving.
defer s.conn.Close()
for {
switch x := s.conn.Receive().(type) {
case redis.Message:
var m broker.Message
// Handle error? Only a log would be necessary since this type
// of issue cannot be fixed.
if err := s.codec.Unmarshal(x.Data, &m); err != nil {
break
}
p := publication{
topic: x.Channel,
message: &m,
}
// Handle error? Retry?
if err := s.handle(&p); err != nil {
break
}
// Added for posterity, however Ack is a no-op.
if s.opts.AutoAck {
if err := p.Ack(); err != nil {
break
}
}
case redis.Subscription:
if x.Count == 0 {
return
}
case error:
return
}
}
} | go | func (s *subscriber) recv() {
// Close the connection once the subscriber stops receiving.
defer s.conn.Close()
for {
switch x := s.conn.Receive().(type) {
case redis.Message:
var m broker.Message
// Handle error? Only a log would be necessary since this type
// of issue cannot be fixed.
if err := s.codec.Unmarshal(x.Data, &m); err != nil {
break
}
p := publication{
topic: x.Channel,
message: &m,
}
// Handle error? Retry?
if err := s.handle(&p); err != nil {
break
}
// Added for posterity, however Ack is a no-op.
if s.opts.AutoAck {
if err := p.Ack(); err != nil {
break
}
}
case redis.Subscription:
if x.Count == 0 {
return
}
case error:
return
}
}
} | [
"func",
"(",
"s",
"*",
"subscriber",
")",
"recv",
"(",
")",
"{",
"defer",
"s",
".",
"conn",
".",
"Close",
"(",
")",
"\n",
"for",
"{",
"switch",
"x",
":=",
"s",
".",
"conn",
".",
"Receive",
"(",
")",
".",
"(",
"type",
")",
"{",
"case",
"redis",
".",
"Message",
":",
"var",
"m",
"broker",
".",
"Message",
"\n",
"if",
"err",
":=",
"s",
".",
"codec",
".",
"Unmarshal",
"(",
"x",
".",
"Data",
",",
"&",
"m",
")",
";",
"err",
"!=",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"p",
":=",
"publication",
"{",
"topic",
":",
"x",
".",
"Channel",
",",
"message",
":",
"&",
"m",
",",
"}",
"\n",
"if",
"err",
":=",
"s",
".",
"handle",
"(",
"&",
"p",
")",
";",
"err",
"!=",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"if",
"s",
".",
"opts",
".",
"AutoAck",
"{",
"if",
"err",
":=",
"p",
".",
"Ack",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"case",
"redis",
".",
"Subscription",
":",
"if",
"x",
".",
"Count",
"==",
"0",
"{",
"return",
"\n",
"}",
"\n",
"case",
"error",
":",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // recv loops to receive new messages from Redis and handle them
// as publications. | [
"recv",
"loops",
"to",
"receive",
"new",
"messages",
"from",
"Redis",
"and",
"handle",
"them",
"as",
"publications",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/redis/redis.go#L54-L95 | train |
micro/go-plugins | broker/redis/redis.go | Init | func (b *redisBroker) Init(opts ...broker.Option) error {
if b.pool != nil {
return errors.New("redis: cannot init while connected")
}
for _, o := range opts {
o(&b.opts)
}
return nil
} | go | func (b *redisBroker) Init(opts ...broker.Option) error {
if b.pool != nil {
return errors.New("redis: cannot init while connected")
}
for _, o := range opts {
o(&b.opts)
}
return nil
} | [
"func",
"(",
"b",
"*",
"redisBroker",
")",
"Init",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"error",
"{",
"if",
"b",
".",
"pool",
"!=",
"nil",
"{",
"return",
"errors",
".",
"New",
"(",
"\"redis: cannot init while connected\"",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"b",
".",
"opts",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Init sets or overrides broker options. | [
"Init",
"sets",
"or",
"overrides",
"broker",
"options",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/redis/redis.go#L137-L147 | train |
micro/go-plugins | broker/redis/redis.go | Disconnect | func (b *redisBroker) Disconnect() error {
err := b.pool.Close()
b.pool = nil
b.addr = ""
return err
} | go | func (b *redisBroker) Disconnect() error {
err := b.pool.Close()
b.pool = nil
b.addr = ""
return err
} | [
"func",
"(",
"b",
"*",
"redisBroker",
")",
"Disconnect",
"(",
")",
"error",
"{",
"err",
":=",
"b",
".",
"pool",
".",
"Close",
"(",
")",
"\n",
"b",
".",
"pool",
"=",
"nil",
"\n",
"b",
".",
"addr",
"=",
"\"\"",
"\n",
"return",
"err",
"\n",
"}"
] | // Disconnect closes the connection pool. | [
"Disconnect",
"closes",
"the",
"connection",
"pool",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/redis/redis.go#L192-L197 | train |
micro/go-plugins | broker/redis/redis.go | Publish | func (b *redisBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
v, err := b.opts.Codec.Marshal(msg)
if err != nil {
return err
}
conn := b.pool.Get()
_, err = redis.Int(conn.Do("PUBLISH", topic, v))
conn.Close()
return err
} | go | func (b *redisBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
v, err := b.opts.Codec.Marshal(msg)
if err != nil {
return err
}
conn := b.pool.Get()
_, err = redis.Int(conn.Do("PUBLISH", topic, v))
conn.Close()
return err
} | [
"func",
"(",
"b",
"*",
"redisBroker",
")",
"Publish",
"(",
"topic",
"string",
",",
"msg",
"*",
"broker",
".",
"Message",
",",
"opts",
"...",
"broker",
".",
"PublishOption",
")",
"error",
"{",
"v",
",",
"err",
":=",
"b",
".",
"opts",
".",
"Codec",
".",
"Marshal",
"(",
"msg",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"conn",
":=",
"b",
".",
"pool",
".",
"Get",
"(",
")",
"\n",
"_",
",",
"err",
"=",
"redis",
".",
"Int",
"(",
"conn",
".",
"Do",
"(",
"\"PUBLISH\"",
",",
"topic",
",",
"v",
")",
")",
"\n",
"conn",
".",
"Close",
"(",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Publish publishes a message. | [
"Publish",
"publishes",
"a",
"message",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/redis/redis.go#L200-L211 | train |
micro/go-plugins | broker/redis/redis.go | Subscribe | func (b *redisBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
var options broker.SubscribeOptions
for _, o := range opts {
o(&options)
}
s := subscriber{
codec: b.opts.Codec,
conn: &redis.PubSubConn{Conn: b.pool.Get()},
topic: topic,
handle: handler,
opts: options,
}
// Run the receiver routine.
go s.recv()
if err := s.conn.Subscribe(s.topic); err != nil {
return nil, err
}
return &s, nil
} | go | func (b *redisBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
var options broker.SubscribeOptions
for _, o := range opts {
o(&options)
}
s := subscriber{
codec: b.opts.Codec,
conn: &redis.PubSubConn{Conn: b.pool.Get()},
topic: topic,
handle: handler,
opts: options,
}
// Run the receiver routine.
go s.recv()
if err := s.conn.Subscribe(s.topic); err != nil {
return nil, err
}
return &s, nil
} | [
"func",
"(",
"b",
"*",
"redisBroker",
")",
"Subscribe",
"(",
"topic",
"string",
",",
"handler",
"broker",
".",
"Handler",
",",
"opts",
"...",
"broker",
".",
"SubscribeOption",
")",
"(",
"broker",
".",
"Subscriber",
",",
"error",
")",
"{",
"var",
"options",
"broker",
".",
"SubscribeOptions",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"s",
":=",
"subscriber",
"{",
"codec",
":",
"b",
".",
"opts",
".",
"Codec",
",",
"conn",
":",
"&",
"redis",
".",
"PubSubConn",
"{",
"Conn",
":",
"b",
".",
"pool",
".",
"Get",
"(",
")",
"}",
",",
"topic",
":",
"topic",
",",
"handle",
":",
"handler",
",",
"opts",
":",
"options",
",",
"}",
"\n",
"go",
"s",
".",
"recv",
"(",
")",
"\n",
"if",
"err",
":=",
"s",
".",
"conn",
".",
"Subscribe",
"(",
"s",
".",
"topic",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"s",
",",
"nil",
"\n",
"}"
] | // Subscribe returns a subscriber for the topic and handler. | [
"Subscribe",
"returns",
"a",
"subscriber",
"for",
"the",
"topic",
"and",
"handler",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/redis/redis.go#L214-L236 | train |
micro/go-plugins | broker/stomp/context.go | setSubscribeOption | func setSubscribeOption(k, v interface{}) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | go | func setSubscribeOption(k, v interface{}) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | [
"func",
"setSubscribeOption",
"(",
"k",
",",
"v",
"interface",
"{",
"}",
")",
"broker",
".",
"SubscribeOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"SubscribeOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"k",
",",
"v",
")",
"\n",
"}",
"\n",
"}"
] | // setSubscribeOption returns a function to setup a context with given value | [
"setSubscribeOption",
"returns",
"a",
"function",
"to",
"setup",
"a",
"context",
"with",
"given",
"value"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/context.go#L25-L32 | train |
micro/go-plugins | broker/stomp/context.go | setBrokerOption | func setBrokerOption(k, v interface{}) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | go | func setBrokerOption(k, v interface{}) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | [
"func",
"setBrokerOption",
"(",
"k",
",",
"v",
"interface",
"{",
"}",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"k",
",",
"v",
")",
"\n",
"}",
"\n",
"}"
] | // setBrokerOption returns a function to setup a context with given value | [
"setBrokerOption",
"returns",
"a",
"function",
"to",
"setup",
"a",
"context",
"with",
"given",
"value"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/context.go#L35-L42 | train |
micro/go-plugins | broker/stomp/context.go | setPublishOption | func setPublishOption(k, v interface{}) broker.PublishOption {
return func(o *broker.PublishOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | go | func setPublishOption(k, v interface{}) broker.PublishOption {
return func(o *broker.PublishOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
} | [
"func",
"setPublishOption",
"(",
"k",
",",
"v",
"interface",
"{",
"}",
")",
"broker",
".",
"PublishOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"PublishOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"k",
",",
"v",
")",
"\n",
"}",
"\n",
"}"
] | // setPublishOption returns a function to setup a context with given value | [
"setPublishOption",
"returns",
"a",
"function",
"to",
"setup",
"a",
"context",
"with",
"given",
"value"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/context.go#L45-L52 | train |
micro/go-plugins | server/grpc/util.go | convertCode | func convertCode(err error) codes.Code {
switch err {
case nil:
return codes.OK
case io.EOF:
return codes.OutOfRange
case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
return codes.FailedPrecondition
case os.ErrInvalid:
return codes.InvalidArgument
case context.Canceled:
return codes.Canceled
case context.DeadlineExceeded:
return codes.DeadlineExceeded
}
switch {
case os.IsExist(err):
return codes.AlreadyExists
case os.IsNotExist(err):
return codes.NotFound
case os.IsPermission(err):
return codes.PermissionDenied
}
return codes.Unknown
} | go | func convertCode(err error) codes.Code {
switch err {
case nil:
return codes.OK
case io.EOF:
return codes.OutOfRange
case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
return codes.FailedPrecondition
case os.ErrInvalid:
return codes.InvalidArgument
case context.Canceled:
return codes.Canceled
case context.DeadlineExceeded:
return codes.DeadlineExceeded
}
switch {
case os.IsExist(err):
return codes.AlreadyExists
case os.IsNotExist(err):
return codes.NotFound
case os.IsPermission(err):
return codes.PermissionDenied
}
return codes.Unknown
} | [
"func",
"convertCode",
"(",
"err",
"error",
")",
"codes",
".",
"Code",
"{",
"switch",
"err",
"{",
"case",
"nil",
":",
"return",
"codes",
".",
"OK",
"\n",
"case",
"io",
".",
"EOF",
":",
"return",
"codes",
".",
"OutOfRange",
"\n",
"case",
"io",
".",
"ErrClosedPipe",
",",
"io",
".",
"ErrNoProgress",
",",
"io",
".",
"ErrShortBuffer",
",",
"io",
".",
"ErrShortWrite",
",",
"io",
".",
"ErrUnexpectedEOF",
":",
"return",
"codes",
".",
"FailedPrecondition",
"\n",
"case",
"os",
".",
"ErrInvalid",
":",
"return",
"codes",
".",
"InvalidArgument",
"\n",
"case",
"context",
".",
"Canceled",
":",
"return",
"codes",
".",
"Canceled",
"\n",
"case",
"context",
".",
"DeadlineExceeded",
":",
"return",
"codes",
".",
"DeadlineExceeded",
"\n",
"}",
"\n",
"switch",
"{",
"case",
"os",
".",
"IsExist",
"(",
"err",
")",
":",
"return",
"codes",
".",
"AlreadyExists",
"\n",
"case",
"os",
".",
"IsNotExist",
"(",
"err",
")",
":",
"return",
"codes",
".",
"NotFound",
"\n",
"case",
"os",
".",
"IsPermission",
"(",
"err",
")",
":",
"return",
"codes",
".",
"PermissionDenied",
"\n",
"}",
"\n",
"return",
"codes",
".",
"Unknown",
"\n",
"}"
] | // convertCode converts a standard Go error into its canonical code. Note that
// this is only used to translate the error returned by the server applications. | [
"convertCode",
"converts",
"a",
"standard",
"Go",
"error",
"into",
"its",
"canonical",
"code",
".",
"Note",
"that",
"this",
"is",
"only",
"used",
"to",
"translate",
"the",
"error",
"returned",
"by",
"the",
"server",
"applications",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/server/grpc/util.go#L24-L48 | train |
micro/go-plugins | wrapper/endpoint/endpoint.go | NewClientWrapper | func NewClientWrapper(cw client.Wrapper, eps ...string) client.Wrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(c client.Client) client.Client {
return &clientWrapper{endpoints, cw, c}
}
} | go | func NewClientWrapper(cw client.Wrapper, eps ...string) client.Wrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(c client.Client) client.Client {
return &clientWrapper{endpoints, cw, c}
}
} | [
"func",
"NewClientWrapper",
"(",
"cw",
"client",
".",
"Wrapper",
",",
"eps",
"...",
"string",
")",
"client",
".",
"Wrapper",
"{",
"endpoints",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"for",
"_",
",",
"ep",
":=",
"range",
"eps",
"{",
"endpoints",
"[",
"ep",
"]",
"=",
"true",
"\n",
"}",
"\n",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"clientWrapper",
"{",
"endpoints",
",",
"cw",
",",
"c",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper wraps another client wrapper but only executes when the endpoints specified are executed. | [
"NewClientWrapper",
"wraps",
"another",
"client",
"wrapper",
"but",
"only",
"executes",
"when",
"the",
"endpoints",
"specified",
"are",
"executed",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/endpoint/endpoint.go#L29-L39 | train |
micro/go-plugins | wrapper/endpoint/endpoint.go | NewHandlerWrapper | func NewHandlerWrapper(hw server.HandlerWrapper, eps ...string) server.HandlerWrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// execute the handler wrapper?
if !endpoints[req.Endpoint()] {
// no
return h(ctx, req, rsp)
}
// yes
return hw(h)(ctx, req, rsp)
}
}
} | go | func NewHandlerWrapper(hw server.HandlerWrapper, eps ...string) server.HandlerWrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// execute the handler wrapper?
if !endpoints[req.Endpoint()] {
// no
return h(ctx, req, rsp)
}
// yes
return hw(h)(ctx, req, rsp)
}
}
} | [
"func",
"NewHandlerWrapper",
"(",
"hw",
"server",
".",
"HandlerWrapper",
",",
"eps",
"...",
"string",
")",
"server",
".",
"HandlerWrapper",
"{",
"endpoints",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"for",
"_",
",",
"ep",
":=",
"range",
"eps",
"{",
"endpoints",
"[",
"ep",
"]",
"=",
"true",
"\n",
"}",
"\n",
"return",
"func",
"(",
"h",
"server",
".",
"HandlerFunc",
")",
"server",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"server",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"!",
"endpoints",
"[",
"req",
".",
"Endpoint",
"(",
")",
"]",
"{",
"return",
"h",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"}",
"\n",
"return",
"hw",
"(",
"h",
")",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewHandlerWrapper wraps another handler wrapper but only executes when the endpoints specified are executed. | [
"NewHandlerWrapper",
"wraps",
"another",
"handler",
"wrapper",
"but",
"only",
"executes",
"when",
"the",
"endpoints",
"specified",
"are",
"executed",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/endpoint/endpoint.go#L42-L61 | train |
micro/go-plugins | micro/cors/cors.go | NewPlugin | func NewPlugin() plugin.Plugin {
return &allowedCors{
allowedHeaders: []string{},
allowedOrigins: []string{},
allowedMethods: []string{},
}
} | go | func NewPlugin() plugin.Plugin {
return &allowedCors{
allowedHeaders: []string{},
allowedOrigins: []string{},
allowedMethods: []string{},
}
} | [
"func",
"NewPlugin",
"(",
")",
"plugin",
".",
"Plugin",
"{",
"return",
"&",
"allowedCors",
"{",
"allowedHeaders",
":",
"[",
"]",
"string",
"{",
"}",
",",
"allowedOrigins",
":",
"[",
"]",
"string",
"{",
"}",
",",
"allowedMethods",
":",
"[",
"]",
"string",
"{",
"}",
",",
"}",
"\n",
"}"
] | // NewPlugin Creates the CORS Plugin | [
"NewPlugin",
"Creates",
"the",
"CORS",
"Plugin"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/cors/cors.go#L83-L89 | train |
micro/go-plugins | wrapper/service/service.go | NewClientWrapper | func NewClientWrapper(service micro.Service) client.Wrapper {
return func(c client.Client) client.Client {
return &clientWrapper{service, c}
}
} | go | func NewClientWrapper(service micro.Service) client.Wrapper {
return func(c client.Client) client.Client {
return &clientWrapper{service, c}
}
} | [
"func",
"NewClientWrapper",
"(",
"service",
"micro",
".",
"Service",
")",
"client",
".",
"Wrapper",
"{",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"clientWrapper",
"{",
"service",
",",
"c",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper wraps a service within a client so it can be accessed by subsequent client wrappers. | [
"NewClientWrapper",
"wraps",
"a",
"service",
"within",
"a",
"client",
"so",
"it",
"can",
"be",
"accessed",
"by",
"subsequent",
"client",
"wrappers",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/service/service.go#L23-L27 | train |
micro/go-plugins | wrapper/service/service.go | NewHandlerWrapper | func NewHandlerWrapper(service micro.Service) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
ctx = micro.NewContext(ctx, service)
return h(ctx, req, rsp)
}
}
} | go | func NewHandlerWrapper(service micro.Service) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
ctx = micro.NewContext(ctx, service)
return h(ctx, req, rsp)
}
}
} | [
"func",
"NewHandlerWrapper",
"(",
"service",
"micro",
".",
"Service",
")",
"server",
".",
"HandlerWrapper",
"{",
"return",
"func",
"(",
"h",
"server",
".",
"HandlerFunc",
")",
"server",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"server",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
")",
"error",
"{",
"ctx",
"=",
"micro",
".",
"NewContext",
"(",
"ctx",
",",
"service",
")",
"\n",
"return",
"h",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewHandlerWrapper wraps a service within the handler so it can be accessed by the handler itself. | [
"NewHandlerWrapper",
"wraps",
"a",
"service",
"within",
"the",
"handler",
"so",
"it",
"can",
"be",
"accessed",
"by",
"the",
"handler",
"itself",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/service/service.go#L30-L37 | train |
micro/go-plugins | wrapper/select/shard/shard.go | NewClientWrapper | func NewClientWrapper(key string) client.Wrapper {
return func(c client.Client) client.Client {
return &shard{
key: key,
Client: c,
}
}
} | go | func NewClientWrapper(key string) client.Wrapper {
return func(c client.Client) client.Client {
return &shard{
key: key,
Client: c,
}
}
} | [
"func",
"NewClientWrapper",
"(",
"key",
"string",
")",
"client",
".",
"Wrapper",
"{",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"shard",
"{",
"key",
":",
"key",
",",
"Client",
":",
"c",
",",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper is a wrapper which shards based on a header key value | [
"NewClientWrapper",
"is",
"a",
"wrapper",
"which",
"shards",
"based",
"on",
"a",
"header",
"key",
"value"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/select/shard/shard.go#L62-L69 | train |
micro/go-plugins | broker/googlepubsub/options.go | ClientOption | func ClientOption(c ...option.ClientOption) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, clientOptionKey{}, c)
}
} | go | func ClientOption(c ...option.ClientOption) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, clientOptionKey{}, c)
}
} | [
"func",
"ClientOption",
"(",
"c",
"...",
"option",
".",
"ClientOption",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"clientOptionKey",
"{",
"}",
",",
"c",
")",
"\n",
"}",
"\n",
"}"
] | // ClientOption is a broker Option which allows google pubsub client options to be
// set for the client | [
"ClientOption",
"is",
"a",
"broker",
"Option",
"which",
"allows",
"google",
"pubsub",
"client",
"options",
"to",
"be",
"set",
"for",
"the",
"client"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/options.go#L25-L32 | train |
micro/go-plugins | broker/googlepubsub/options.go | ProjectID | func ProjectID(id string) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, projectIDKey{}, id)
}
} | go | func ProjectID(id string) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, projectIDKey{}, id)
}
} | [
"func",
"ProjectID",
"(",
"id",
"string",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"projectIDKey",
"{",
"}",
",",
"id",
")",
"\n",
"}",
"\n",
"}"
] | // ProjectID provides an option which sets the google project id | [
"ProjectID",
"provides",
"an",
"option",
"which",
"sets",
"the",
"google",
"project",
"id"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/options.go#L35-L42 | train |
micro/go-plugins | broker/googlepubsub/options.go | CreateSubscription | func CreateSubscription(b bool) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, createSubscription{}, b)
}
} | go | func CreateSubscription(b bool) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, createSubscription{}, b)
}
} | [
"func",
"CreateSubscription",
"(",
"b",
"bool",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"createSubscription",
"{",
"}",
",",
"b",
")",
"\n",
"}",
"\n",
"}"
] | // CreateSubscription prevents the creation of the subscription if it not exists | [
"CreateSubscription",
"prevents",
"the",
"creation",
"of",
"the",
"subscription",
"if",
"it",
"not",
"exists"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/options.go#L45-L53 | train |
micro/go-plugins | broker/googlepubsub/options.go | DeleteSubscription | func DeleteSubscription(b bool) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, deleteSubscription{}, b)
}
} | go | func DeleteSubscription(b bool) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, deleteSubscription{}, b)
}
} | [
"func",
"DeleteSubscription",
"(",
"b",
"bool",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"deleteSubscription",
"{",
"}",
",",
"b",
")",
"\n",
"}",
"\n",
"}"
] | // DeleteSubscription prevents the deletion of the subscription if it not exists | [
"DeleteSubscription",
"prevents",
"the",
"deletion",
"of",
"the",
"subscription",
"if",
"it",
"not",
"exists"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/options.go#L56-L64 | train |
micro/go-plugins | broker/googlepubsub/options.go | MaxExtension | func MaxExtension(d time.Duration) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxExtensionKey{}, d)
}
} | go | func MaxExtension(d time.Duration) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxExtensionKey{}, d)
}
} | [
"func",
"MaxExtension",
"(",
"d",
"time",
".",
"Duration",
")",
"broker",
".",
"SubscribeOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"SubscribeOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"maxExtensionKey",
"{",
"}",
",",
"d",
")",
"\n",
"}",
"\n",
"}"
] | // MaxExtension is the maximum period for which the Subscription should
// automatically extend the ack deadline for each message. | [
"MaxExtension",
"is",
"the",
"maximum",
"period",
"for",
"which",
"the",
"Subscription",
"should",
"automatically",
"extend",
"the",
"ack",
"deadline",
"for",
"each",
"message",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/options.go#L80-L88 | train |
micro/go-plugins | client/grpc/grpc.go | secure | func (g *grpcClient) secure() grpc.DialOption {
if g.opts.Context != nil {
if v := g.opts.Context.Value(tlsAuth{}); v != nil {
tls := v.(*tls.Config)
creds := credentials.NewTLS(tls)
return grpc.WithTransportCredentials(creds)
}
}
return grpc.WithInsecure()
} | go | func (g *grpcClient) secure() grpc.DialOption {
if g.opts.Context != nil {
if v := g.opts.Context.Value(tlsAuth{}); v != nil {
tls := v.(*tls.Config)
creds := credentials.NewTLS(tls)
return grpc.WithTransportCredentials(creds)
}
}
return grpc.WithInsecure()
} | [
"func",
"(",
"g",
"*",
"grpcClient",
")",
"secure",
"(",
")",
"grpc",
".",
"DialOption",
"{",
"if",
"g",
".",
"opts",
".",
"Context",
"!=",
"nil",
"{",
"if",
"v",
":=",
"g",
".",
"opts",
".",
"Context",
".",
"Value",
"(",
"tlsAuth",
"{",
"}",
")",
";",
"v",
"!=",
"nil",
"{",
"tls",
":=",
"v",
".",
"(",
"*",
"tls",
".",
"Config",
")",
"\n",
"creds",
":=",
"credentials",
".",
"NewTLS",
"(",
"tls",
")",
"\n",
"return",
"grpc",
".",
"WithTransportCredentials",
"(",
"creds",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"grpc",
".",
"WithInsecure",
"(",
")",
"\n",
"}"
] | // secure returns the dial option for whether its a secure or insecure connection | [
"secure",
"returns",
"the",
"dial",
"option",
"for",
"whether",
"its",
"a",
"secure",
"or",
"insecure",
"connection"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/client/grpc/grpc.go#L42-L51 | train |
micro/go-plugins | wrapper/trace/awsxray/awsxray.go | NewCallWrapper | func NewCallWrapper(opts ...Option) client.CallWrapper {
options := Options{
Name: "go.micro.client.CallFunc",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
var err error
s := getSegment(options.Name, ctx)
defer func() {
setCallStatus(s, node.Address, req.Endpoint(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = cf(ctx, node, req, rsp, opts)
return err
}
}
} | go | func NewCallWrapper(opts ...Option) client.CallWrapper {
options := Options{
Name: "go.micro.client.CallFunc",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
var err error
s := getSegment(options.Name, ctx)
defer func() {
setCallStatus(s, node.Address, req.Endpoint(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = cf(ctx, node, req, rsp, opts)
return err
}
}
} | [
"func",
"NewCallWrapper",
"(",
"opts",
"...",
"Option",
")",
"client",
".",
"CallWrapper",
"{",
"options",
":=",
"Options",
"{",
"Name",
":",
"\"go.micro.client.CallFunc\"",
",",
"Daemon",
":",
"\"localhost:2000\"",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"x",
":=",
"newXRay",
"(",
"options",
")",
"\n",
"return",
"func",
"(",
"cf",
"client",
".",
"CallFunc",
")",
"client",
".",
"CallFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"node",
"*",
"registry",
".",
"Node",
",",
"req",
"client",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
",",
"opts",
"client",
".",
"CallOptions",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"s",
":=",
"getSegment",
"(",
"options",
".",
"Name",
",",
"ctx",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"setCallStatus",
"(",
"s",
",",
"node",
".",
"Address",
",",
"req",
".",
"Endpoint",
"(",
")",
",",
"err",
")",
"\n",
"go",
"record",
"(",
"x",
",",
"s",
")",
"\n",
"}",
"(",
")",
"\n",
"ctx",
"=",
"newContext",
"(",
"ctx",
",",
"s",
")",
"\n",
"err",
"=",
"cf",
"(",
"ctx",
",",
"node",
",",
"req",
",",
"rsp",
",",
"opts",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewCallWrapper accepts Options and returns a Trace Call Wrapper for individual node calls made by the client | [
"NewCallWrapper",
"accepts",
"Options",
"and",
"returns",
"a",
"Trace",
"Call",
"Wrapper",
"for",
"individual",
"node",
"calls",
"made",
"by",
"the",
"client"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/awsxray/awsxray.go#L33-L60 | train |
micro/go-plugins | wrapper/trace/awsxray/awsxray.go | NewClientWrapper | func NewClientWrapper(opts ...Option) client.Wrapper {
options := Options{
Name: "go.micro.client.Call",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
return func(c client.Client) client.Client {
return &xrayWrapper{options, newXRay(options), c}
}
} | go | func NewClientWrapper(opts ...Option) client.Wrapper {
options := Options{
Name: "go.micro.client.Call",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
return func(c client.Client) client.Client {
return &xrayWrapper{options, newXRay(options), c}
}
} | [
"func",
"NewClientWrapper",
"(",
"opts",
"...",
"Option",
")",
"client",
".",
"Wrapper",
"{",
"options",
":=",
"Options",
"{",
"Name",
":",
"\"go.micro.client.Call\"",
",",
"Daemon",
":",
"\"localhost:2000\"",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"xrayWrapper",
"{",
"options",
",",
"newXRay",
"(",
"options",
")",
",",
"c",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper accepts Options and returns a Trace Client Wrapper which tracks high level service calls | [
"NewClientWrapper",
"accepts",
"Options",
"and",
"returns",
"a",
"Trace",
"Client",
"Wrapper",
"which",
"tracks",
"high",
"level",
"service",
"calls"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/awsxray/awsxray.go#L63-L76 | train |
micro/go-plugins | wrapper/trace/awsxray/awsxray.go | NewHandlerWrapper | func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
options := Options{
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
name := options.Name
if len(name) == 0 {
// default name
name = req.Service() + "." + req.Endpoint()
}
var err error
s := getSegment(name, ctx)
defer func() {
setCallStatus(s, req.Service(), req.Endpoint(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = h(ctx, req, rsp)
return err
}
}
} | go | func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
options := Options{
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
name := options.Name
if len(name) == 0 {
// default name
name = req.Service() + "." + req.Endpoint()
}
var err error
s := getSegment(name, ctx)
defer func() {
setCallStatus(s, req.Service(), req.Endpoint(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = h(ctx, req, rsp)
return err
}
}
} | [
"func",
"NewHandlerWrapper",
"(",
"opts",
"...",
"Option",
")",
"server",
".",
"HandlerWrapper",
"{",
"options",
":=",
"Options",
"{",
"Daemon",
":",
"\"localhost:2000\"",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"x",
":=",
"newXRay",
"(",
"options",
")",
"\n",
"return",
"func",
"(",
"h",
"server",
".",
"HandlerFunc",
")",
"server",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"server",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
")",
"error",
"{",
"name",
":=",
"options",
".",
"Name",
"\n",
"if",
"len",
"(",
"name",
")",
"==",
"0",
"{",
"name",
"=",
"req",
".",
"Service",
"(",
")",
"+",
"\".\"",
"+",
"req",
".",
"Endpoint",
"(",
")",
"\n",
"}",
"\n",
"var",
"err",
"error",
"\n",
"s",
":=",
"getSegment",
"(",
"name",
",",
"ctx",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"setCallStatus",
"(",
"s",
",",
"req",
".",
"Service",
"(",
")",
",",
"req",
".",
"Endpoint",
"(",
")",
",",
"err",
")",
"\n",
"go",
"record",
"(",
"x",
",",
"s",
")",
"\n",
"}",
"(",
")",
"\n",
"ctx",
"=",
"newContext",
"(",
"ctx",
",",
"s",
")",
"\n",
"err",
"=",
"h",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewHandlerWrapper accepts Options and returns a Trace Handler Wrapper | [
"NewHandlerWrapper",
"accepts",
"Options",
"and",
"returns",
"a",
"Trace",
"Handler",
"Wrapper"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/awsxray/awsxray.go#L79-L111 | train |
micro/go-plugins | selector/label/options.go | Label | func Label(k, v string) selector.Option {
return func(o *selector.Options) {
l, ok := o.Context.Value(labelKey{}).([]label)
if !ok {
l = []label{}
}
l = append(l, label{k, v})
o.Context = context.WithValue(o.Context, labelKey{}, l)
}
} | go | func Label(k, v string) selector.Option {
return func(o *selector.Options) {
l, ok := o.Context.Value(labelKey{}).([]label)
if !ok {
l = []label{}
}
l = append(l, label{k, v})
o.Context = context.WithValue(o.Context, labelKey{}, l)
}
} | [
"func",
"Label",
"(",
"k",
",",
"v",
"string",
")",
"selector",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"selector",
".",
"Options",
")",
"{",
"l",
",",
"ok",
":=",
"o",
".",
"Context",
".",
"Value",
"(",
"labelKey",
"{",
"}",
")",
".",
"(",
"[",
"]",
"label",
")",
"\n",
"if",
"!",
"ok",
"{",
"l",
"=",
"[",
"]",
"label",
"{",
"}",
"\n",
"}",
"\n",
"l",
"=",
"append",
"(",
"l",
",",
"label",
"{",
"k",
",",
"v",
"}",
")",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"labelKey",
"{",
"}",
",",
"l",
")",
"\n",
"}",
"\n",
"}"
] | // Label used in the priority label list | [
"Label",
"used",
"in",
"the",
"priority",
"label",
"list"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/selector/label/options.go#L17-L26 | train |
micro/go-plugins | broker/stomp/options.go | Auth | func Auth(username string, password string) broker.Option {
return setBrokerOption(authKey{}, &authRecord{
username: username,
password: password,
})
} | go | func Auth(username string, password string) broker.Option {
return setBrokerOption(authKey{}, &authRecord{
username: username,
password: password,
})
} | [
"func",
"Auth",
"(",
"username",
"string",
",",
"password",
"string",
")",
"broker",
".",
"Option",
"{",
"return",
"setBrokerOption",
"(",
"authKey",
"{",
"}",
",",
"&",
"authRecord",
"{",
"username",
":",
"username",
",",
"password",
":",
"password",
",",
"}",
")",
"\n",
"}"
] | // Auth sets the authentication information | [
"Auth",
"sets",
"the",
"authentication",
"information"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/options.go#L50-L55 | train |
micro/go-plugins | micro/index/index.go | WithResponse | func WithResponse(status int, header http.Header, body []byte) plugin.Plugin {
return &index{
r: &response{
status: status,
header: header,
body: body,
},
}
} | go | func WithResponse(status int, header http.Header, body []byte) plugin.Plugin {
return &index{
r: &response{
status: status,
header: header,
body: body,
},
}
} | [
"func",
"WithResponse",
"(",
"status",
"int",
",",
"header",
"http",
".",
"Header",
",",
"body",
"[",
"]",
"byte",
")",
"plugin",
".",
"Plugin",
"{",
"return",
"&",
"index",
"{",
"r",
":",
"&",
"response",
"{",
"status",
":",
"status",
",",
"header",
":",
"header",
",",
"body",
":",
"body",
",",
"}",
",",
"}",
"\n",
"}"
] | // WithContent will write the given status, header and body | [
"WithContent",
"will",
"write",
"the",
"given",
"status",
"header",
"and",
"body"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/index/index.go#L139-L147 | train |
micro/go-plugins | broker/sqs/sqs.go | run | func (s *subscriber) run(hdlr broker.Handler) {
log.Log(fmt.Sprintf("SQS subscription started. Queue:%s, URL: %s", s.queueName, s.URL))
for {
select {
case <-s.exit:
return
default:
result, err := s.svc.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: &s.URL,
MaxNumberOfMessages: s.getMaxMessages(),
VisibilityTimeout: s.getVisibilityTimeout(),
WaitTimeSeconds: s.getWaitSeconds(),
AttributeNames: aws.StringSlice([]string{
"SentTimestamp", // TODO: not currently exposing this to plugin users
}),
MessageAttributeNames: aws.StringSlice([]string{
"All",
}),
})
if err != nil {
time.Sleep(time.Second)
log.Log(fmt.Sprintf("Error receiving SQS message: %s", err.Error()))
continue
}
if len(result.Messages) == 0 {
time.Sleep(time.Second)
continue
}
for _, sm := range result.Messages {
s.handleMessage(sm, hdlr)
}
}
}
} | go | func (s *subscriber) run(hdlr broker.Handler) {
log.Log(fmt.Sprintf("SQS subscription started. Queue:%s, URL: %s", s.queueName, s.URL))
for {
select {
case <-s.exit:
return
default:
result, err := s.svc.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: &s.URL,
MaxNumberOfMessages: s.getMaxMessages(),
VisibilityTimeout: s.getVisibilityTimeout(),
WaitTimeSeconds: s.getWaitSeconds(),
AttributeNames: aws.StringSlice([]string{
"SentTimestamp", // TODO: not currently exposing this to plugin users
}),
MessageAttributeNames: aws.StringSlice([]string{
"All",
}),
})
if err != nil {
time.Sleep(time.Second)
log.Log(fmt.Sprintf("Error receiving SQS message: %s", err.Error()))
continue
}
if len(result.Messages) == 0 {
time.Sleep(time.Second)
continue
}
for _, sm := range result.Messages {
s.handleMessage(sm, hdlr)
}
}
}
} | [
"func",
"(",
"s",
"*",
"subscriber",
")",
"run",
"(",
"hdlr",
"broker",
".",
"Handler",
")",
"{",
"log",
".",
"Log",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"SQS subscription started. Queue:%s, URL: %s\"",
",",
"s",
".",
"queueName",
",",
"s",
".",
"URL",
")",
")",
"\n",
"for",
"{",
"select",
"{",
"case",
"<-",
"s",
".",
"exit",
":",
"return",
"\n",
"default",
":",
"result",
",",
"err",
":=",
"s",
".",
"svc",
".",
"ReceiveMessage",
"(",
"&",
"sqs",
".",
"ReceiveMessageInput",
"{",
"QueueUrl",
":",
"&",
"s",
".",
"URL",
",",
"MaxNumberOfMessages",
":",
"s",
".",
"getMaxMessages",
"(",
")",
",",
"VisibilityTimeout",
":",
"s",
".",
"getVisibilityTimeout",
"(",
")",
",",
"WaitTimeSeconds",
":",
"s",
".",
"getWaitSeconds",
"(",
")",
",",
"AttributeNames",
":",
"aws",
".",
"StringSlice",
"(",
"[",
"]",
"string",
"{",
"\"SentTimestamp\"",
",",
"}",
")",
",",
"MessageAttributeNames",
":",
"aws",
".",
"StringSlice",
"(",
"[",
"]",
"string",
"{",
"\"All\"",
",",
"}",
")",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"time",
".",
"Sleep",
"(",
"time",
".",
"Second",
")",
"\n",
"log",
".",
"Log",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"Error receiving SQS message: %s\"",
",",
"err",
".",
"Error",
"(",
")",
")",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"len",
"(",
"result",
".",
"Messages",
")",
"==",
"0",
"{",
"time",
".",
"Sleep",
"(",
"time",
".",
"Second",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"for",
"_",
",",
"sm",
":=",
"range",
"result",
".",
"Messages",
"{",
"s",
".",
"handleMessage",
"(",
"sm",
",",
"hdlr",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // run is designed to run as a goroutine and poll SQS for new messages. Note that it's possible to receive
// more than one message from a single poll depending on the options configured for the plugin | [
"run",
"is",
"designed",
"to",
"run",
"as",
"a",
"goroutine",
"and",
"poll",
"SQS",
"for",
"new",
"messages",
".",
"Note",
"that",
"it",
"s",
"possible",
"to",
"receive",
"more",
"than",
"one",
"message",
"from",
"a",
"single",
"poll",
"depending",
"on",
"the",
"options",
"configured",
"for",
"the",
"plugin"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/sqs.go#L54-L90 | train |
micro/go-plugins | broker/sqs/sqs.go | Init | func (b *sqsBroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&b.options)
}
return nil
} | go | func (b *sqsBroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&b.options)
}
return nil
} | [
"func",
"(",
"b",
"*",
"sqsBroker",
")",
"Init",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"error",
"{",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"b",
".",
"options",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Init initializes a broker and configures an AWS session and SQS struct | [
"Init",
"initializes",
"a",
"broker",
"and",
"configures",
"an",
"AWS",
"session",
"and",
"SQS",
"struct"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/sqs.go#L206-L212 | train |
micro/go-plugins | broker/sqs/sqs.go | Publish | func (b *sqsBroker) Publish(queueName string, msg *broker.Message, opts ...broker.PublishOption) error {
queueURL, err := b.urlFromQueueName(queueName)
if err != nil {
return err
}
input := &sqs.SendMessageInput{
MessageBody: aws.String(string(msg.Body[:])),
QueueUrl: &queueURL,
}
input.MessageAttributes = copyMessageHeader(msg)
input.MessageDeduplicationId = b.generateDedupID(msg)
input.MessageGroupId = b.generateGroupID(msg)
log.Log(fmt.Sprintf("Publishing SQS message, %d bytes", len(msg.Body)))
_, err = b.svc.SendMessage(input)
if err != nil {
return err
}
// Broker interfaces don't let us do anything with message ID or sequence number
return nil
} | go | func (b *sqsBroker) Publish(queueName string, msg *broker.Message, opts ...broker.PublishOption) error {
queueURL, err := b.urlFromQueueName(queueName)
if err != nil {
return err
}
input := &sqs.SendMessageInput{
MessageBody: aws.String(string(msg.Body[:])),
QueueUrl: &queueURL,
}
input.MessageAttributes = copyMessageHeader(msg)
input.MessageDeduplicationId = b.generateDedupID(msg)
input.MessageGroupId = b.generateGroupID(msg)
log.Log(fmt.Sprintf("Publishing SQS message, %d bytes", len(msg.Body)))
_, err = b.svc.SendMessage(input)
if err != nil {
return err
}
// Broker interfaces don't let us do anything with message ID or sequence number
return nil
} | [
"func",
"(",
"b",
"*",
"sqsBroker",
")",
"Publish",
"(",
"queueName",
"string",
",",
"msg",
"*",
"broker",
".",
"Message",
",",
"opts",
"...",
"broker",
".",
"PublishOption",
")",
"error",
"{",
"queueURL",
",",
"err",
":=",
"b",
".",
"urlFromQueueName",
"(",
"queueName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"input",
":=",
"&",
"sqs",
".",
"SendMessageInput",
"{",
"MessageBody",
":",
"aws",
".",
"String",
"(",
"string",
"(",
"msg",
".",
"Body",
"[",
":",
"]",
")",
")",
",",
"QueueUrl",
":",
"&",
"queueURL",
",",
"}",
"\n",
"input",
".",
"MessageAttributes",
"=",
"copyMessageHeader",
"(",
"msg",
")",
"\n",
"input",
".",
"MessageDeduplicationId",
"=",
"b",
".",
"generateDedupID",
"(",
"msg",
")",
"\n",
"input",
".",
"MessageGroupId",
"=",
"b",
".",
"generateGroupID",
"(",
"msg",
")",
"\n",
"log",
".",
"Log",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"Publishing SQS message, %d bytes\"",
",",
"len",
"(",
"msg",
".",
"Body",
")",
")",
")",
"\n",
"_",
",",
"err",
"=",
"b",
".",
"svc",
".",
"SendMessage",
"(",
"input",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Publish publishes a message via SQS | [
"Publish",
"publishes",
"a",
"message",
"via",
"SQS"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/sqs.go#L215-L238 | train |
micro/go-plugins | broker/sqs/sqs.go | Subscribe | func (b *sqsBroker) Subscribe(queueName string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
queueURL, err := b.urlFromQueueName(queueName)
if err != nil {
return nil, err
}
options := broker.SubscribeOptions{
AutoAck: true,
Queue: queueName,
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
subscriber := &subscriber{
options: options,
URL: queueURL,
queueName: queueName,
svc: b.svc,
exit: make(chan bool),
}
go subscriber.run(h)
return subscriber, nil
} | go | func (b *sqsBroker) Subscribe(queueName string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
queueURL, err := b.urlFromQueueName(queueName)
if err != nil {
return nil, err
}
options := broker.SubscribeOptions{
AutoAck: true,
Queue: queueName,
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
subscriber := &subscriber{
options: options,
URL: queueURL,
queueName: queueName,
svc: b.svc,
exit: make(chan bool),
}
go subscriber.run(h)
return subscriber, nil
} | [
"func",
"(",
"b",
"*",
"sqsBroker",
")",
"Subscribe",
"(",
"queueName",
"string",
",",
"h",
"broker",
".",
"Handler",
",",
"opts",
"...",
"broker",
".",
"SubscribeOption",
")",
"(",
"broker",
".",
"Subscriber",
",",
"error",
")",
"{",
"queueURL",
",",
"err",
":=",
"b",
".",
"urlFromQueueName",
"(",
"queueName",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"options",
":=",
"broker",
".",
"SubscribeOptions",
"{",
"AutoAck",
":",
"true",
",",
"Queue",
":",
"queueName",
",",
"Context",
":",
"context",
".",
"Background",
"(",
")",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"subscriber",
":=",
"&",
"subscriber",
"{",
"options",
":",
"options",
",",
"URL",
":",
"queueURL",
",",
"queueName",
":",
"queueName",
",",
"svc",
":",
"b",
".",
"svc",
",",
"exit",
":",
"make",
"(",
"chan",
"bool",
")",
",",
"}",
"\n",
"go",
"subscriber",
".",
"run",
"(",
"h",
")",
"\n",
"return",
"subscriber",
",",
"nil",
"\n",
"}"
] | // Subscribe subscribes to an SQS queue, starting a goroutine to poll for messages | [
"Subscribe",
"subscribes",
"to",
"an",
"SQS",
"queue",
"starting",
"a",
"goroutine",
"to",
"poll",
"for",
"messages"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/sqs.go#L241-L267 | train |
micro/go-plugins | broker/sqs/sqs.go | NewBroker | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
return &sqsBroker{
options: options,
}
} | go | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
return &sqsBroker{
options: options,
}
} | [
"func",
"NewBroker",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"broker",
".",
"Broker",
"{",
"options",
":=",
"broker",
".",
"Options",
"{",
"Context",
":",
"context",
".",
"Background",
"(",
")",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"return",
"&",
"sqsBroker",
"{",
"options",
":",
"options",
",",
"}",
"\n",
"}"
] | // NewBroker creates a new broker with options | [
"NewBroker",
"creates",
"a",
"new",
"broker",
"with",
"options"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/sqs.go#L335-L347 | train |
micro/go-plugins | broker/grpc/grpc.go | Publish | func (h *grpcHandler) Publish(ctx context.Context, msg *proto.Message) (*proto.Empty, error) {
if len(msg.Topic) == 0 {
return nil, merr.InternalServerError("go.micro.broker", "Topic not found")
}
m := &broker.Message{
Header: msg.Header,
Body: msg.Body,
}
p := &grpcPublication{m: m, t: msg.Topic}
h.g.RLock()
for _, subscriber := range h.g.subscribers[msg.Topic] {
if msg.Id == subscriber.id {
// sub is sync; crufty rate limiting
// so we don't hose the cpu
subscriber.fn(p)
}
}
h.g.RUnlock()
return new(proto.Empty), nil
} | go | func (h *grpcHandler) Publish(ctx context.Context, msg *proto.Message) (*proto.Empty, error) {
if len(msg.Topic) == 0 {
return nil, merr.InternalServerError("go.micro.broker", "Topic not found")
}
m := &broker.Message{
Header: msg.Header,
Body: msg.Body,
}
p := &grpcPublication{m: m, t: msg.Topic}
h.g.RLock()
for _, subscriber := range h.g.subscribers[msg.Topic] {
if msg.Id == subscriber.id {
// sub is sync; crufty rate limiting
// so we don't hose the cpu
subscriber.fn(p)
}
}
h.g.RUnlock()
return new(proto.Empty), nil
} | [
"func",
"(",
"h",
"*",
"grpcHandler",
")",
"Publish",
"(",
"ctx",
"context",
".",
"Context",
",",
"msg",
"*",
"proto",
".",
"Message",
")",
"(",
"*",
"proto",
".",
"Empty",
",",
"error",
")",
"{",
"if",
"len",
"(",
"msg",
".",
"Topic",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"merr",
".",
"InternalServerError",
"(",
"\"go.micro.broker\"",
",",
"\"Topic not found\"",
")",
"\n",
"}",
"\n",
"m",
":=",
"&",
"broker",
".",
"Message",
"{",
"Header",
":",
"msg",
".",
"Header",
",",
"Body",
":",
"msg",
".",
"Body",
",",
"}",
"\n",
"p",
":=",
"&",
"grpcPublication",
"{",
"m",
":",
"m",
",",
"t",
":",
"msg",
".",
"Topic",
"}",
"\n",
"h",
".",
"g",
".",
"RLock",
"(",
")",
"\n",
"for",
"_",
",",
"subscriber",
":=",
"range",
"h",
".",
"g",
".",
"subscribers",
"[",
"msg",
".",
"Topic",
"]",
"{",
"if",
"msg",
".",
"Id",
"==",
"subscriber",
".",
"id",
"{",
"subscriber",
".",
"fn",
"(",
"p",
")",
"\n",
"}",
"\n",
"}",
"\n",
"h",
".",
"g",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"new",
"(",
"proto",
".",
"Empty",
")",
",",
"nil",
"\n",
"}"
] | // The grpc handler | [
"The",
"grpc",
"handler"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/grpc/grpc.go#L148-L170 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | verb | func (r *Request) verb(method string) *Request {
r.method = method
return r
} | go | func (r *Request) verb(method string) *Request {
r.method = method
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"verb",
"(",
"method",
"string",
")",
"*",
"Request",
"{",
"r",
".",
"method",
"=",
"method",
"\n",
"return",
"r",
"\n",
"}"
] | // verb sets method | [
"verb",
"sets",
"method"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L38-L41 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Namespace | func (r *Request) Namespace(s string) *Request {
r.namespace = s
return r
} | go | func (r *Request) Namespace(s string) *Request {
r.namespace = s
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Namespace",
"(",
"s",
"string",
")",
"*",
"Request",
"{",
"r",
".",
"namespace",
"=",
"s",
"\n",
"return",
"r",
"\n",
"}"
] | // Namespace is to set the namespace to operate on | [
"Namespace",
"is",
"to",
"set",
"the",
"namespace",
"to",
"operate",
"on"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L70-L73 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Resource | func (r *Request) Resource(s string) *Request {
r.resource = s
return r
} | go | func (r *Request) Resource(s string) *Request {
r.resource = s
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Resource",
"(",
"s",
"string",
")",
"*",
"Request",
"{",
"r",
".",
"resource",
"=",
"s",
"\n",
"return",
"r",
"\n",
"}"
] | // Resource is the type of resource the operation is
// for, such as "services", "endpoints" or "pods" | [
"Resource",
"is",
"the",
"type",
"of",
"resource",
"the",
"operation",
"is",
"for",
"such",
"as",
"services",
"endpoints",
"or",
"pods"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L77-L80 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Name | func (r *Request) Name(s string) *Request {
r.resourceName = &s
return r
} | go | func (r *Request) Name(s string) *Request {
r.resourceName = &s
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Name",
"(",
"s",
"string",
")",
"*",
"Request",
"{",
"r",
".",
"resourceName",
"=",
"&",
"s",
"\n",
"return",
"r",
"\n",
"}"
] | // Name is for targeting a specific resource by id | [
"Name",
"is",
"for",
"targeting",
"a",
"specific",
"resource",
"by",
"id"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L83-L86 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Body | func (r *Request) Body(in interface{}) *Request {
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(&in); err != nil {
r.err = err
return r
}
r.body = b
return r
} | go | func (r *Request) Body(in interface{}) *Request {
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(&in); err != nil {
r.err = err
return r
}
r.body = b
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Body",
"(",
"in",
"interface",
"{",
"}",
")",
"*",
"Request",
"{",
"b",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"if",
"err",
":=",
"json",
".",
"NewEncoder",
"(",
"b",
")",
".",
"Encode",
"(",
"&",
"in",
")",
";",
"err",
"!=",
"nil",
"{",
"r",
".",
"err",
"=",
"err",
"\n",
"return",
"r",
"\n",
"}",
"\n",
"r",
".",
"body",
"=",
"b",
"\n",
"return",
"r",
"\n",
"}"
] | // Body pass in a body to set, this is for POST, PUT
// and PATCH requests | [
"Body",
"pass",
"in",
"a",
"body",
"to",
"set",
"this",
"is",
"for",
"POST",
"PUT",
"and",
"PATCH",
"requests"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L90-L98 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Params | func (r *Request) Params(p *Params) *Request {
for k, v := range p.LabelSelector {
r.params.Add("labelSelectors", k+"="+v)
}
return r
} | go | func (r *Request) Params(p *Params) *Request {
for k, v := range p.LabelSelector {
r.params.Add("labelSelectors", k+"="+v)
}
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Params",
"(",
"p",
"*",
"Params",
")",
"*",
"Request",
"{",
"for",
"k",
",",
"v",
":=",
"range",
"p",
".",
"LabelSelector",
"{",
"r",
".",
"params",
".",
"Add",
"(",
"\"labelSelectors\"",
",",
"k",
"+",
"\"=\"",
"+",
"v",
")",
"\n",
"}",
"\n",
"return",
"r",
"\n",
"}"
] | // Params isused to set paramters on a request | [
"Params",
"isused",
"to",
"set",
"paramters",
"on",
"a",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L101-L107 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | SetHeader | func (r *Request) SetHeader(key, value string) *Request {
r.header.Add(key, value)
return r
} | go | func (r *Request) SetHeader(key, value string) *Request {
r.header.Add(key, value)
return r
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"SetHeader",
"(",
"key",
",",
"value",
"string",
")",
"*",
"Request",
"{",
"r",
".",
"header",
".",
"Add",
"(",
"key",
",",
"value",
")",
"\n",
"return",
"r",
"\n",
"}"
] | // SetHeader sets a header on a request with
// a `key` and `value` | [
"SetHeader",
"sets",
"a",
"header",
"on",
"a",
"request",
"with",
"a",
"key",
"and",
"value"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L111-L114 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | request | func (r *Request) request() (*http.Request, error) {
url := fmt.Sprintf("%s/api/v1/namespaces/%s/%s/", r.host, r.namespace, r.resource)
// append resourceName if it is present
if r.resourceName != nil {
url += *r.resourceName
}
// append any query params
if len(r.params) > 0 {
url += "?" + r.params.Encode()
}
// build request
req, err := http.NewRequest(r.method, url, r.body)
if err != nil {
return nil, err
}
// set headers on request
req.Header = r.header
return req, nil
} | go | func (r *Request) request() (*http.Request, error) {
url := fmt.Sprintf("%s/api/v1/namespaces/%s/%s/", r.host, r.namespace, r.resource)
// append resourceName if it is present
if r.resourceName != nil {
url += *r.resourceName
}
// append any query params
if len(r.params) > 0 {
url += "?" + r.params.Encode()
}
// build request
req, err := http.NewRequest(r.method, url, r.body)
if err != nil {
return nil, err
}
// set headers on request
req.Header = r.header
return req, nil
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"request",
"(",
")",
"(",
"*",
"http",
".",
"Request",
",",
"error",
")",
"{",
"url",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"%s/api/v1/namespaces/%s/%s/\"",
",",
"r",
".",
"host",
",",
"r",
".",
"namespace",
",",
"r",
".",
"resource",
")",
"\n",
"if",
"r",
".",
"resourceName",
"!=",
"nil",
"{",
"url",
"+=",
"*",
"r",
".",
"resourceName",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"params",
")",
">",
"0",
"{",
"url",
"+=",
"\"?\"",
"+",
"r",
".",
"params",
".",
"Encode",
"(",
")",
"\n",
"}",
"\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"r",
".",
"method",
",",
"url",
",",
"r",
".",
"body",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"req",
".",
"Header",
"=",
"r",
".",
"header",
"\n",
"return",
"req",
",",
"nil",
"\n",
"}"
] | // request builds the http.Request from the options | [
"request",
"builds",
"the",
"http",
".",
"Request",
"from",
"the",
"options"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L117-L139 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Do | func (r *Request) Do() *Response {
if r.err != nil {
return &Response{
err: r.err,
}
}
req, err := r.request()
if err != nil {
return &Response{
err: err,
}
}
res, err := r.client.Do(req)
if err != nil {
return &Response{
err: err,
}
}
// return res, err
return newResponse(res, err)
} | go | func (r *Request) Do() *Response {
if r.err != nil {
return &Response{
err: r.err,
}
}
req, err := r.request()
if err != nil {
return &Response{
err: err,
}
}
res, err := r.client.Do(req)
if err != nil {
return &Response{
err: err,
}
}
// return res, err
return newResponse(res, err)
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Do",
"(",
")",
"*",
"Response",
"{",
"if",
"r",
".",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Response",
"{",
"err",
":",
"r",
".",
"err",
",",
"}",
"\n",
"}",
"\n",
"req",
",",
"err",
":=",
"r",
".",
"request",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Response",
"{",
"err",
":",
"err",
",",
"}",
"\n",
"}",
"\n",
"res",
",",
"err",
":=",
"r",
".",
"client",
".",
"Do",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Response",
"{",
"err",
":",
"err",
",",
"}",
"\n",
"}",
"\n",
"return",
"newResponse",
"(",
"res",
",",
"err",
")",
"\n",
"}"
] | // Do builds and triggers the request | [
"Do",
"builds",
"and",
"triggers",
"the",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L142-L165 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | Watch | func (r *Request) Watch() (watch.Watch, error) {
if r.err != nil {
return nil, r.err
}
r.params.Set("watch", "true")
req, err := r.request()
if err != nil {
return nil, err
}
w, err := watch.NewBodyWatcher(req, r.client)
return w, err
} | go | func (r *Request) Watch() (watch.Watch, error) {
if r.err != nil {
return nil, r.err
}
r.params.Set("watch", "true")
req, err := r.request()
if err != nil {
return nil, err
}
w, err := watch.NewBodyWatcher(req, r.client)
return w, err
} | [
"func",
"(",
"r",
"*",
"Request",
")",
"Watch",
"(",
")",
"(",
"watch",
".",
"Watch",
",",
"error",
")",
"{",
"if",
"r",
".",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"r",
".",
"err",
"\n",
"}",
"\n",
"r",
".",
"params",
".",
"Set",
"(",
"\"watch\"",
",",
"\"true\"",
")",
"\n",
"req",
",",
"err",
":=",
"r",
".",
"request",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"w",
",",
"err",
":=",
"watch",
".",
"NewBodyWatcher",
"(",
"req",
",",
"r",
".",
"client",
")",
"\n",
"return",
"w",
",",
"err",
"\n",
"}"
] | // Watch builds and triggers the request, but
// will watch instead of return an object | [
"Watch",
"builds",
"and",
"triggers",
"the",
"request",
"but",
"will",
"watch",
"instead",
"of",
"return",
"an",
"object"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L169-L183 | train |
micro/go-plugins | registry/kubernetes/client/api/request.go | NewRequest | func NewRequest(opts *Options) *Request {
req := &Request{
header: make(http.Header),
params: make(url.Values),
client: opts.Client,
namespace: opts.Namespace,
host: opts.Host,
}
if opts.BearerToken != nil {
req.SetHeader("Authorization", "Bearer "+*opts.BearerToken)
}
return req
} | go | func NewRequest(opts *Options) *Request {
req := &Request{
header: make(http.Header),
params: make(url.Values),
client: opts.Client,
namespace: opts.Namespace,
host: opts.Host,
}
if opts.BearerToken != nil {
req.SetHeader("Authorization", "Bearer "+*opts.BearerToken)
}
return req
} | [
"func",
"NewRequest",
"(",
"opts",
"*",
"Options",
")",
"*",
"Request",
"{",
"req",
":=",
"&",
"Request",
"{",
"header",
":",
"make",
"(",
"http",
".",
"Header",
")",
",",
"params",
":",
"make",
"(",
"url",
".",
"Values",
")",
",",
"client",
":",
"opts",
".",
"Client",
",",
"namespace",
":",
"opts",
".",
"Namespace",
",",
"host",
":",
"opts",
".",
"Host",
",",
"}",
"\n",
"if",
"opts",
".",
"BearerToken",
"!=",
"nil",
"{",
"req",
".",
"SetHeader",
"(",
"\"Authorization\"",
",",
"\"Bearer \"",
"+",
"*",
"opts",
".",
"BearerToken",
")",
"\n",
"}",
"\n",
"return",
"req",
"\n",
"}"
] | // NewRequest creates a k8s api request | [
"NewRequest",
"creates",
"a",
"k8s",
"api",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/api/request.go#L194-L208 | train |
micro/go-plugins | broker/stomp/stomp.go | stompHeaderToMap | 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
} | go | 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
} | [
"func",
"stompHeaderToMap",
"(",
"h",
"*",
"frame",
".",
"Header",
")",
"map",
"[",
"string",
"]",
"string",
"{",
"m",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"h",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"k",
",",
"v",
":=",
"h",
".",
"GetAt",
"(",
"i",
")",
"\n",
"m",
"[",
"k",
"]",
"=",
"v",
"\n",
"}",
"\n",
"return",
"m",
"\n",
"}"
] | // stompHeaderToMap converts STOMP header to broker friendly header | [
"stompHeaderToMap",
"converts",
"STOMP",
"header",
"to",
"broker",
"friendly",
"header"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/stomp.go#L29-L36 | train |
micro/go-plugins | broker/stomp/stomp.go | defaults | func (r *rbroker) defaults() {
ConnectTimeout(30 * time.Second)(&r.opts)
VirtualHost("/")(&r.opts)
} | go | func (r *rbroker) defaults() {
ConnectTimeout(30 * time.Second)(&r.opts)
VirtualHost("/")(&r.opts)
} | [
"func",
"(",
"r",
"*",
"rbroker",
")",
"defaults",
"(",
")",
"{",
"ConnectTimeout",
"(",
"30",
"*",
"time",
".",
"Second",
")",
"(",
"&",
"r",
".",
"opts",
")",
"\n",
"VirtualHost",
"(",
"\"/\"",
")",
"(",
"&",
"r",
".",
"opts",
")",
"\n",
"}"
] | // defaults sets 'sane' STOMP default | [
"defaults",
"sets",
"sane",
"STOMP",
"default"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/stomp.go#L39-L42 | train |
micro/go-plugins | broker/stomp/stomp.go | NewBroker | func NewBroker(opts ...broker.Option) broker.Broker {
r := &rbroker{
opts: broker.Options{
Context: context.Background(),
},
}
r.Init(opts...)
return r
} | go | func NewBroker(opts ...broker.Option) broker.Broker {
r := &rbroker{
opts: broker.Options{
Context: context.Background(),
},
}
r.Init(opts...)
return r
} | [
"func",
"NewBroker",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"broker",
".",
"Broker",
"{",
"r",
":=",
"&",
"rbroker",
"{",
"opts",
":",
"broker",
".",
"Options",
"{",
"Context",
":",
"context",
".",
"Background",
"(",
")",
",",
"}",
",",
"}",
"\n",
"r",
".",
"Init",
"(",
"opts",
"...",
")",
"\n",
"return",
"r",
"\n",
"}"
] | // NewBroker returns a STOMP broker | [
"NewBroker",
"returns",
"a",
"STOMP",
"broker"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/stomp/stomp.go#L236-L244 | train |
micro/go-plugins | wrapper/trace/datadog/tracker.go | finishWithError | func (t *tracker) finishWithError(err error, noDebugStack bool) {
if t.span == nil {
return
}
statusCode := codes.OK
finishOptions := []tracer.FinishOption{
tracer.FinishTime(time.Now()),
}
microErr, ok := err.(*microerr.Error)
if ok {
finishOptions = append(finishOptions, tracer.WithError(
fmt.Errorf("%s: %s", microErr.Id, microErr.Detail),
))
c, ok := microCodeToStatusCode[microErr.Code]
if ok {
statusCode = c
} else {
statusCode = codes.Unknown
}
}
t.span.SetTag(tagStatus, statusCode.String())
if noDebugStack {
finishOptions = append(finishOptions, tracer.NoDebugStack())
}
t.span.Finish(finishOptions...)
t.span = nil
} | go | func (t *tracker) finishWithError(err error, noDebugStack bool) {
if t.span == nil {
return
}
statusCode := codes.OK
finishOptions := []tracer.FinishOption{
tracer.FinishTime(time.Now()),
}
microErr, ok := err.(*microerr.Error)
if ok {
finishOptions = append(finishOptions, tracer.WithError(
fmt.Errorf("%s: %s", microErr.Id, microErr.Detail),
))
c, ok := microCodeToStatusCode[microErr.Code]
if ok {
statusCode = c
} else {
statusCode = codes.Unknown
}
}
t.span.SetTag(tagStatus, statusCode.String())
if noDebugStack {
finishOptions = append(finishOptions, tracer.NoDebugStack())
}
t.span.Finish(finishOptions...)
t.span = nil
} | [
"func",
"(",
"t",
"*",
"tracker",
")",
"finishWithError",
"(",
"err",
"error",
",",
"noDebugStack",
"bool",
")",
"{",
"if",
"t",
".",
"span",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"statusCode",
":=",
"codes",
".",
"OK",
"\n",
"finishOptions",
":=",
"[",
"]",
"tracer",
".",
"FinishOption",
"{",
"tracer",
".",
"FinishTime",
"(",
"time",
".",
"Now",
"(",
")",
")",
",",
"}",
"\n",
"microErr",
",",
"ok",
":=",
"err",
".",
"(",
"*",
"microerr",
".",
"Error",
")",
"\n",
"if",
"ok",
"{",
"finishOptions",
"=",
"append",
"(",
"finishOptions",
",",
"tracer",
".",
"WithError",
"(",
"fmt",
".",
"Errorf",
"(",
"\"%s: %s\"",
",",
"microErr",
".",
"Id",
",",
"microErr",
".",
"Detail",
")",
",",
")",
")",
"\n",
"c",
",",
"ok",
":=",
"microCodeToStatusCode",
"[",
"microErr",
".",
"Code",
"]",
"\n",
"if",
"ok",
"{",
"statusCode",
"=",
"c",
"\n",
"}",
"else",
"{",
"statusCode",
"=",
"codes",
".",
"Unknown",
"\n",
"}",
"\n",
"}",
"\n",
"t",
".",
"span",
".",
"SetTag",
"(",
"tagStatus",
",",
"statusCode",
".",
"String",
"(",
")",
")",
"\n",
"if",
"noDebugStack",
"{",
"finishOptions",
"=",
"append",
"(",
"finishOptions",
",",
"tracer",
".",
"NoDebugStack",
"(",
")",
")",
"\n",
"}",
"\n",
"t",
".",
"span",
".",
"Finish",
"(",
"finishOptions",
"...",
")",
"\n",
"t",
".",
"span",
"=",
"nil",
"\n",
"}"
] | // finishWithError end a request's monitoring session. If there is a span ongoing, it will
// be ended. | [
"finishWithError",
"end",
"a",
"request",
"s",
"monitoring",
"session",
".",
"If",
"there",
"is",
"a",
"span",
"ongoing",
"it",
"will",
"be",
"ended",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/datadog/tracker.go#L72-L105 | train |
micro/go-plugins | client/grpc/options.go | AuthTLS | func AuthTLS(t *tls.Config) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, tlsAuth{}, t)
}
} | go | func AuthTLS(t *tls.Config) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, tlsAuth{}, t)
}
} | [
"func",
"AuthTLS",
"(",
"t",
"*",
"tls",
".",
"Config",
")",
"client",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"client",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"tlsAuth",
"{",
"}",
",",
"t",
")",
"\n",
"}",
"\n",
"}"
] | // AuthTLS should be used to setup a secure authentication using TLS | [
"AuthTLS",
"should",
"be",
"used",
"to",
"setup",
"a",
"secure",
"authentication",
"using",
"TLS"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/client/grpc/options.go#L43-L50 | train |
micro/go-plugins | client/grpc/options.go | MaxRecvMsgSize | func MaxRecvMsgSize(s int) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s)
}
} | go | func MaxRecvMsgSize(s int) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s)
}
} | [
"func",
"MaxRecvMsgSize",
"(",
"s",
"int",
")",
"client",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"client",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"maxRecvMsgSizeKey",
"{",
"}",
",",
"s",
")",
"\n",
"}",
"\n",
"}"
] | //
// MaxRecvMsgSize set the maximum size of message that client can receive.
// | [
"MaxRecvMsgSize",
"set",
"the",
"maximum",
"size",
"of",
"message",
"that",
"client",
"can",
"receive",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/client/grpc/options.go#L55-L62 | train |
micro/go-plugins | client/grpc/options.go | MaxSendMsgSize | func MaxSendMsgSize(s int) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s)
}
} | go | func MaxSendMsgSize(s int) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s)
}
} | [
"func",
"MaxSendMsgSize",
"(",
"s",
"int",
")",
"client",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"client",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"maxSendMsgSizeKey",
"{",
"}",
",",
"s",
")",
"\n",
"}",
"\n",
"}"
] | //
// MaxSendMsgSize set the maximum size of message that client can send.
// | [
"MaxSendMsgSize",
"set",
"the",
"maximum",
"size",
"of",
"message",
"that",
"client",
"can",
"send",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/client/grpc/options.go#L67-L74 | train |
micro/go-plugins | broker/sqs/options.go | DeduplicationFunction | func DeduplicationFunction(dedup StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, dedupFunctionKey{}, dedup)
}
} | go | func DeduplicationFunction(dedup StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, dedupFunctionKey{}, dedup)
}
} | [
"func",
"DeduplicationFunction",
"(",
"dedup",
"StringFromMessageFunc",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"dedupFunctionKey",
"{",
"}",
",",
"dedup",
")",
"\n",
"}",
"\n",
"}"
] | // DeduplicationFunction sets the function used to create the deduplication string
// for a given message | [
"DeduplicationFunction",
"sets",
"the",
"function",
"used",
"to",
"create",
"the",
"deduplication",
"string",
"for",
"a",
"given",
"message"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L21-L28 | train |
micro/go-plugins | broker/sqs/options.go | GroupIDFunction | func GroupIDFunction(groupfunc StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, groupIdFunctionKey{}, groupfunc)
}
} | go | func GroupIDFunction(groupfunc StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, groupIdFunctionKey{}, groupfunc)
}
} | [
"func",
"GroupIDFunction",
"(",
"groupfunc",
"StringFromMessageFunc",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"groupIdFunctionKey",
"{",
"}",
",",
"groupfunc",
")",
"\n",
"}",
"\n",
"}"
] | // GroupIDFunction sets the function used to create the group ID string for a
// given message | [
"GroupIDFunction",
"sets",
"the",
"function",
"used",
"to",
"create",
"the",
"group",
"ID",
"string",
"for",
"a",
"given",
"message"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L32-L39 | train |
micro/go-plugins | broker/sqs/options.go | MaxReceiveMessages | func MaxReceiveMessages(max int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxMessagesKey{}, max)
}
} | go | func MaxReceiveMessages(max int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxMessagesKey{}, max)
}
} | [
"func",
"MaxReceiveMessages",
"(",
"max",
"int64",
")",
"broker",
".",
"SubscribeOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"SubscribeOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"maxMessagesKey",
"{",
"}",
",",
"max",
")",
"\n",
"}",
"\n",
"}"
] | // MaxReceiveMessages indicates how many messages a receive operation should pull
// during any single call | [
"MaxReceiveMessages",
"indicates",
"how",
"many",
"messages",
"a",
"receive",
"operation",
"should",
"pull",
"during",
"any",
"single",
"call"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L43-L50 | train |
micro/go-plugins | broker/sqs/options.go | VisibilityTimeout | func VisibilityTimeout(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, visiblityTimeoutKey{}, seconds)
}
} | go | func VisibilityTimeout(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, visiblityTimeoutKey{}, seconds)
}
} | [
"func",
"VisibilityTimeout",
"(",
"seconds",
"int64",
")",
"broker",
".",
"SubscribeOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"SubscribeOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"visiblityTimeoutKey",
"{",
"}",
",",
"seconds",
")",
"\n",
"}",
"\n",
"}"
] | // VisibilityTimeout controls how long a message is hidden from other queue consumers
// before being put back. If a consumer does not delete the message, it will be put back
// even if it was "processed" | [
"VisibilityTimeout",
"controls",
"how",
"long",
"a",
"message",
"is",
"hidden",
"from",
"other",
"queue",
"consumers",
"before",
"being",
"put",
"back",
".",
"If",
"a",
"consumer",
"does",
"not",
"delete",
"the",
"message",
"it",
"will",
"be",
"put",
"back",
"even",
"if",
"it",
"was",
"processed"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L55-L62 | train |
micro/go-plugins | broker/sqs/options.go | WaitTimeSeconds | func WaitTimeSeconds(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, waitTimeSecondsKey{}, seconds)
}
} | go | func WaitTimeSeconds(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, waitTimeSecondsKey{}, seconds)
}
} | [
"func",
"WaitTimeSeconds",
"(",
"seconds",
"int64",
")",
"broker",
".",
"SubscribeOption",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"SubscribeOptions",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"waitTimeSecondsKey",
"{",
"}",
",",
"seconds",
")",
"\n",
"}",
"\n",
"}"
] | // WaitTimeSeconds controls the length of long polling for available messages | [
"WaitTimeSeconds",
"controls",
"the",
"length",
"of",
"long",
"polling",
"for",
"available",
"messages"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L65-L72 | train |
micro/go-plugins | broker/sqs/options.go | Client | func Client(c *sqs.SQS) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, sqsClientKey{}, c)
}
} | go | func Client(c *sqs.SQS) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, sqsClientKey{}, c)
}
} | [
"func",
"Client",
"(",
"c",
"*",
"sqs",
".",
"SQS",
")",
"broker",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"broker",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"sqsClientKey",
"{",
"}",
",",
"c",
")",
"\n",
"}",
"\n",
"}"
] | // Client receives an instantiated instance of an SQS client which is used instead of initialising a new client | [
"Client",
"receives",
"an",
"instantiated",
"instance",
"of",
"an",
"SQS",
"client",
"which",
"is",
"used",
"instead",
"of",
"initialising",
"a",
"new",
"client"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/sqs/options.go#L75-L82 | train |
micro/go-plugins | wrapper/ratelimiter/uber/uber.go | NewClientWrapper | func NewClientWrapper(rate int, opts ...ratelimit.Option) client.Wrapper {
r := ratelimit.New(rate, opts...)
return func(c client.Client) client.Client {
return &clientWrapper{r, c}
}
} | go | func NewClientWrapper(rate int, opts ...ratelimit.Option) client.Wrapper {
r := ratelimit.New(rate, opts...)
return func(c client.Client) client.Client {
return &clientWrapper{r, c}
}
} | [
"func",
"NewClientWrapper",
"(",
"rate",
"int",
",",
"opts",
"...",
"ratelimit",
".",
"Option",
")",
"client",
".",
"Wrapper",
"{",
"r",
":=",
"ratelimit",
".",
"New",
"(",
"rate",
",",
"opts",
"...",
")",
"\n",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"clientWrapper",
"{",
"r",
",",
"c",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper creates a blocking side rate limiter | [
"NewClientWrapper",
"creates",
"a",
"blocking",
"side",
"rate",
"limiter"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/ratelimiter/uber/uber.go#L22-L28 | train |
micro/go-plugins | wrapper/ratelimiter/uber/uber.go | NewHandlerWrapper | func NewHandlerWrapper(rate int, opts ...ratelimit.Option) server.HandlerWrapper {
r := ratelimit.New(rate, opts...)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
r.Take()
return h(ctx, req, rsp)
}
}
} | go | func NewHandlerWrapper(rate int, opts ...ratelimit.Option) server.HandlerWrapper {
r := ratelimit.New(rate, opts...)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
r.Take()
return h(ctx, req, rsp)
}
}
} | [
"func",
"NewHandlerWrapper",
"(",
"rate",
"int",
",",
"opts",
"...",
"ratelimit",
".",
"Option",
")",
"server",
".",
"HandlerWrapper",
"{",
"r",
":=",
"ratelimit",
".",
"New",
"(",
"rate",
",",
"opts",
"...",
")",
"\n",
"return",
"func",
"(",
"h",
"server",
".",
"HandlerFunc",
")",
"server",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"server",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
")",
"error",
"{",
"r",
".",
"Take",
"(",
")",
"\n",
"return",
"h",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewHandlerWrapper creates a blocking server side rate limiter | [
"NewHandlerWrapper",
"creates",
"a",
"blocking",
"server",
"side",
"rate",
"limiter"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/ratelimiter/uber/uber.go#L31-L40 | train |
micro/go-plugins | wrapper/select/roundrobin/roundrobin.go | NewClientWrapper | func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
return &roundrobin{
rr: make(map[string]int),
Client: c,
}
}
} | go | func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
return &roundrobin{
rr: make(map[string]int),
Client: c,
}
}
} | [
"func",
"NewClientWrapper",
"(",
")",
"client",
".",
"Wrapper",
"{",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"return",
"&",
"roundrobin",
"{",
"rr",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"int",
")",
",",
"Client",
":",
"c",
",",
"}",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper is a wrapper which roundrobins requests | [
"NewClientWrapper",
"is",
"a",
"wrapper",
"which",
"roundrobins",
"requests"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/select/roundrobin/roundrobin.go#L55-L62 | train |
micro/go-plugins | micro/trace/awsxray/util.go | complete | func complete(s *awsxray.Segment, status int) {
switch {
case status >= 500:
s.Fault = true
case status >= 400:
s.Error = true
}
s.HTTP.Response.Status = status
s.EndTime = float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9
} | go | func complete(s *awsxray.Segment, status int) {
switch {
case status >= 500:
s.Fault = true
case status >= 400:
s.Error = true
}
s.HTTP.Response.Status = status
s.EndTime = float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9
} | [
"func",
"complete",
"(",
"s",
"*",
"awsxray",
".",
"Segment",
",",
"status",
"int",
")",
"{",
"switch",
"{",
"case",
"status",
">=",
"500",
":",
"s",
".",
"Fault",
"=",
"true",
"\n",
"case",
"status",
">=",
"400",
":",
"s",
".",
"Error",
"=",
"true",
"\n",
"}",
"\n",
"s",
".",
"HTTP",
".",
"Response",
".",
"Status",
"=",
"status",
"\n",
"s",
".",
"EndTime",
"=",
"float64",
"(",
"time",
".",
"Now",
"(",
")",
".",
"Truncate",
"(",
"time",
".",
"Millisecond",
")",
".",
"UnixNano",
"(",
")",
")",
"/",
"1e9",
"\n",
"}"
] | // complete sets the response status and end time | [
"complete",
"sets",
"the",
"response",
"status",
"and",
"end",
"time"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/trace/awsxray/util.go#L15-L24 | train |
micro/go-plugins | micro/trace/awsxray/util.go | getIp | func getIp(r *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, ip := range strings.Split(r.Header.Get(h), ",") {
if len(ip) == 0 {
continue
}
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
return realIP.String()
}
}
// not found in header
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// just return remote addr
return r.RemoteAddr
}
return host
} | go | func getIp(r *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, ip := range strings.Split(r.Header.Get(h), ",") {
if len(ip) == 0 {
continue
}
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
return realIP.String()
}
}
// not found in header
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// just return remote addr
return r.RemoteAddr
}
return host
} | [
"func",
"getIp",
"(",
"r",
"*",
"http",
".",
"Request",
")",
"string",
"{",
"for",
"_",
",",
"h",
":=",
"range",
"[",
"]",
"string",
"{",
"\"X-Forwarded-For\"",
",",
"\"X-Real-Ip\"",
"}",
"{",
"for",
"_",
",",
"ip",
":=",
"range",
"strings",
".",
"Split",
"(",
"r",
".",
"Header",
".",
"Get",
"(",
"h",
")",
",",
"\",\"",
")",
"{",
"if",
"len",
"(",
"ip",
")",
"==",
"0",
"{",
"continue",
"\n",
"}",
"\n",
"realIP",
":=",
"net",
".",
"ParseIP",
"(",
"strings",
".",
"Replace",
"(",
"ip",
",",
"\" \"",
",",
"\"\"",
",",
"-",
"1",
")",
")",
"\n",
"return",
"realIP",
".",
"String",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"host",
",",
"_",
",",
"err",
":=",
"net",
".",
"SplitHostPort",
"(",
"r",
".",
"RemoteAddr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"r",
".",
"RemoteAddr",
"\n",
"}",
"\n",
"return",
"host",
"\n",
"}"
] | // getIp naively returns an ip for the request | [
"getIp",
"naively",
"returns",
"an",
"ip",
"for",
"the",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/trace/awsxray/util.go#L27-L46 | train |
micro/go-plugins | micro/trace/awsxray/util.go | newHTTP | func newHTTP(r *http.Request) *awsxray.HTTP {
scheme := "http"
host := r.Host
if len(r.URL.Scheme) > 0 {
scheme = r.URL.Scheme
}
if len(r.URL.Host) > 0 {
host = r.URL.Host
}
return &awsxray.HTTP{
Request: &awsxray.Request{
Method: r.Method,
URL: fmt.Sprintf("%s://%s%s", scheme, host, r.URL.Path),
ClientIP: getIp(r),
UserAgent: r.UserAgent(),
},
Response: &awsxray.Response{
Status: 200,
},
}
} | go | func newHTTP(r *http.Request) *awsxray.HTTP {
scheme := "http"
host := r.Host
if len(r.URL.Scheme) > 0 {
scheme = r.URL.Scheme
}
if len(r.URL.Host) > 0 {
host = r.URL.Host
}
return &awsxray.HTTP{
Request: &awsxray.Request{
Method: r.Method,
URL: fmt.Sprintf("%s://%s%s", scheme, host, r.URL.Path),
ClientIP: getIp(r),
UserAgent: r.UserAgent(),
},
Response: &awsxray.Response{
Status: 200,
},
}
} | [
"func",
"newHTTP",
"(",
"r",
"*",
"http",
".",
"Request",
")",
"*",
"awsxray",
".",
"HTTP",
"{",
"scheme",
":=",
"\"http\"",
"\n",
"host",
":=",
"r",
".",
"Host",
"\n",
"if",
"len",
"(",
"r",
".",
"URL",
".",
"Scheme",
")",
">",
"0",
"{",
"scheme",
"=",
"r",
".",
"URL",
".",
"Scheme",
"\n",
"}",
"\n",
"if",
"len",
"(",
"r",
".",
"URL",
".",
"Host",
")",
">",
"0",
"{",
"host",
"=",
"r",
".",
"URL",
".",
"Host",
"\n",
"}",
"\n",
"return",
"&",
"awsxray",
".",
"HTTP",
"{",
"Request",
":",
"&",
"awsxray",
".",
"Request",
"{",
"Method",
":",
"r",
".",
"Method",
",",
"URL",
":",
"fmt",
".",
"Sprintf",
"(",
"\"%s://%s%s\"",
",",
"scheme",
",",
"host",
",",
"r",
".",
"URL",
".",
"Path",
")",
",",
"ClientIP",
":",
"getIp",
"(",
"r",
")",
",",
"UserAgent",
":",
"r",
".",
"UserAgent",
"(",
")",
",",
"}",
",",
"Response",
":",
"&",
"awsxray",
".",
"Response",
"{",
"Status",
":",
"200",
",",
"}",
",",
"}",
"\n",
"}"
] | // newHTTP returns a http struct | [
"newHTTP",
"returns",
"a",
"http",
"struct"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/trace/awsxray/util.go#L81-L104 | train |
micro/go-plugins | micro/trace/awsxray/util.go | newSegment | func newSegment(name string, r *http.Request) *awsxray.Segment {
// attempt to get IDs first
parentId := getParentId(r.Header)
traceId := getTraceId(r.Header)
// now set the trace ID
traceHdr := r.Header.Get(awsxray.TraceHeader)
traceHdr = awsxray.SetTraceId(traceHdr, traceId)
// create segment
s := &awsxray.Segment{
Id: getRandom(8),
HTTP: newHTTP(r),
Name: name,
TraceId: traceId,
StartTime: float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9,
}
// if we have a parent then we are a subsegment
if len(parentId) > 0 {
s.ParentId = parentId
s.Type = "subsegment"
} else {
// set a new parent Id
traceHdr = awsxray.SetParentId(traceHdr, s.Id)
}
// now save the header for the future context
r.Header.Set(awsxray.TraceHeader, traceHdr)
return s
} | go | func newSegment(name string, r *http.Request) *awsxray.Segment {
// attempt to get IDs first
parentId := getParentId(r.Header)
traceId := getTraceId(r.Header)
// now set the trace ID
traceHdr := r.Header.Get(awsxray.TraceHeader)
traceHdr = awsxray.SetTraceId(traceHdr, traceId)
// create segment
s := &awsxray.Segment{
Id: getRandom(8),
HTTP: newHTTP(r),
Name: name,
TraceId: traceId,
StartTime: float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9,
}
// if we have a parent then we are a subsegment
if len(parentId) > 0 {
s.ParentId = parentId
s.Type = "subsegment"
} else {
// set a new parent Id
traceHdr = awsxray.SetParentId(traceHdr, s.Id)
}
// now save the header for the future context
r.Header.Set(awsxray.TraceHeader, traceHdr)
return s
} | [
"func",
"newSegment",
"(",
"name",
"string",
",",
"r",
"*",
"http",
".",
"Request",
")",
"*",
"awsxray",
".",
"Segment",
"{",
"parentId",
":=",
"getParentId",
"(",
"r",
".",
"Header",
")",
"\n",
"traceId",
":=",
"getTraceId",
"(",
"r",
".",
"Header",
")",
"\n",
"traceHdr",
":=",
"r",
".",
"Header",
".",
"Get",
"(",
"awsxray",
".",
"TraceHeader",
")",
"\n",
"traceHdr",
"=",
"awsxray",
".",
"SetTraceId",
"(",
"traceHdr",
",",
"traceId",
")",
"\n",
"s",
":=",
"&",
"awsxray",
".",
"Segment",
"{",
"Id",
":",
"getRandom",
"(",
"8",
")",
",",
"HTTP",
":",
"newHTTP",
"(",
"r",
")",
",",
"Name",
":",
"name",
",",
"TraceId",
":",
"traceId",
",",
"StartTime",
":",
"float64",
"(",
"time",
".",
"Now",
"(",
")",
".",
"Truncate",
"(",
"time",
".",
"Millisecond",
")",
".",
"UnixNano",
"(",
")",
")",
"/",
"1e9",
",",
"}",
"\n",
"if",
"len",
"(",
"parentId",
")",
">",
"0",
"{",
"s",
".",
"ParentId",
"=",
"parentId",
"\n",
"s",
".",
"Type",
"=",
"\"subsegment\"",
"\n",
"}",
"else",
"{",
"traceHdr",
"=",
"awsxray",
".",
"SetParentId",
"(",
"traceHdr",
",",
"s",
".",
"Id",
")",
"\n",
"}",
"\n",
"r",
".",
"Header",
".",
"Set",
"(",
"awsxray",
".",
"TraceHeader",
",",
"traceHdr",
")",
"\n",
"return",
"s",
"\n",
"}"
] | // newSegment creates a new segment based on whether we're part of an existing flow | [
"newSegment",
"creates",
"a",
"new",
"segment",
"based",
"on",
"whether",
"we",
"re",
"part",
"of",
"an",
"existing",
"flow"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/trace/awsxray/util.go#L107-L138 | train |
micro/go-plugins | wrapper/breaker/gobreaker/gobreaker.go | NewClientWrapper | func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
w := &clientWrapper{}
w.bs = gobreaker.Settings{}
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
w.Client = c
return w
}
} | go | func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
w := &clientWrapper{}
w.bs = gobreaker.Settings{}
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
w.Client = c
return w
}
} | [
"func",
"NewClientWrapper",
"(",
")",
"client",
".",
"Wrapper",
"{",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"w",
":=",
"&",
"clientWrapper",
"{",
"}",
"\n",
"w",
".",
"bs",
"=",
"gobreaker",
".",
"Settings",
"{",
"}",
"\n",
"w",
".",
"cbs",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"gobreaker",
".",
"TwoStepCircuitBreaker",
")",
"\n",
"w",
".",
"Client",
"=",
"c",
"\n",
"return",
"w",
"\n",
"}",
"\n",
"}"
] | // NewClientWrapper returns a client Wrapper. | [
"NewClientWrapper",
"returns",
"a",
"client",
"Wrapper",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/breaker/gobreaker/gobreaker.go#L73-L81 | train |
micro/go-plugins | wrapper/breaker/gobreaker/gobreaker.go | NewCustomClientWrapper | func NewCustomClientWrapper(bs gobreaker.Settings, bm BreakerMethod) client.Wrapper {
return func(c client.Client) client.Client {
w := &clientWrapper{}
w.bm = bm
w.bs = bs
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
w.Client = c
return w
}
} | go | func NewCustomClientWrapper(bs gobreaker.Settings, bm BreakerMethod) client.Wrapper {
return func(c client.Client) client.Client {
w := &clientWrapper{}
w.bm = bm
w.bs = bs
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
w.Client = c
return w
}
} | [
"func",
"NewCustomClientWrapper",
"(",
"bs",
"gobreaker",
".",
"Settings",
",",
"bm",
"BreakerMethod",
")",
"client",
".",
"Wrapper",
"{",
"return",
"func",
"(",
"c",
"client",
".",
"Client",
")",
"client",
".",
"Client",
"{",
"w",
":=",
"&",
"clientWrapper",
"{",
"}",
"\n",
"w",
".",
"bm",
"=",
"bm",
"\n",
"w",
".",
"bs",
"=",
"bs",
"\n",
"w",
".",
"cbs",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"gobreaker",
".",
"TwoStepCircuitBreaker",
")",
"\n",
"w",
".",
"Client",
"=",
"c",
"\n",
"return",
"w",
"\n",
"}",
"\n",
"}"
] | // NewCustomClientWrapper takes a gobreaker.Settings and BreakerMethod. Returns a client Wrapper. | [
"NewCustomClientWrapper",
"takes",
"a",
"gobreaker",
".",
"Settings",
"and",
"BreakerMethod",
".",
"Returns",
"a",
"client",
"Wrapper",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/breaker/gobreaker/gobreaker.go#L84-L93 | train |
micro/go-plugins | wrapper/trace/opencensus/opencensus.go | Call | func (w *clientWrapper) Call(
ctx context.Context,
req client.Request,
rsp interface{},
opts ...client.CallOption) (err error) {
t := newRequestTracker(req, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Call(ctx, req, rsp, opts...)
return
} | go | func (w *clientWrapper) Call(
ctx context.Context,
req client.Request,
rsp interface{},
opts ...client.CallOption) (err error) {
t := newRequestTracker(req, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Call(ctx, req, rsp, opts...)
return
} | [
"func",
"(",
"w",
"*",
"clientWrapper",
")",
"Call",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"client",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
",",
"opts",
"...",
"client",
".",
"CallOption",
")",
"(",
"err",
"error",
")",
"{",
"t",
":=",
"newRequestTracker",
"(",
"req",
",",
"ClientProfile",
")",
"\n",
"ctx",
"=",
"t",
".",
"start",
"(",
"ctx",
",",
"true",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"t",
".",
"end",
"(",
"ctx",
",",
"err",
")",
"}",
"(",
")",
"\n",
"ctx",
"=",
"injectTraceIntoCtx",
"(",
"ctx",
",",
"t",
".",
"span",
")",
"\n",
"err",
"=",
"w",
".",
"Client",
".",
"Call",
"(",
"ctx",
",",
"req",
",",
"rsp",
",",
"opts",
"...",
")",
"\n",
"return",
"\n",
"}"
] | // Call implements client.Client.Call. | [
"Call",
"implements",
"client",
".",
"Client",
".",
"Call",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/opencensus/opencensus.go#L42-L56 | train |
micro/go-plugins | wrapper/trace/opencensus/opencensus.go | Publish | func (w *clientWrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) (err error) {
t := newPublicationTracker(p, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Publish(ctx, p, opts...)
return
} | go | func (w *clientWrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) (err error) {
t := newPublicationTracker(p, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Publish(ctx, p, opts...)
return
} | [
"func",
"(",
"w",
"*",
"clientWrapper",
")",
"Publish",
"(",
"ctx",
"context",
".",
"Context",
",",
"p",
"client",
".",
"Message",
",",
"opts",
"...",
"client",
".",
"PublishOption",
")",
"(",
"err",
"error",
")",
"{",
"t",
":=",
"newPublicationTracker",
"(",
"p",
",",
"ClientProfile",
")",
"\n",
"ctx",
"=",
"t",
".",
"start",
"(",
"ctx",
",",
"true",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"t",
".",
"end",
"(",
"ctx",
",",
"err",
")",
"}",
"(",
")",
"\n",
"ctx",
"=",
"injectTraceIntoCtx",
"(",
"ctx",
",",
"t",
".",
"span",
")",
"\n",
"err",
"=",
"w",
".",
"Client",
".",
"Publish",
"(",
"ctx",
",",
"p",
",",
"opts",
"...",
")",
"\n",
"return",
"\n",
"}"
] | // Publish implements client.Client.Publish. | [
"Publish",
"implements",
"client",
".",
"Client",
".",
"Publish",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/opencensus/opencensus.go#L59-L69 | train |
micro/go-plugins | wrapper/trace/opencensus/opencensus.go | NewHandlerWrapper | func NewHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
t := newRequestTracker(req, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Endpoint()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Endpoint()),
)
}
err = fn(ctx, req, rsp)
return
}
}
} | go | func NewHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
t := newRequestTracker(req, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Endpoint()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Endpoint()),
)
}
err = fn(ctx, req, rsp)
return
}
}
} | [
"func",
"NewHandlerWrapper",
"(",
")",
"server",
".",
"HandlerWrapper",
"{",
"return",
"func",
"(",
"fn",
"server",
".",
"HandlerFunc",
")",
"server",
".",
"HandlerFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"server",
".",
"Request",
",",
"rsp",
"interface",
"{",
"}",
")",
"(",
"err",
"error",
")",
"{",
"t",
":=",
"newRequestTracker",
"(",
"req",
",",
"ServerProfile",
")",
"\n",
"ctx",
"=",
"t",
".",
"start",
"(",
"ctx",
",",
"false",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"t",
".",
"end",
"(",
"ctx",
",",
"err",
")",
"}",
"(",
")",
"\n",
"spanCtx",
":=",
"getTraceFromCtx",
"(",
"ctx",
")",
"\n",
"if",
"spanCtx",
"!=",
"nil",
"{",
"ctx",
",",
"t",
".",
"span",
"=",
"trace",
".",
"StartSpanWithRemoteParent",
"(",
"ctx",
",",
"fmt",
".",
"Sprintf",
"(",
"\"rpc/%s/%s/%s\"",
",",
"ServerProfile",
".",
"Role",
",",
"req",
".",
"Service",
"(",
")",
",",
"req",
".",
"Endpoint",
"(",
")",
")",
",",
"*",
"spanCtx",
",",
")",
"\n",
"}",
"else",
"{",
"ctx",
",",
"t",
".",
"span",
"=",
"trace",
".",
"StartSpan",
"(",
"ctx",
",",
"fmt",
".",
"Sprintf",
"(",
"\"rpc/%s/%s/%s\"",
",",
"ServerProfile",
".",
"Role",
",",
"req",
".",
"Service",
"(",
")",
",",
"req",
".",
"Endpoint",
"(",
")",
")",
",",
")",
"\n",
"}",
"\n",
"err",
"=",
"fn",
"(",
"ctx",
",",
"req",
",",
"rsp",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewHandlerWrapper returns a server.HandlerWrapper
// that adds tracing to incoming requests. | [
"NewHandlerWrapper",
"returns",
"a",
"server",
".",
"HandlerWrapper",
"that",
"adds",
"tracing",
"to",
"incoming",
"requests",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/opencensus/opencensus.go#L107-L133 | train |
micro/go-plugins | wrapper/trace/opencensus/opencensus.go | NewSubscriberWrapper | func NewSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, p server.Message) (err error) {
t := newPublicationTracker(p, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
)
}
err = fn(ctx, p)
return
}
}
} | go | func NewSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, p server.Message) (err error) {
t := newPublicationTracker(p, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
)
}
err = fn(ctx, p)
return
}
}
} | [
"func",
"NewSubscriberWrapper",
"(",
")",
"server",
".",
"SubscriberWrapper",
"{",
"return",
"func",
"(",
"fn",
"server",
".",
"SubscriberFunc",
")",
"server",
".",
"SubscriberFunc",
"{",
"return",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"p",
"server",
".",
"Message",
")",
"(",
"err",
"error",
")",
"{",
"t",
":=",
"newPublicationTracker",
"(",
"p",
",",
"ServerProfile",
")",
"\n",
"ctx",
"=",
"t",
".",
"start",
"(",
"ctx",
",",
"false",
")",
"\n",
"defer",
"func",
"(",
")",
"{",
"t",
".",
"end",
"(",
"ctx",
",",
"err",
")",
"}",
"(",
")",
"\n",
"spanCtx",
":=",
"getTraceFromCtx",
"(",
"ctx",
")",
"\n",
"if",
"spanCtx",
"!=",
"nil",
"{",
"ctx",
",",
"t",
".",
"span",
"=",
"trace",
".",
"StartSpanWithRemoteParent",
"(",
"ctx",
",",
"fmt",
".",
"Sprintf",
"(",
"\"rpc/%s/pubsub/%s\"",
",",
"ServerProfile",
".",
"Role",
",",
"p",
".",
"Topic",
"(",
")",
")",
",",
"*",
"spanCtx",
",",
")",
"\n",
"}",
"else",
"{",
"ctx",
",",
"t",
".",
"span",
"=",
"trace",
".",
"StartSpan",
"(",
"ctx",
",",
"fmt",
".",
"Sprintf",
"(",
"\"rpc/%s/pubsub/%s\"",
",",
"ServerProfile",
".",
"Role",
",",
"p",
".",
"Topic",
"(",
")",
")",
",",
")",
"\n",
"}",
"\n",
"err",
"=",
"fn",
"(",
"ctx",
",",
"p",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // NewSubscriberWrapper returns a server.SubscriberWrapper
// that adds tracing to subscription requests. | [
"NewSubscriberWrapper",
"returns",
"a",
"server",
".",
"SubscriberWrapper",
"that",
"adds",
"tracing",
"to",
"subscription",
"requests",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/wrapper/trace/opencensus/opencensus.go#L137-L163 | train |
micro/go-plugins | broker/googlepubsub/googlepubsub.go | Publish | func (b *pubsubBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
t := b.client.Topic(topic)
ctx := context.Background()
exists, err := t.Exists(ctx)
if err != nil {
return err
}
if !exists {
tt, err := b.client.CreateTopic(ctx, topic)
if err != nil {
return err
}
t = tt
}
m := &pubsub.Message{
ID: "m-" + uuid.New().String(),
Data: msg.Body,
Attributes: msg.Header,
}
pr := t.Publish(ctx, m)
_, err = pr.Get(ctx)
return err
} | go | func (b *pubsubBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
t := b.client.Topic(topic)
ctx := context.Background()
exists, err := t.Exists(ctx)
if err != nil {
return err
}
if !exists {
tt, err := b.client.CreateTopic(ctx, topic)
if err != nil {
return err
}
t = tt
}
m := &pubsub.Message{
ID: "m-" + uuid.New().String(),
Data: msg.Body,
Attributes: msg.Header,
}
pr := t.Publish(ctx, m)
_, err = pr.Get(ctx)
return err
} | [
"func",
"(",
"b",
"*",
"pubsubBroker",
")",
"Publish",
"(",
"topic",
"string",
",",
"msg",
"*",
"broker",
".",
"Message",
",",
"opts",
"...",
"broker",
".",
"PublishOption",
")",
"error",
"{",
"t",
":=",
"b",
".",
"client",
".",
"Topic",
"(",
"topic",
")",
"\n",
"ctx",
":=",
"context",
".",
"Background",
"(",
")",
"\n",
"exists",
",",
"err",
":=",
"t",
".",
"Exists",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"!",
"exists",
"{",
"tt",
",",
"err",
":=",
"b",
".",
"client",
".",
"CreateTopic",
"(",
"ctx",
",",
"topic",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"t",
"=",
"tt",
"\n",
"}",
"\n",
"m",
":=",
"&",
"pubsub",
".",
"Message",
"{",
"ID",
":",
"\"m-\"",
"+",
"uuid",
".",
"New",
"(",
")",
".",
"String",
"(",
")",
",",
"Data",
":",
"msg",
".",
"Body",
",",
"Attributes",
":",
"msg",
".",
"Header",
",",
"}",
"\n",
"pr",
":=",
"t",
".",
"Publish",
"(",
"ctx",
",",
"m",
")",
"\n",
"_",
",",
"err",
"=",
"pr",
".",
"Get",
"(",
"ctx",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Publish checks if the topic exists and then publishes via google pubsub | [
"Publish",
"checks",
"if",
"the",
"topic",
"exists",
"and",
"then",
"publishes",
"via",
"google",
"pubsub"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/googlepubsub.go#L142-L168 | train |
micro/go-plugins | broker/googlepubsub/googlepubsub.go | Subscribe | func (b *pubsubBroker) Subscribe(topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
options := broker.SubscribeOptions{
AutoAck: true,
Queue: "q-" + uuid.New().String(),
Context: b.options.Context,
}
for _, o := range opts {
o(&options)
}
ctx := context.Background()
sub := b.client.Subscription(options.Queue)
if createSubscription, ok := b.options.Context.Value(createSubscription{}).(bool); !ok || createSubscription {
exists, err := sub.Exists(ctx)
if err != nil {
return nil, err
}
if !exists {
tt := b.client.Topic(topic)
subb, err := b.client.CreateSubscription(ctx, options.Queue, pubsub.SubscriptionConfig{
Topic: tt,
AckDeadline: time.Duration(0),
})
if err != nil {
return nil, err
}
sub = subb
}
}
subscriber := &subscriber{
options: options,
topic: topic,
exit: make(chan bool),
sub: sub,
}
go subscriber.run(h)
return subscriber, nil
} | go | func (b *pubsubBroker) Subscribe(topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
options := broker.SubscribeOptions{
AutoAck: true,
Queue: "q-" + uuid.New().String(),
Context: b.options.Context,
}
for _, o := range opts {
o(&options)
}
ctx := context.Background()
sub := b.client.Subscription(options.Queue)
if createSubscription, ok := b.options.Context.Value(createSubscription{}).(bool); !ok || createSubscription {
exists, err := sub.Exists(ctx)
if err != nil {
return nil, err
}
if !exists {
tt := b.client.Topic(topic)
subb, err := b.client.CreateSubscription(ctx, options.Queue, pubsub.SubscriptionConfig{
Topic: tt,
AckDeadline: time.Duration(0),
})
if err != nil {
return nil, err
}
sub = subb
}
}
subscriber := &subscriber{
options: options,
topic: topic,
exit: make(chan bool),
sub: sub,
}
go subscriber.run(h)
return subscriber, nil
} | [
"func",
"(",
"b",
"*",
"pubsubBroker",
")",
"Subscribe",
"(",
"topic",
"string",
",",
"h",
"broker",
".",
"Handler",
",",
"opts",
"...",
"broker",
".",
"SubscribeOption",
")",
"(",
"broker",
".",
"Subscriber",
",",
"error",
")",
"{",
"options",
":=",
"broker",
".",
"SubscribeOptions",
"{",
"AutoAck",
":",
"true",
",",
"Queue",
":",
"\"q-\"",
"+",
"uuid",
".",
"New",
"(",
")",
".",
"String",
"(",
")",
",",
"Context",
":",
"b",
".",
"options",
".",
"Context",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"ctx",
":=",
"context",
".",
"Background",
"(",
")",
"\n",
"sub",
":=",
"b",
".",
"client",
".",
"Subscription",
"(",
"options",
".",
"Queue",
")",
"\n",
"if",
"createSubscription",
",",
"ok",
":=",
"b",
".",
"options",
".",
"Context",
".",
"Value",
"(",
"createSubscription",
"{",
"}",
")",
".",
"(",
"bool",
")",
";",
"!",
"ok",
"||",
"createSubscription",
"{",
"exists",
",",
"err",
":=",
"sub",
".",
"Exists",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"!",
"exists",
"{",
"tt",
":=",
"b",
".",
"client",
".",
"Topic",
"(",
"topic",
")",
"\n",
"subb",
",",
"err",
":=",
"b",
".",
"client",
".",
"CreateSubscription",
"(",
"ctx",
",",
"options",
".",
"Queue",
",",
"pubsub",
".",
"SubscriptionConfig",
"{",
"Topic",
":",
"tt",
",",
"AckDeadline",
":",
"time",
".",
"Duration",
"(",
"0",
")",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"sub",
"=",
"subb",
"\n",
"}",
"\n",
"}",
"\n",
"subscriber",
":=",
"&",
"subscriber",
"{",
"options",
":",
"options",
",",
"topic",
":",
"topic",
",",
"exit",
":",
"make",
"(",
"chan",
"bool",
")",
",",
"sub",
":",
"sub",
",",
"}",
"\n",
"go",
"subscriber",
".",
"run",
"(",
"h",
")",
"\n",
"return",
"subscriber",
",",
"nil",
"\n",
"}"
] | // Subscribe registers a subscription to the given topic against the google pubsub api | [
"Subscribe",
"registers",
"a",
"subscription",
"to",
"the",
"given",
"topic",
"against",
"the",
"google",
"pubsub",
"api"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/googlepubsub.go#L171-L214 | train |
micro/go-plugins | broker/googlepubsub/googlepubsub.go | NewBroker | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
// retrieve project id
prjID, _ := options.Context.Value(projectIDKey{}).(string)
// retrieve client opts
cOpts, _ := options.Context.Value(clientOptionKey{}).([]option.ClientOption)
// create pubsub client
c, err := pubsub.NewClient(context.Background(), prjID, cOpts...)
if err != nil {
panic(err.Error())
}
return &pubsubBroker{
client: c,
options: options,
}
} | go | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
// retrieve project id
prjID, _ := options.Context.Value(projectIDKey{}).(string)
// retrieve client opts
cOpts, _ := options.Context.Value(clientOptionKey{}).([]option.ClientOption)
// create pubsub client
c, err := pubsub.NewClient(context.Background(), prjID, cOpts...)
if err != nil {
panic(err.Error())
}
return &pubsubBroker{
client: c,
options: options,
}
} | [
"func",
"NewBroker",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"broker",
".",
"Broker",
"{",
"options",
":=",
"broker",
".",
"Options",
"{",
"Context",
":",
"context",
".",
"Background",
"(",
")",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"prjID",
",",
"_",
":=",
"options",
".",
"Context",
".",
"Value",
"(",
"projectIDKey",
"{",
"}",
")",
".",
"(",
"string",
")",
"\n",
"cOpts",
",",
"_",
":=",
"options",
".",
"Context",
".",
"Value",
"(",
"clientOptionKey",
"{",
"}",
")",
".",
"(",
"[",
"]",
"option",
".",
"ClientOption",
")",
"\n",
"c",
",",
"err",
":=",
"pubsub",
".",
"NewClient",
"(",
"context",
".",
"Background",
"(",
")",
",",
"prjID",
",",
"cOpts",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"return",
"&",
"pubsubBroker",
"{",
"client",
":",
"c",
",",
"options",
":",
"options",
",",
"}",
"\n",
"}"
] | // NewBroker creates a new google pubsub broker | [
"NewBroker",
"creates",
"a",
"new",
"google",
"pubsub",
"broker"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/googlepubsub/googlepubsub.go#L221-L245 | train |
micro/go-plugins | server/grpc/options.go | Options | func Options(opts ...grpc.ServerOption) server.Option {
return func(o *server.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, grpcOptions{}, opts)
}
} | go | func Options(opts ...grpc.ServerOption) server.Option {
return func(o *server.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, grpcOptions{}, opts)
}
} | [
"func",
"Options",
"(",
"opts",
"...",
"grpc",
".",
"ServerOption",
")",
"server",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"server",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"grpcOptions",
"{",
"}",
",",
"opts",
")",
"\n",
"}",
"\n",
"}"
] | // Options to be used to configure gRPC options | [
"Options",
"to",
"be",
"used",
"to",
"configure",
"gRPC",
"options"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/server/grpc/options.go#L48-L55 | train |
micro/go-plugins | server/grpc/options.go | MaxMsgSize | func MaxMsgSize(s int) server.Option {
return func(o *server.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxMsgSizeKey{}, s)
}
} | go | func MaxMsgSize(s int) server.Option {
return func(o *server.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxMsgSizeKey{}, s)
}
} | [
"func",
"MaxMsgSize",
"(",
"s",
"int",
")",
"server",
".",
"Option",
"{",
"return",
"func",
"(",
"o",
"*",
"server",
".",
"Options",
")",
"{",
"if",
"o",
".",
"Context",
"==",
"nil",
"{",
"o",
".",
"Context",
"=",
"context",
".",
"Background",
"(",
")",
"\n",
"}",
"\n",
"o",
".",
"Context",
"=",
"context",
".",
"WithValue",
"(",
"o",
".",
"Context",
",",
"maxMsgSizeKey",
"{",
"}",
",",
"s",
")",
"\n",
"}",
"\n",
"}"
] | //
// MaxMsgSize set the maximum message in bytes the server can receive and
// send. Default maximum message size is 4 MB.
// | [
"MaxMsgSize",
"set",
"the",
"maximum",
"message",
"in",
"bytes",
"the",
"server",
"can",
"receive",
"and",
"send",
".",
"Default",
"maximum",
"message",
"size",
"is",
"4",
"MB",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/server/grpc/options.go#L61-L68 | train |
micro/go-plugins | selector/shard/shard.go | NewSelector | func NewSelector(keys []string) selector.SelectOption {
return selector.WithStrategy(func(services []*registry.Service) selector.Next {
return Next(keys, services)
})
} | go | func NewSelector(keys []string) selector.SelectOption {
return selector.WithStrategy(func(services []*registry.Service) selector.Next {
return Next(keys, services)
})
} | [
"func",
"NewSelector",
"(",
"keys",
"[",
"]",
"string",
")",
"selector",
".",
"SelectOption",
"{",
"return",
"selector",
".",
"WithStrategy",
"(",
"func",
"(",
"services",
"[",
"]",
"*",
"registry",
".",
"Service",
")",
"selector",
".",
"Next",
"{",
"return",
"Next",
"(",
"keys",
",",
"services",
")",
"\n",
"}",
")",
"\n",
"}"
] | // NewSelector returns a `SelectOption` that directs all request according to the given `keys`. | [
"NewSelector",
"returns",
"a",
"SelectOption",
"that",
"directs",
"all",
"request",
"according",
"to",
"the",
"given",
"keys",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/selector/shard/shard.go#L27-L31 | train |
micro/go-plugins | selector/shard/shard.go | Next | func Next(keys []string, services []*registry.Service) selector.Next {
possibleNodes, scores := ScoreNodes(keys, services)
return func() (*registry.Node, error) {
var best uint64
pos := -1
// Find the best scoring node from those available.
for i, score := range scores {
if score >= best && possibleNodes[i] != nil {
best = score
pos = i
}
}
if pos < 0 {
// There was no node found.
return nil, selector.ErrNoneAvailable
}
// Choose this node and set it's score to zero to stop it being selected again.
node := possibleNodes[pos]
possibleNodes[pos] = nil
scores[pos] = 0
return node, nil
}
} | go | func Next(keys []string, services []*registry.Service) selector.Next {
possibleNodes, scores := ScoreNodes(keys, services)
return func() (*registry.Node, error) {
var best uint64
pos := -1
// Find the best scoring node from those available.
for i, score := range scores {
if score >= best && possibleNodes[i] != nil {
best = score
pos = i
}
}
if pos < 0 {
// There was no node found.
return nil, selector.ErrNoneAvailable
}
// Choose this node and set it's score to zero to stop it being selected again.
node := possibleNodes[pos]
possibleNodes[pos] = nil
scores[pos] = 0
return node, nil
}
} | [
"func",
"Next",
"(",
"keys",
"[",
"]",
"string",
",",
"services",
"[",
"]",
"*",
"registry",
".",
"Service",
")",
"selector",
".",
"Next",
"{",
"possibleNodes",
",",
"scores",
":=",
"ScoreNodes",
"(",
"keys",
",",
"services",
")",
"\n",
"return",
"func",
"(",
")",
"(",
"*",
"registry",
".",
"Node",
",",
"error",
")",
"{",
"var",
"best",
"uint64",
"\n",
"pos",
":=",
"-",
"1",
"\n",
"for",
"i",
",",
"score",
":=",
"range",
"scores",
"{",
"if",
"score",
">=",
"best",
"&&",
"possibleNodes",
"[",
"i",
"]",
"!=",
"nil",
"{",
"best",
"=",
"score",
"\n",
"pos",
"=",
"i",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"pos",
"<",
"0",
"{",
"return",
"nil",
",",
"selector",
".",
"ErrNoneAvailable",
"\n",
"}",
"\n",
"node",
":=",
"possibleNodes",
"[",
"pos",
"]",
"\n",
"possibleNodes",
"[",
"pos",
"]",
"=",
"nil",
"\n",
"scores",
"[",
"pos",
"]",
"=",
"0",
"\n",
"return",
"node",
",",
"nil",
"\n",
"}",
"\n",
"}"
] | // Next returns a `Next` function which returns the next highest scoring node. | [
"Next",
"returns",
"a",
"Next",
"function",
"which",
"returns",
"the",
"next",
"highest",
"scoring",
"node",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/selector/shard/shard.go#L34-L60 | train |
micro/go-plugins | selector/shard/shard.go | ScoreNodes | func ScoreNodes(keys []string, services []*registry.Service) (possibleNodes []*registry.Node, scores []uint64) {
// Generate a base hashing key based off the supplied keys values.
key := highwayhash.Sum([]byte(strings.Join(keys, ":")), zeroKey[:])
// Get all the possible nodes for the services, and assign a hash-based score to each of them.
for _, s := range services {
for _, n := range s.Nodes {
// Use the base key from above to calculate a derivative 64 bit hash number based off the instance ID.
score := highwayhash.Sum64([]byte(n.Id), key[:])
scores = append(scores, score)
possibleNodes = append(possibleNodes, n)
}
}
return
} | go | func ScoreNodes(keys []string, services []*registry.Service) (possibleNodes []*registry.Node, scores []uint64) {
// Generate a base hashing key based off the supplied keys values.
key := highwayhash.Sum([]byte(strings.Join(keys, ":")), zeroKey[:])
// Get all the possible nodes for the services, and assign a hash-based score to each of them.
for _, s := range services {
for _, n := range s.Nodes {
// Use the base key from above to calculate a derivative 64 bit hash number based off the instance ID.
score := highwayhash.Sum64([]byte(n.Id), key[:])
scores = append(scores, score)
possibleNodes = append(possibleNodes, n)
}
}
return
} | [
"func",
"ScoreNodes",
"(",
"keys",
"[",
"]",
"string",
",",
"services",
"[",
"]",
"*",
"registry",
".",
"Service",
")",
"(",
"possibleNodes",
"[",
"]",
"*",
"registry",
".",
"Node",
",",
"scores",
"[",
"]",
"uint64",
")",
"{",
"key",
":=",
"highwayhash",
".",
"Sum",
"(",
"[",
"]",
"byte",
"(",
"strings",
".",
"Join",
"(",
"keys",
",",
"\":\"",
")",
")",
",",
"zeroKey",
"[",
":",
"]",
")",
"\n",
"for",
"_",
",",
"s",
":=",
"range",
"services",
"{",
"for",
"_",
",",
"n",
":=",
"range",
"s",
".",
"Nodes",
"{",
"score",
":=",
"highwayhash",
".",
"Sum64",
"(",
"[",
"]",
"byte",
"(",
"n",
".",
"Id",
")",
",",
"key",
"[",
":",
"]",
")",
"\n",
"scores",
"=",
"append",
"(",
"scores",
",",
"score",
")",
"\n",
"possibleNodes",
"=",
"append",
"(",
"possibleNodes",
",",
"n",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] | // ScoreNodes returns a score for each node found in the given services. | [
"ScoreNodes",
"returns",
"a",
"score",
"for",
"each",
"node",
"found",
"in",
"the",
"given",
"services",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/selector/shard/shard.go#L63-L77 | train |
micro/go-plugins | codec/msgpackrpc/codec.go | ReadHeader | func (c *msgpackCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
c.mt = mt
switch mt {
case codec.Request:
var h Request
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = h.ID
m.Endpoint = h.Method
case codec.Response:
var h Response
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = h.ID
m.Error = h.Error
case codec.Publication:
var h Notification
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Endpoint = h.Method
default:
return errors.New("Unrecognized message type")
}
return nil
} | go | func (c *msgpackCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
c.mt = mt
switch mt {
case codec.Request:
var h Request
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = h.ID
m.Endpoint = h.Method
case codec.Response:
var h Response
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = h.ID
m.Error = h.Error
case codec.Publication:
var h Notification
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Endpoint = h.Method
default:
return errors.New("Unrecognized message type")
}
return nil
} | [
"func",
"(",
"c",
"*",
"msgpackCodec",
")",
"ReadHeader",
"(",
"m",
"*",
"codec",
".",
"Message",
",",
"mt",
"codec",
".",
"MessageType",
")",
"error",
"{",
"c",
".",
"mt",
"=",
"mt",
"\n",
"switch",
"mt",
"{",
"case",
"codec",
".",
"Request",
":",
"var",
"h",
"Request",
"\n",
"if",
"err",
":=",
"msgp",
".",
"Decode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"c",
".",
"body",
"=",
"h",
".",
"hasBody",
"\n",
"m",
".",
"Id",
"=",
"h",
".",
"ID",
"\n",
"m",
".",
"Endpoint",
"=",
"h",
".",
"Method",
"\n",
"case",
"codec",
".",
"Response",
":",
"var",
"h",
"Response",
"\n",
"if",
"err",
":=",
"msgp",
".",
"Decode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"c",
".",
"body",
"=",
"h",
".",
"hasBody",
"\n",
"m",
".",
"Id",
"=",
"h",
".",
"ID",
"\n",
"m",
".",
"Error",
"=",
"h",
".",
"Error",
"\n",
"case",
"codec",
".",
"Publication",
":",
"var",
"h",
"Notification",
"\n",
"if",
"err",
":=",
"msgp",
".",
"Decode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"c",
".",
"body",
"=",
"h",
".",
"hasBody",
"\n",
"m",
".",
"Endpoint",
"=",
"h",
".",
"Method",
"\n",
"default",
":",
"return",
"errors",
".",
"New",
"(",
"\"Unrecognized message type\"",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // ReadHeader reads the header from the wire. | [
"ReadHeader",
"reads",
"the",
"header",
"from",
"the",
"wire",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/msgpackrpc/codec.go#L27-L68 | train |
micro/go-plugins | codec/msgpackrpc/codec.go | ReadBody | func (c *msgpackCodec) ReadBody(v interface{}) error {
if !c.body {
return nil
}
r := msgp.NewReader(c.rwc)
// Body is present, but no value to decode into.
if v == nil {
return r.Skip()
}
switch c.mt {
case codec.Request, codec.Response, codec.Publication:
return decodeBody(r, v)
default:
return fmt.Errorf("Unrecognized message type: %v", c.mt)
}
} | go | func (c *msgpackCodec) ReadBody(v interface{}) error {
if !c.body {
return nil
}
r := msgp.NewReader(c.rwc)
// Body is present, but no value to decode into.
if v == nil {
return r.Skip()
}
switch c.mt {
case codec.Request, codec.Response, codec.Publication:
return decodeBody(r, v)
default:
return fmt.Errorf("Unrecognized message type: %v", c.mt)
}
} | [
"func",
"(",
"c",
"*",
"msgpackCodec",
")",
"ReadBody",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"!",
"c",
".",
"body",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"r",
":=",
"msgp",
".",
"NewReader",
"(",
"c",
".",
"rwc",
")",
"\n",
"if",
"v",
"==",
"nil",
"{",
"return",
"r",
".",
"Skip",
"(",
")",
"\n",
"}",
"\n",
"switch",
"c",
".",
"mt",
"{",
"case",
"codec",
".",
"Request",
",",
"codec",
".",
"Response",
",",
"codec",
".",
"Publication",
":",
"return",
"decodeBody",
"(",
"r",
",",
"v",
")",
"\n",
"default",
":",
"return",
"fmt",
".",
"Errorf",
"(",
"\"Unrecognized message type: %v\"",
",",
"c",
".",
"mt",
")",
"\n",
"}",
"\n",
"}"
] | // ReadBody reads the body of the message. It is assumed the value being
// decoded into is a satisfies the msgp.Decodable interface. | [
"ReadBody",
"reads",
"the",
"body",
"of",
"the",
"message",
".",
"It",
"is",
"assumed",
"the",
"value",
"being",
"decoded",
"into",
"is",
"a",
"satisfies",
"the",
"msgp",
".",
"Decodable",
"interface",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/msgpackrpc/codec.go#L72-L90 | train |
micro/go-plugins | codec/msgpackrpc/codec.go | Write | func (c *msgpackCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type {
case codec.Request:
h := Request{
ID: m.Id,
Method: m.Endpoint,
Body: b,
}
return msgp.Encode(c.rwc, &h)
case codec.Response:
h := Response{
ID: m.Id,
Body: b,
}
h.Error = m.Error
return msgp.Encode(c.rwc, &h)
case codec.Publication:
h := Notification{
Method: m.Endpoint,
Body: b,
}
return msgp.Encode(c.rwc, &h)
default:
return fmt.Errorf("Unrecognized message type: %v", m.Type)
}
} | go | func (c *msgpackCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type {
case codec.Request:
h := Request{
ID: m.Id,
Method: m.Endpoint,
Body: b,
}
return msgp.Encode(c.rwc, &h)
case codec.Response:
h := Response{
ID: m.Id,
Body: b,
}
h.Error = m.Error
return msgp.Encode(c.rwc, &h)
case codec.Publication:
h := Notification{
Method: m.Endpoint,
Body: b,
}
return msgp.Encode(c.rwc, &h)
default:
return fmt.Errorf("Unrecognized message type: %v", m.Type)
}
} | [
"func",
"(",
"c",
"*",
"msgpackCodec",
")",
"Write",
"(",
"m",
"*",
"codec",
".",
"Message",
",",
"b",
"interface",
"{",
"}",
")",
"error",
"{",
"switch",
"m",
".",
"Type",
"{",
"case",
"codec",
".",
"Request",
":",
"h",
":=",
"Request",
"{",
"ID",
":",
"m",
".",
"Id",
",",
"Method",
":",
"m",
".",
"Endpoint",
",",
"Body",
":",
"b",
",",
"}",
"\n",
"return",
"msgp",
".",
"Encode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
"\n",
"case",
"codec",
".",
"Response",
":",
"h",
":=",
"Response",
"{",
"ID",
":",
"m",
".",
"Id",
",",
"Body",
":",
"b",
",",
"}",
"\n",
"h",
".",
"Error",
"=",
"m",
".",
"Error",
"\n",
"return",
"msgp",
".",
"Encode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
"\n",
"case",
"codec",
".",
"Publication",
":",
"h",
":=",
"Notification",
"{",
"Method",
":",
"m",
".",
"Endpoint",
",",
"Body",
":",
"b",
",",
"}",
"\n",
"return",
"msgp",
".",
"Encode",
"(",
"c",
".",
"rwc",
",",
"&",
"h",
")",
"\n",
"default",
":",
"return",
"fmt",
".",
"Errorf",
"(",
"\"Unrecognized message type: %v\"",
",",
"m",
".",
"Type",
")",
"\n",
"}",
"\n",
"}"
] | // Write writes a message to the wire which contains the header followed by the body.
// The body is assumed to satisfy the msgp.Encodable interface. | [
"Write",
"writes",
"a",
"message",
"to",
"the",
"wire",
"which",
"contains",
"the",
"header",
"followed",
"by",
"the",
"body",
".",
"The",
"body",
"is",
"assumed",
"to",
"satisfy",
"the",
"msgp",
".",
"Encodable",
"interface",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/msgpackrpc/codec.go#L94-L126 | train |
micro/go-plugins | codec/jsonrpc2/errors.go | NewError | func NewError(code int, message string) *Error {
return &Error{Code: code, Message: message}
} | go | func NewError(code int, message string) *Error {
return &Error{Code: code, Message: message}
} | [
"func",
"NewError",
"(",
"code",
"int",
",",
"message",
"string",
")",
"*",
"Error",
"{",
"return",
"&",
"Error",
"{",
"Code",
":",
"code",
",",
"Message",
":",
"message",
"}",
"\n",
"}"
] | // NewError returns an Error with given code and message. | [
"NewError",
"returns",
"an",
"Error",
"with",
"given",
"code",
"and",
"message",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/jsonrpc2/errors.go#L28-L30 | train |
micro/go-plugins | codec/jsonrpc2/errors.go | newError | func newError(message string) *Error {
switch {
case strings.HasPrefix(message, "rpc: service/method request ill-formed"):
return NewError(errMethod.Code, message)
case strings.HasPrefix(message, "rpc: can't find service"):
return NewError(errMethod.Code, message)
case strings.HasPrefix(message, "rpc: can't find method"):
return NewError(errMethod.Code, message)
default:
return NewError(errServer.Code, message)
}
} | go | func newError(message string) *Error {
switch {
case strings.HasPrefix(message, "rpc: service/method request ill-formed"):
return NewError(errMethod.Code, message)
case strings.HasPrefix(message, "rpc: can't find service"):
return NewError(errMethod.Code, message)
case strings.HasPrefix(message, "rpc: can't find method"):
return NewError(errMethod.Code, message)
default:
return NewError(errServer.Code, message)
}
} | [
"func",
"newError",
"(",
"message",
"string",
")",
"*",
"Error",
"{",
"switch",
"{",
"case",
"strings",
".",
"HasPrefix",
"(",
"message",
",",
"\"rpc: service/method request ill-formed\"",
")",
":",
"return",
"NewError",
"(",
"errMethod",
".",
"Code",
",",
"message",
")",
"\n",
"case",
"strings",
".",
"HasPrefix",
"(",
"message",
",",
"\"rpc: can't find service\"",
")",
":",
"return",
"NewError",
"(",
"errMethod",
".",
"Code",
",",
"message",
")",
"\n",
"case",
"strings",
".",
"HasPrefix",
"(",
"message",
",",
"\"rpc: can't find method\"",
")",
":",
"return",
"NewError",
"(",
"errMethod",
".",
"Code",
",",
"message",
")",
"\n",
"default",
":",
"return",
"NewError",
"(",
"errServer",
".",
"Code",
",",
"message",
")",
"\n",
"}",
"\n",
"}"
] | // newError returns an Error with auto-detected code for given message. | [
"newError",
"returns",
"an",
"Error",
"with",
"auto",
"-",
"detected",
"code",
"for",
"given",
"message",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/jsonrpc2/errors.go#L33-L44 | train |
micro/go-plugins | codec/jsonrpc2/errors.go | Error | func (e *Error) Error() string {
buf, err := json.Marshal(e)
if err != nil {
msg, err := json.Marshal(err.Error())
if err != nil {
msg = []byte(`"` + errServerError.Message + `"`)
}
return fmt.Sprintf(`{"code":%d,"message":%s}`, errServerError.Code, string(msg))
}
return string(buf)
} | go | func (e *Error) Error() string {
buf, err := json.Marshal(e)
if err != nil {
msg, err := json.Marshal(err.Error())
if err != nil {
msg = []byte(`"` + errServerError.Message + `"`)
}
return fmt.Sprintf(`{"code":%d,"message":%s}`, errServerError.Code, string(msg))
}
return string(buf)
} | [
"func",
"(",
"e",
"*",
"Error",
")",
"Error",
"(",
")",
"string",
"{",
"buf",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"e",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"msg",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"msg",
"=",
"[",
"]",
"byte",
"(",
"`\"`",
"+",
"errServerError",
".",
"Message",
"+",
"`\"`",
")",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"`{\"code\":%d,\"message\":%s}`",
",",
"errServerError",
".",
"Code",
",",
"string",
"(",
"msg",
")",
")",
"\n",
"}",
"\n",
"return",
"string",
"(",
"buf",
")",
"\n",
"}"
] | // Error returns JSON representation of Error. | [
"Error",
"returns",
"JSON",
"representation",
"of",
"Error",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/codec/jsonrpc2/errors.go#L82-L92 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | serviceName | func serviceName(name string) string {
aname := make([]byte, len(name))
for i, r := range []byte(name) {
if !labelRe.Match([]byte{r}) {
aname[i] = '_'
continue
}
aname[i] = r
}
return string(aname)
} | go | func serviceName(name string) string {
aname := make([]byte, len(name))
for i, r := range []byte(name) {
if !labelRe.Match([]byte{r}) {
aname[i] = '_'
continue
}
aname[i] = r
}
return string(aname)
} | [
"func",
"serviceName",
"(",
"name",
"string",
")",
"string",
"{",
"aname",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"len",
"(",
"name",
")",
")",
"\n",
"for",
"i",
",",
"r",
":=",
"range",
"[",
"]",
"byte",
"(",
"name",
")",
"{",
"if",
"!",
"labelRe",
".",
"Match",
"(",
"[",
"]",
"byte",
"{",
"r",
"}",
")",
"{",
"aname",
"[",
"i",
"]",
"=",
"'_'",
"\n",
"continue",
"\n",
"}",
"\n",
"aname",
"[",
"i",
"]",
"=",
"r",
"\n",
"}",
"\n",
"return",
"string",
"(",
"aname",
")",
"\n",
"}"
] | // serviceName generates a valid service name for k8s labels | [
"serviceName",
"generates",
"a",
"valid",
"service",
"name",
"for",
"k8s",
"labels"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L84-L96 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | Init | func (c *kregistry) Init(opts ...registry.Option) error {
return configure(c, opts...)
} | go | func (c *kregistry) Init(opts ...registry.Option) error {
return configure(c, opts...)
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"Init",
"(",
"opts",
"...",
"registry",
".",
"Option",
")",
"error",
"{",
"return",
"configure",
"(",
"c",
",",
"opts",
"...",
")",
"\n",
"}"
] | // Init allows reconfig of options | [
"Init",
"allows",
"reconfig",
"of",
"options"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L99-L101 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | Register | func (c *kregistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
if len(s.Nodes) == 0 {
return errors.New("you must register at least one node")
}
// TODO: grab podname from somewhere better than this.
podName := os.Getenv("HOSTNAME")
svcName := s.Name
// encode micro service
b, err := json.Marshal(s)
if err != nil {
return err
}
svc := string(b)
pod := &client.Pod{
Metadata: &client.Meta{
Labels: map[string]*string{
labelTypeKey: &labelTypeValueService,
svcSelectorPrefix + serviceName(svcName): &svcSelectorValue,
},
Annotations: map[string]*string{
annotationServiceKeyPrefix + serviceName(svcName): &svc,
},
},
}
if _, err := c.client.UpdatePod(podName, pod); err != nil {
return err
}
return nil
} | go | func (c *kregistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
if len(s.Nodes) == 0 {
return errors.New("you must register at least one node")
}
// TODO: grab podname from somewhere better than this.
podName := os.Getenv("HOSTNAME")
svcName := s.Name
// encode micro service
b, err := json.Marshal(s)
if err != nil {
return err
}
svc := string(b)
pod := &client.Pod{
Metadata: &client.Meta{
Labels: map[string]*string{
labelTypeKey: &labelTypeValueService,
svcSelectorPrefix + serviceName(svcName): &svcSelectorValue,
},
Annotations: map[string]*string{
annotationServiceKeyPrefix + serviceName(svcName): &svc,
},
},
}
if _, err := c.client.UpdatePod(podName, pod); err != nil {
return err
}
return nil
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"Register",
"(",
"s",
"*",
"registry",
".",
"Service",
",",
"opts",
"...",
"registry",
".",
"RegisterOption",
")",
"error",
"{",
"if",
"len",
"(",
"s",
".",
"Nodes",
")",
"==",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"you must register at least one node\"",
")",
"\n",
"}",
"\n",
"podName",
":=",
"os",
".",
"Getenv",
"(",
"\"HOSTNAME\"",
")",
"\n",
"svcName",
":=",
"s",
".",
"Name",
"\n",
"b",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"svc",
":=",
"string",
"(",
"b",
")",
"\n",
"pod",
":=",
"&",
"client",
".",
"Pod",
"{",
"Metadata",
":",
"&",
"client",
".",
"Meta",
"{",
"Labels",
":",
"map",
"[",
"string",
"]",
"*",
"string",
"{",
"labelTypeKey",
":",
"&",
"labelTypeValueService",
",",
"svcSelectorPrefix",
"+",
"serviceName",
"(",
"svcName",
")",
":",
"&",
"svcSelectorValue",
",",
"}",
",",
"Annotations",
":",
"map",
"[",
"string",
"]",
"*",
"string",
"{",
"annotationServiceKeyPrefix",
"+",
"serviceName",
"(",
"svcName",
")",
":",
"&",
"svc",
",",
"}",
",",
"}",
",",
"}",
"\n",
"if",
"_",
",",
"err",
":=",
"c",
".",
"client",
".",
"UpdatePod",
"(",
"podName",
",",
"pod",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Register sets a service selector label and an annotation with a
// serialised version of the service passed in. | [
"Register",
"sets",
"a",
"service",
"selector",
"label",
"and",
"an",
"annotation",
"with",
"a",
"serialised",
"version",
"of",
"the",
"service",
"passed",
"in",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L110-L144 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | Deregister | func (c *kregistry) Deregister(s *registry.Service) error {
if len(s.Nodes) == 0 {
return errors.New("you must deregister at least one node")
}
// TODO: grab podname from somewhere better than this.
podName := os.Getenv("HOSTNAME")
svcName := s.Name
pod := &client.Pod{
Metadata: &client.Meta{
Labels: map[string]*string{
svcSelectorPrefix + serviceName(svcName): nil,
},
Annotations: map[string]*string{
annotationServiceKeyPrefix + serviceName(svcName): nil,
},
},
}
if _, err := c.client.UpdatePod(podName, pod); err != nil {
return err
}
return nil
} | go | func (c *kregistry) Deregister(s *registry.Service) error {
if len(s.Nodes) == 0 {
return errors.New("you must deregister at least one node")
}
// TODO: grab podname from somewhere better than this.
podName := os.Getenv("HOSTNAME")
svcName := s.Name
pod := &client.Pod{
Metadata: &client.Meta{
Labels: map[string]*string{
svcSelectorPrefix + serviceName(svcName): nil,
},
Annotations: map[string]*string{
annotationServiceKeyPrefix + serviceName(svcName): nil,
},
},
}
if _, err := c.client.UpdatePod(podName, pod); err != nil {
return err
}
return nil
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"Deregister",
"(",
"s",
"*",
"registry",
".",
"Service",
")",
"error",
"{",
"if",
"len",
"(",
"s",
".",
"Nodes",
")",
"==",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"you must deregister at least one node\"",
")",
"\n",
"}",
"\n",
"podName",
":=",
"os",
".",
"Getenv",
"(",
"\"HOSTNAME\"",
")",
"\n",
"svcName",
":=",
"s",
".",
"Name",
"\n",
"pod",
":=",
"&",
"client",
".",
"Pod",
"{",
"Metadata",
":",
"&",
"client",
".",
"Meta",
"{",
"Labels",
":",
"map",
"[",
"string",
"]",
"*",
"string",
"{",
"svcSelectorPrefix",
"+",
"serviceName",
"(",
"svcName",
")",
":",
"nil",
",",
"}",
",",
"Annotations",
":",
"map",
"[",
"string",
"]",
"*",
"string",
"{",
"annotationServiceKeyPrefix",
"+",
"serviceName",
"(",
"svcName",
")",
":",
"nil",
",",
"}",
",",
"}",
",",
"}",
"\n",
"if",
"_",
",",
"err",
":=",
"c",
".",
"client",
".",
"UpdatePod",
"(",
"podName",
",",
"pod",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Deregister nils out any things set in Register | [
"Deregister",
"nils",
"out",
"any",
"things",
"set",
"in",
"Register"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L147-L173 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | GetService | func (c *kregistry) GetService(name string) ([]*registry.Service, error) {
pods, err := c.client.ListPods(map[string]string{
svcSelectorPrefix + serviceName(name): svcSelectorValue,
})
if err != nil {
return nil, err
}
if len(pods.Items) == 0 {
return nil, registry.ErrNotFound
}
// svcs mapped by version
svcs := make(map[string]*registry.Service)
// loop through items
for _, pod := range pods.Items {
if pod.Status.Phase != podRunning {
continue
}
// get serialised service from annotation
svcStr, ok := pod.Metadata.Annotations[annotationServiceKeyPrefix+serviceName(name)]
if !ok {
continue
}
// unmarshal service string
var svc registry.Service
err := json.Unmarshal([]byte(*svcStr), &svc)
if err != nil {
return nil, fmt.Errorf("could not unmarshal service '%s' from pod annotation", name)
}
// merge up pod service & ip with versioned service.
vs, ok := svcs[svc.Version]
if !ok {
svcs[svc.Version] = &svc
continue
}
vs.Nodes = append(vs.Nodes, svc.Nodes...)
}
var list []*registry.Service
for _, val := range svcs {
list = append(list, val)
}
return list, nil
} | go | func (c *kregistry) GetService(name string) ([]*registry.Service, error) {
pods, err := c.client.ListPods(map[string]string{
svcSelectorPrefix + serviceName(name): svcSelectorValue,
})
if err != nil {
return nil, err
}
if len(pods.Items) == 0 {
return nil, registry.ErrNotFound
}
// svcs mapped by version
svcs := make(map[string]*registry.Service)
// loop through items
for _, pod := range pods.Items {
if pod.Status.Phase != podRunning {
continue
}
// get serialised service from annotation
svcStr, ok := pod.Metadata.Annotations[annotationServiceKeyPrefix+serviceName(name)]
if !ok {
continue
}
// unmarshal service string
var svc registry.Service
err := json.Unmarshal([]byte(*svcStr), &svc)
if err != nil {
return nil, fmt.Errorf("could not unmarshal service '%s' from pod annotation", name)
}
// merge up pod service & ip with versioned service.
vs, ok := svcs[svc.Version]
if !ok {
svcs[svc.Version] = &svc
continue
}
vs.Nodes = append(vs.Nodes, svc.Nodes...)
}
var list []*registry.Service
for _, val := range svcs {
list = append(list, val)
}
return list, nil
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"GetService",
"(",
"name",
"string",
")",
"(",
"[",
"]",
"*",
"registry",
".",
"Service",
",",
"error",
")",
"{",
"pods",
",",
"err",
":=",
"c",
".",
"client",
".",
"ListPods",
"(",
"map",
"[",
"string",
"]",
"string",
"{",
"svcSelectorPrefix",
"+",
"serviceName",
"(",
"name",
")",
":",
"svcSelectorValue",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"len",
"(",
"pods",
".",
"Items",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"registry",
".",
"ErrNotFound",
"\n",
"}",
"\n",
"svcs",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"registry",
".",
"Service",
")",
"\n",
"for",
"_",
",",
"pod",
":=",
"range",
"pods",
".",
"Items",
"{",
"if",
"pod",
".",
"Status",
".",
"Phase",
"!=",
"podRunning",
"{",
"continue",
"\n",
"}",
"\n",
"svcStr",
",",
"ok",
":=",
"pod",
".",
"Metadata",
".",
"Annotations",
"[",
"annotationServiceKeyPrefix",
"+",
"serviceName",
"(",
"name",
")",
"]",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n",
"var",
"svc",
"registry",
".",
"Service",
"\n",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"[",
"]",
"byte",
"(",
"*",
"svcStr",
")",
",",
"&",
"svc",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"could not unmarshal service '%s' from pod annotation\"",
",",
"name",
")",
"\n",
"}",
"\n",
"vs",
",",
"ok",
":=",
"svcs",
"[",
"svc",
".",
"Version",
"]",
"\n",
"if",
"!",
"ok",
"{",
"svcs",
"[",
"svc",
".",
"Version",
"]",
"=",
"&",
"svc",
"\n",
"continue",
"\n",
"}",
"\n",
"vs",
".",
"Nodes",
"=",
"append",
"(",
"vs",
".",
"Nodes",
",",
"svc",
".",
"Nodes",
"...",
")",
"\n",
"}",
"\n",
"var",
"list",
"[",
"]",
"*",
"registry",
".",
"Service",
"\n",
"for",
"_",
",",
"val",
":=",
"range",
"svcs",
"{",
"list",
"=",
"append",
"(",
"list",
",",
"val",
")",
"\n",
"}",
"\n",
"return",
"list",
",",
"nil",
"\n",
"}"
] | // GetService will get all the pods with the given service selector,
// and build services from the annotations. | [
"GetService",
"will",
"get",
"all",
"the",
"pods",
"with",
"the",
"given",
"service",
"selector",
"and",
"build",
"services",
"from",
"the",
"annotations",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L177-L225 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | ListServices | func (c *kregistry) ListServices() ([]*registry.Service, error) {
pods, err := c.client.ListPods(podSelector)
if err != nil {
return nil, err
}
// svcs mapped by name
svcs := make(map[string]bool)
for _, pod := range pods.Items {
if pod.Status.Phase != podRunning {
continue
}
for k, v := range pod.Metadata.Annotations {
if !strings.HasPrefix(k, annotationServiceKeyPrefix) {
continue
}
// we have to unmarshal the annotation itself since the
// key is encoded to match the regex restriction.
var svc registry.Service
if err := json.Unmarshal([]byte(*v), &svc); err != nil {
continue
}
svcs[svc.Name] = true
}
}
var list []*registry.Service
for val := range svcs {
list = append(list, ®istry.Service{Name: val})
}
return list, nil
} | go | func (c *kregistry) ListServices() ([]*registry.Service, error) {
pods, err := c.client.ListPods(podSelector)
if err != nil {
return nil, err
}
// svcs mapped by name
svcs := make(map[string]bool)
for _, pod := range pods.Items {
if pod.Status.Phase != podRunning {
continue
}
for k, v := range pod.Metadata.Annotations {
if !strings.HasPrefix(k, annotationServiceKeyPrefix) {
continue
}
// we have to unmarshal the annotation itself since the
// key is encoded to match the regex restriction.
var svc registry.Service
if err := json.Unmarshal([]byte(*v), &svc); err != nil {
continue
}
svcs[svc.Name] = true
}
}
var list []*registry.Service
for val := range svcs {
list = append(list, ®istry.Service{Name: val})
}
return list, nil
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"ListServices",
"(",
")",
"(",
"[",
"]",
"*",
"registry",
".",
"Service",
",",
"error",
")",
"{",
"pods",
",",
"err",
":=",
"c",
".",
"client",
".",
"ListPods",
"(",
"podSelector",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"svcs",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"for",
"_",
",",
"pod",
":=",
"range",
"pods",
".",
"Items",
"{",
"if",
"pod",
".",
"Status",
".",
"Phase",
"!=",
"podRunning",
"{",
"continue",
"\n",
"}",
"\n",
"for",
"k",
",",
"v",
":=",
"range",
"pod",
".",
"Metadata",
".",
"Annotations",
"{",
"if",
"!",
"strings",
".",
"HasPrefix",
"(",
"k",
",",
"annotationServiceKeyPrefix",
")",
"{",
"continue",
"\n",
"}",
"\n",
"var",
"svc",
"registry",
".",
"Service",
"\n",
"if",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"[",
"]",
"byte",
"(",
"*",
"v",
")",
",",
"&",
"svc",
")",
";",
"err",
"!=",
"nil",
"{",
"continue",
"\n",
"}",
"\n",
"svcs",
"[",
"svc",
".",
"Name",
"]",
"=",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"var",
"list",
"[",
"]",
"*",
"registry",
".",
"Service",
"\n",
"for",
"val",
":=",
"range",
"svcs",
"{",
"list",
"=",
"append",
"(",
"list",
",",
"&",
"registry",
".",
"Service",
"{",
"Name",
":",
"val",
"}",
")",
"\n",
"}",
"\n",
"return",
"list",
",",
"nil",
"\n",
"}"
] | // ListServices will list all the service names | [
"ListServices",
"will",
"list",
"all",
"the",
"service",
"names"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L228-L261 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | Watch | func (c *kregistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
return newWatcher(c, opts...)
} | go | func (c *kregistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
return newWatcher(c, opts...)
} | [
"func",
"(",
"c",
"*",
"kregistry",
")",
"Watch",
"(",
"opts",
"...",
"registry",
".",
"WatchOption",
")",
"(",
"registry",
".",
"Watcher",
",",
"error",
")",
"{",
"return",
"newWatcher",
"(",
"c",
",",
"opts",
"...",
")",
"\n",
"}"
] | // Watch returns a kubernetes watcher | [
"Watch",
"returns",
"a",
"kubernetes",
"watcher"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L264-L266 | train |
micro/go-plugins | registry/kubernetes/kubernetes.go | NewRegistry | func NewRegistry(opts ...registry.Option) registry.Registry {
k := &kregistry{
options: registry.Options{},
}
configure(k, opts...)
return k
} | go | func NewRegistry(opts ...registry.Option) registry.Registry {
k := &kregistry{
options: registry.Options{},
}
configure(k, opts...)
return k
} | [
"func",
"NewRegistry",
"(",
"opts",
"...",
"registry",
".",
"Option",
")",
"registry",
".",
"Registry",
"{",
"k",
":=",
"&",
"kregistry",
"{",
"options",
":",
"registry",
".",
"Options",
"{",
"}",
",",
"}",
"\n",
"configure",
"(",
"k",
",",
"opts",
"...",
")",
"\n",
"return",
"k",
"\n",
"}"
] | // NewRegistry creates a kubernetes registry | [
"NewRegistry",
"creates",
"a",
"kubernetes",
"registry"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/kubernetes.go#L273-L279 | train |
micro/go-plugins | micro/bot/input/discord/discord.go | CheckPrefixFactory | func CheckPrefixFactory(prefixes ...string) func(string) (string, bool) {
return func(content string) (string, bool) {
for _, prefix := range prefixes {
if strings.HasPrefix(content, prefix) {
return strings.TrimPrefix(content, prefix), true
}
}
return "", false
}
} | go | func CheckPrefixFactory(prefixes ...string) func(string) (string, bool) {
return func(content string) (string, bool) {
for _, prefix := range prefixes {
if strings.HasPrefix(content, prefix) {
return strings.TrimPrefix(content, prefix), true
}
}
return "", false
}
} | [
"func",
"CheckPrefixFactory",
"(",
"prefixes",
"...",
"string",
")",
"func",
"(",
"string",
")",
"(",
"string",
",",
"bool",
")",
"{",
"return",
"func",
"(",
"content",
"string",
")",
"(",
"string",
",",
"bool",
")",
"{",
"for",
"_",
",",
"prefix",
":=",
"range",
"prefixes",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"content",
",",
"prefix",
")",
"{",
"return",
"strings",
".",
"TrimPrefix",
"(",
"content",
",",
"prefix",
")",
",",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"\"\"",
",",
"false",
"\n",
"}",
"\n",
"}"
] | // CheckPrefixFactory Creates a prefix checking function and stuff. | [
"CheckPrefixFactory",
"Creates",
"a",
"prefix",
"checking",
"function",
"and",
"stuff",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/micro/bot/input/discord/discord.go#L141-L150 | train |
micro/go-plugins | registry/kubernetes/client/watch/body.go | Stop | func (wr *bodyWatcher) Stop() {
select {
case <-wr.stop:
return
default:
close(wr.stop)
close(wr.results)
}
} | go | func (wr *bodyWatcher) Stop() {
select {
case <-wr.stop:
return
default:
close(wr.stop)
close(wr.results)
}
} | [
"func",
"(",
"wr",
"*",
"bodyWatcher",
")",
"Stop",
"(",
")",
"{",
"select",
"{",
"case",
"<-",
"wr",
".",
"stop",
":",
"return",
"\n",
"default",
":",
"close",
"(",
"wr",
".",
"stop",
")",
"\n",
"close",
"(",
"wr",
".",
"results",
")",
"\n",
"}",
"\n",
"}"
] | // Stop cancels the request | [
"Stop",
"cancels",
"the",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/watch/body.go#L24-L32 | train |
micro/go-plugins | registry/kubernetes/client/watch/body.go | NewBodyWatcher | func NewBodyWatcher(req *http.Request, client *http.Client) (Watch, error) {
stop := make(chan struct{})
req.Cancel = stop
res, err := client.Do(req)
if err != nil {
return nil, err
}
wr := &bodyWatcher{
results: make(chan Event),
stop: stop,
req: req,
res: res,
}
go wr.stream()
return wr, nil
} | go | func NewBodyWatcher(req *http.Request, client *http.Client) (Watch, error) {
stop := make(chan struct{})
req.Cancel = stop
res, err := client.Do(req)
if err != nil {
return nil, err
}
wr := &bodyWatcher{
results: make(chan Event),
stop: stop,
req: req,
res: res,
}
go wr.stream()
return wr, nil
} | [
"func",
"NewBodyWatcher",
"(",
"req",
"*",
"http",
".",
"Request",
",",
"client",
"*",
"http",
".",
"Client",
")",
"(",
"Watch",
",",
"error",
")",
"{",
"stop",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
"\n",
"req",
".",
"Cancel",
"=",
"stop",
"\n",
"res",
",",
"err",
":=",
"client",
".",
"Do",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"wr",
":=",
"&",
"bodyWatcher",
"{",
"results",
":",
"make",
"(",
"chan",
"Event",
")",
",",
"stop",
":",
"stop",
",",
"req",
":",
"req",
",",
"res",
":",
"res",
",",
"}",
"\n",
"go",
"wr",
".",
"stream",
"(",
")",
"\n",
"return",
"wr",
",",
"nil",
"\n",
"}"
] | // NewBodyWatcher creates a k8s body watcher for
// a given http request | [
"NewBodyWatcher",
"creates",
"a",
"k8s",
"body",
"watcher",
"for",
"a",
"given",
"http",
"request"
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/registry/kubernetes/client/watch/body.go#L74-L92 | train |
micro/go-plugins | broker/gocloud/gocloud.go | NewBroker | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
var openTopic topicOpener
var openSub subOpener
var err error
if projID, ok := options.Context.Value(gcpProjectIDKey{}).(gcp.ProjectID); ok {
ts := options.Context.Value(gcpTokenSourceKey{}).(gcp.TokenSource)
openTopic, openSub, err = setupGCP(options.Context, projID, ts)
} else if rurl, ok := options.Context.Value(rabbitURLKey{}).(string); ok {
openTopic, openSub, err = setupRabbit(options.Context, rurl)
} else {
openTopic, openSub, err = setupMem()
}
if err != nil {
return &pubsubBroker{err: err}
}
return &pubsubBroker{
options: options,
openTopic: openTopic,
openSub: openSub,
topics: map[string]*pubsub.Topic{},
subs: map[string]*pubsub.Subscription{},
}
} | go | func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
var openTopic topicOpener
var openSub subOpener
var err error
if projID, ok := options.Context.Value(gcpProjectIDKey{}).(gcp.ProjectID); ok {
ts := options.Context.Value(gcpTokenSourceKey{}).(gcp.TokenSource)
openTopic, openSub, err = setupGCP(options.Context, projID, ts)
} else if rurl, ok := options.Context.Value(rabbitURLKey{}).(string); ok {
openTopic, openSub, err = setupRabbit(options.Context, rurl)
} else {
openTopic, openSub, err = setupMem()
}
if err != nil {
return &pubsubBroker{err: err}
}
return &pubsubBroker{
options: options,
openTopic: openTopic,
openSub: openSub,
topics: map[string]*pubsub.Topic{},
subs: map[string]*pubsub.Subscription{},
}
} | [
"func",
"NewBroker",
"(",
"opts",
"...",
"broker",
".",
"Option",
")",
"broker",
".",
"Broker",
"{",
"options",
":=",
"broker",
".",
"Options",
"{",
"Context",
":",
"context",
".",
"Background",
"(",
")",
",",
"}",
"\n",
"for",
"_",
",",
"o",
":=",
"range",
"opts",
"{",
"o",
"(",
"&",
"options",
")",
"\n",
"}",
"\n",
"var",
"openTopic",
"topicOpener",
"\n",
"var",
"openSub",
"subOpener",
"\n",
"var",
"err",
"error",
"\n",
"if",
"projID",
",",
"ok",
":=",
"options",
".",
"Context",
".",
"Value",
"(",
"gcpProjectIDKey",
"{",
"}",
")",
".",
"(",
"gcp",
".",
"ProjectID",
")",
";",
"ok",
"{",
"ts",
":=",
"options",
".",
"Context",
".",
"Value",
"(",
"gcpTokenSourceKey",
"{",
"}",
")",
".",
"(",
"gcp",
".",
"TokenSource",
")",
"\n",
"openTopic",
",",
"openSub",
",",
"err",
"=",
"setupGCP",
"(",
"options",
".",
"Context",
",",
"projID",
",",
"ts",
")",
"\n",
"}",
"else",
"if",
"rurl",
",",
"ok",
":=",
"options",
".",
"Context",
".",
"Value",
"(",
"rabbitURLKey",
"{",
"}",
")",
".",
"(",
"string",
")",
";",
"ok",
"{",
"openTopic",
",",
"openSub",
",",
"err",
"=",
"setupRabbit",
"(",
"options",
".",
"Context",
",",
"rurl",
")",
"\n",
"}",
"else",
"{",
"openTopic",
",",
"openSub",
",",
"err",
"=",
"setupMem",
"(",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"pubsubBroker",
"{",
"err",
":",
"err",
"}",
"\n",
"}",
"\n",
"return",
"&",
"pubsubBroker",
"{",
"options",
":",
"options",
",",
"openTopic",
":",
"openTopic",
",",
"openSub",
":",
"openSub",
",",
"topics",
":",
"map",
"[",
"string",
"]",
"*",
"pubsub",
".",
"Topic",
"{",
"}",
",",
"subs",
":",
"map",
"[",
"string",
"]",
"*",
"pubsub",
".",
"Subscription",
"{",
"}",
",",
"}",
"\n",
"}"
] | // NewBroker creates a new gocloud pubsubBroker.
// If the GCPProjectID option is set, Go Cloud uses its Google Cloud PubSub implementation.
// If the RabbitURL option is set, Go Cloud uses its RabbitMQ implementation.
// Otherwise, Go Cloud uses its in-memory implementation. | [
"NewBroker",
"creates",
"a",
"new",
"gocloud",
"pubsubBroker",
".",
"If",
"the",
"GCPProjectID",
"option",
"is",
"set",
"Go",
"Cloud",
"uses",
"its",
"Google",
"Cloud",
"PubSub",
"implementation",
".",
"If",
"the",
"RabbitURL",
"option",
"is",
"set",
"Go",
"Cloud",
"uses",
"its",
"RabbitMQ",
"implementation",
".",
"Otherwise",
"Go",
"Cloud",
"uses",
"its",
"in",
"-",
"memory",
"implementation",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/gocloud/gocloud.go#L47-L76 | train |
micro/go-plugins | broker/gocloud/gocloud.go | Publish | func (b *pubsubBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
if b.err != nil {
return b.err
}
t := b.topic(topic)
return t.Send(context.Background(), &pubsub.Message{Metadata: msg.Header, Body: msg.Body})
} | go | func (b *pubsubBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
if b.err != nil {
return b.err
}
t := b.topic(topic)
return t.Send(context.Background(), &pubsub.Message{Metadata: msg.Header, Body: msg.Body})
} | [
"func",
"(",
"b",
"*",
"pubsubBroker",
")",
"Publish",
"(",
"topic",
"string",
",",
"msg",
"*",
"broker",
".",
"Message",
",",
"opts",
"...",
"broker",
".",
"PublishOption",
")",
"error",
"{",
"if",
"b",
".",
"err",
"!=",
"nil",
"{",
"return",
"b",
".",
"err",
"\n",
"}",
"\n",
"t",
":=",
"b",
".",
"topic",
"(",
"topic",
")",
"\n",
"return",
"t",
".",
"Send",
"(",
"context",
".",
"Background",
"(",
")",
",",
"&",
"pubsub",
".",
"Message",
"{",
"Metadata",
":",
"msg",
".",
"Header",
",",
"Body",
":",
"msg",
".",
"Body",
"}",
")",
"\n",
"}"
] | // Publish opens the topic if it hasn't been already, then publishes the message. | [
"Publish",
"opens",
"the",
"topic",
"if",
"it",
"hasn",
"t",
"been",
"already",
"then",
"publishes",
"the",
"message",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/gocloud/gocloud.go#L131-L137 | train |
micro/go-plugins | broker/gocloud/gocloud.go | topic | func (b *pubsubBroker) topic(name string) *pubsub.Topic {
b.mu.Lock()
defer b.mu.Unlock()
t := b.topics[name]
if t == nil {
t = b.openTopic(name)
b.topics[name] = t
}
return t
} | go | func (b *pubsubBroker) topic(name string) *pubsub.Topic {
b.mu.Lock()
defer b.mu.Unlock()
t := b.topics[name]
if t == nil {
t = b.openTopic(name)
b.topics[name] = t
}
return t
} | [
"func",
"(",
"b",
"*",
"pubsubBroker",
")",
"topic",
"(",
"name",
"string",
")",
"*",
"pubsub",
".",
"Topic",
"{",
"b",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"b",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"t",
":=",
"b",
".",
"topics",
"[",
"name",
"]",
"\n",
"if",
"t",
"==",
"nil",
"{",
"t",
"=",
"b",
".",
"openTopic",
"(",
"name",
")",
"\n",
"b",
".",
"topics",
"[",
"name",
"]",
"=",
"t",
"\n",
"}",
"\n",
"return",
"t",
"\n",
"}"
] | // topic returns the topic with the given name if this broker has
// seen it before. Otherwise it opens a new topic. | [
"topic",
"returns",
"the",
"topic",
"with",
"the",
"given",
"name",
"if",
"this",
"broker",
"has",
"seen",
"it",
"before",
".",
"Otherwise",
"it",
"opens",
"a",
"new",
"topic",
"."
] | d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0 | https://github.com/micro/go-plugins/blob/d1cb339c5c5a3ab1e4be090c75a3cc03e38348e0/broker/gocloud/gocloud.go#L181-L190 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.