fix golint issues in core/threading (#524)

master
Kevin Wan 4 years ago committed by GitHub
parent f309e9f80c
commit ad32f9de23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,10 +2,12 @@ package threading
import "sync" import "sync"
// A RoutineGroup is used to group goroutines together and all wait all goroutines to be done.
type RoutineGroup struct { type RoutineGroup struct {
waitGroup sync.WaitGroup waitGroup sync.WaitGroup
} }
// NewRoutineGroup returns a RoutineGroup.
func NewRoutineGroup() *RoutineGroup { func NewRoutineGroup() *RoutineGroup {
return new(RoutineGroup) return new(RoutineGroup)
} }

@ -5,16 +5,19 @@ import (
"github.com/tal-tech/go-zero/core/rescue" "github.com/tal-tech/go-zero/core/rescue"
) )
// A TaskRunner is used to control the concurrency of goroutines.
type TaskRunner struct { type TaskRunner struct {
limitChan chan lang.PlaceholderType limitChan chan lang.PlaceholderType
} }
// NewTaskRunner returns a TaskRunner.
func NewTaskRunner(concurrency int) *TaskRunner { func NewTaskRunner(concurrency int) *TaskRunner {
return &TaskRunner{ return &TaskRunner{
limitChan: make(chan lang.PlaceholderType, concurrency), limitChan: make(chan lang.PlaceholderType, concurrency),
} }
} }
// Schedule schedules a task to run under concurrency control.
func (rp *TaskRunner) Schedule(task func()) { func (rp *TaskRunner) Schedule(task func()) {
rp.limitChan <- lang.Placeholder rp.limitChan <- lang.Placeholder

@ -1,10 +1,12 @@
package threading package threading
// A WorkerGroup is used to run given number of workers to process jobs.
type WorkerGroup struct { type WorkerGroup struct {
job func() job func()
workers int workers int
} }
// NewWorkerGroup returns a WorkerGroup with given job and workers.
func NewWorkerGroup(job func(), workers int) WorkerGroup { func NewWorkerGroup(job func(), workers int) WorkerGroup {
return WorkerGroup{ return WorkerGroup{
job: job, job: job,
@ -12,6 +14,7 @@ func NewWorkerGroup(job func(), workers int) WorkerGroup {
} }
} }
// Start starts a WorkerGroup.
func (wg WorkerGroup) Start() { func (wg WorkerGroup) Start() {
group := NewRoutineGroup() group := NewRoutineGroup()
for i := 0; i < wg.workers; i++ { for i := 0; i < wg.workers; i++ {

@ -2,11 +2,11 @@ package httpx
import "net/http" import "net/http"
const xForwardFor = "X-Forwarded-For" const xForwardedFor = "X-Forwarded-For"
// GetRemoteAddr returns the peer address, supports X-Forward-For. // GetRemoteAddr returns the peer address, supports X-Forward-For.
func GetRemoteAddr(r *http.Request) string { func GetRemoteAddr(r *http.Request) string {
v := r.Header.Get(xForwardFor) v := r.Header.Get(xForwardedFor)
if len(v) > 0 { if len(v) > 0 {
return v return v
} }

@ -13,6 +13,6 @@ func TestGetRemoteAddr(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader("")) r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
assert.Nil(t, err) assert.Nil(t, err)
r.Header.Set(xForwardFor, host) r.Header.Set(xForwardedFor, host)
assert.Equal(t, host, GetRemoteAddr(r)) assert.Equal(t, host, GetRemoteAddr(r))
} }

Loading…
Cancel
Save