Fix golint warnings for publisher package (#1822)

Co-authored-by: Shajitha Mohammed <shajithamohammed28@gmail.com>
This commit is contained in:
Sajitha
2020-10-27 21:19:28 +05:30
committed by GitHub
co-authored by Shajitha Mohammed
parent e65bfcdacf
commit fe5542845e
2 changed files with 12 additions and 8 deletions
+3 -1
View File
@@ -17,8 +17,10 @@ limitations under the License.
package publisher package publisher
type ( type (
// Publisher interface wraps the Publish method that publishes an request
// with given "body" and "headers" to given "target"
Publisher interface { Publisher interface {
// Publish an request to a "target". Target's meaning depends on the // Publish an request to a "target". Target's meaning depends on the
// publisher: it's a URL in the case of a webhook publisher, or a queue // publisher: it's a URL in the case of a webhook publisher, or a queue
// name in a queue-based publisher such as NATS. // name in a queue-based publisher such as NATS.
Publish(body string, headers map[string]string, target string) Publish(body string, headers map[string]string, target string)
+9 -7
View File
@@ -27,7 +27,7 @@ import (
) )
type ( type (
// A webhook publisher for a single URL. Satisifies the Publisher interface. // WebhookPublisher for a single URL. Satisfies the Publisher interface.
WebhookPublisher struct { WebhookPublisher struct {
logger *zap.Logger logger *zap.Logger
@@ -36,7 +36,7 @@ type (
maxRetries int maxRetries int
retryDelay time.Duration retryDelay time.Duration
baseUrl string baseURL string
} }
publishRequest struct { publishRequest struct {
body string body string
@@ -47,10 +47,11 @@ type (
} }
) )
func MakeWebhookPublisher(logger *zap.Logger, baseUrl string) *WebhookPublisher { // MakeWebhookPublisher creates a WebhookPublisher object for the given baseURL
func MakeWebhookPublisher(logger *zap.Logger, baseURL string) *WebhookPublisher {
p := &WebhookPublisher{ p := &WebhookPublisher{
logger: logger.Named("webhook_publisher"), logger: logger.Named("webhook_publisher"),
baseUrl: baseUrl, baseURL: baseURL,
requestChannel: make(chan *publishRequest, 32), // buffered channel requestChannel: make(chan *publishRequest, 32), // buffered channel
// TODO make this configurable // TODO make this configurable
maxRetries: 10, maxRetries: 10,
@@ -60,6 +61,7 @@ func MakeWebhookPublisher(logger *zap.Logger, baseUrl string) *WebhookPublisher
return p return p
} }
// Publish sends a request to the target with payload having given body and headers
func (p *WebhookPublisher) Publish(body string, headers map[string]string, target string) { func (p *WebhookPublisher) Publish(body string, headers map[string]string, target string) {
// serializing the request gives user a guarantee that the request is sent in sequence order // serializing the request gives user a guarantee that the request is sent in sequence order
p.requestChannel <- &publishRequest{ p.requestChannel <- &publishRequest{
@@ -74,12 +76,12 @@ func (p *WebhookPublisher) Publish(body string, headers map[string]string, targe
func (p *WebhookPublisher) svc() { func (p *WebhookPublisher) svc() {
for { for {
r := <-p.requestChannel r := <-p.requestChannel
p.makeHttpRequest(r) p.makeHTTPRequest(r)
} }
} }
func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) { func (p *WebhookPublisher) makeHTTPRequest(r *publishRequest) {
url := p.baseUrl + "/" + strings.TrimPrefix(r.target, "/") url := p.baseURL + "/" + strings.TrimPrefix(r.target, "/")
msg := "making HTTP request" msg := "making HTTP request"
level := zap.ErrorLevel level := zap.ErrorLevel