nats-io / kubecon-contribfest

Apache License 2.0
0 stars 1 forks source link

Refactor context.go and nats.go and make them DRY #4

Open piotrpio opened 1 year ago

piotrpio commented 1 year ago

context.go duplicates a lot of code from nats.go. Refactor those files to remove duplications and make the code DRY.

l33t3mr commented 1 year ago

I'll try to work on this!

l33t3mr commented 1 year ago

@wallyqs I'd suggest to refactor the functions in nats.go to be wrappers as in the following example:

func (nc *Conn) RequestMsg(msg *Msg, timeout time.Duration) (*Msg, error) {
    if msg == nil {
        return nil, ErrInvalidMsg
    }
    hdr, err := msg.headerBytes()
    if err != nil {
        return nil, err
    }

    return nc.request(msg.Subject, hdr, msg.Data, timeout)
}

should become:

func (nc *Conn) RequestMsg(msg *Msg, timeout time.Duration) (*Msg, error) {
    ctx := context.Background()
    timoutCtx,cancel := context.WithTimeout(ctx, timeout)
    defer cancel()
    return nc.RequestMsgWithContext(timoutCtx, msg)

}

With the option to make the context.WithTimeOut a global variable. I'll continue to take a look at the code as I might get other ideas.