-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "refactoring http ammo" This reverts commit 19bec38cfddef61db1c13b7f15ef8b542acf6a95.
- Loading branch information
Showing
21 changed files
with
475 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,47 @@ | ||
package base | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
urlpkg "net/url" | ||
import "github.com/yandex/pandora/core/aggregator/netsample" | ||
|
||
"github.com/yandex/pandora/components/providers/http/util" | ||
"github.com/yandex/pandora/core/aggregator/netsample" | ||
"github.com/yandex/pandora/lib/netutil" | ||
) | ||
|
||
func NewAmmo(method string, url string, body []byte, header http.Header, tag string) (*Ammo, error) { | ||
if ok := netutil.ValidHTTPMethod(method); !ok { | ||
return nil, errors.New("invalid HTTP method " + method) | ||
} | ||
if _, err := urlpkg.Parse(url); err != nil { | ||
return nil, fmt.Errorf("invalid URL %s; err %w ", url, err) | ||
} | ||
return &Ammo{ | ||
method: method, | ||
body: body, | ||
url: url, | ||
tag: tag, | ||
header: header, | ||
constructor: true, | ||
}, nil | ||
} | ||
|
||
type Ammo struct { | ||
Req *http.Request | ||
method string | ||
body []byte | ||
url string | ||
tag string | ||
header http.Header | ||
id uint64 | ||
isInvalid bool | ||
constructor bool | ||
type Ammo[R any] struct { | ||
Req *R | ||
tag string | ||
id uint64 | ||
isInvalid bool | ||
} | ||
|
||
func (a *Ammo) Request() (*http.Request, *netsample.Sample) { | ||
if a.Req == nil { | ||
_ = a.BuildRequest() // TODO: what if error. There isn't a logger | ||
} | ||
func (a *Ammo[R]) Request() (*R, *netsample.Sample) { | ||
sample := netsample.Acquire(a.Tag()) | ||
sample.SetID(a.ID()) | ||
return a.Req, sample | ||
} | ||
|
||
func (a *Ammo) SetID(id uint64) { | ||
func (a *Ammo[R]) Reset(req *R, tag string) { | ||
a.Req = req | ||
a.tag = tag | ||
a.id = 0 | ||
a.isInvalid = false | ||
} | ||
|
||
func (a *Ammo[_]) SetID(id uint64) { | ||
a.id = id | ||
} | ||
|
||
func (a *Ammo) ID() uint64 { | ||
func (a *Ammo[_]) ID() uint64 { | ||
return a.id | ||
} | ||
|
||
func (a *Ammo) Invalidate() { | ||
func (a *Ammo[_]) Invalidate() { | ||
a.isInvalid = true | ||
} | ||
|
||
func (a *Ammo) IsInvalid() bool { | ||
func (a *Ammo[_]) IsInvalid() bool { | ||
return a.isInvalid | ||
} | ||
|
||
func (a *Ammo) IsValid() bool { | ||
func (a *Ammo[_]) IsValid() bool { | ||
return !a.isInvalid | ||
} | ||
|
||
func (a *Ammo) SetTag(tag string) { | ||
a.tag = tag | ||
} | ||
|
||
func (a *Ammo) Tag() string { | ||
func (a *Ammo[_]) Tag() string { | ||
return a.tag | ||
} | ||
|
||
func (a *Ammo) FromConstructor() bool { | ||
return a.constructor | ||
} | ||
|
||
// use NewAmmo() for skipping error here | ||
func (a *Ammo) BuildRequest() error { | ||
var buff io.Reader | ||
if a.body != nil { | ||
buff = bytes.NewReader(a.body) | ||
} | ||
req, err := http.NewRequest(a.method, a.url, buff) | ||
if err != nil { | ||
return fmt.Errorf("cant create request: %w", err) | ||
} | ||
a.Req = req | ||
util.EnrichRequestWithHeaders(req, a.header) | ||
return nil | ||
} | ||
|
||
func (a *Ammo) Reset() { | ||
a.Req = nil | ||
a.method = "" | ||
a.body = nil | ||
a.url = "" | ||
a.tag = "" | ||
a.header = nil | ||
a.id = 0 | ||
a.isInvalid = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package base | ||
|
||
import "sync" | ||
|
||
type Decoder[R any] struct { | ||
Sink chan<- *Ammo[R] | ||
Pool *sync.Pool | ||
} | ||
|
||
func NewDecoder[R any](sink chan<- *Ammo[R]) Decoder[R] { | ||
return Decoder[R]{ | ||
Sink: sink, | ||
Pool: &sync.Pool{New: func() any { | ||
return new(Ammo[R]) | ||
}}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.