-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/upstream_service'
- Loading branch information
Showing
14 changed files
with
296 additions
and
140 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package service_http | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/eolinker/eosc/log" | ||
|
||
http_service "github.com/eolinker/eosc/http-service" | ||
) | ||
|
||
var _ http_service.IChain = (*UpstreamHandler)(nil) | ||
|
||
type UpstreamHandler struct { | ||
id string | ||
upstream *Upstream | ||
retry int | ||
timeout time.Duration | ||
} | ||
|
||
func (u *UpstreamHandler) Destroy() { | ||
|
||
upstream := u.upstream | ||
if upstream != nil { | ||
u.upstream = nil | ||
upstream.handlers.Del(u.id) | ||
} | ||
|
||
} | ||
|
||
func NewUpstreamHandler(id string, upstream *Upstream, retry int, timeout time.Duration) *UpstreamHandler { | ||
uh := &UpstreamHandler{ | ||
id: id, | ||
upstream: upstream, | ||
retry: retry, | ||
timeout: timeout, | ||
} | ||
return uh | ||
} | ||
|
||
//DoChain 请求发送 | ||
func (u *UpstreamHandler) DoChain(ctx http_service.IHttpContext) error { | ||
|
||
var lastErr error | ||
|
||
//设置响应开始时间 | ||
proxyTime := time.Now() | ||
|
||
defer func() { | ||
//设置原始响应状态码 | ||
ctx.Response().SetProxyStatus(ctx.Response().StatusCode(), "") | ||
//设置上游响应时间, 单位为毫秒 | ||
ctx.WithValue("response_time", time.Now().Sub(proxyTime).Milliseconds()) | ||
}() | ||
|
||
for doTrice := u.retry + 1; doTrice > 0; doTrice-- { | ||
|
||
node, err := u.upstream.handler.Next() | ||
if err != nil { | ||
return err | ||
} | ||
scheme := node.Scheme() | ||
if scheme != "http" && scheme != "https" { | ||
scheme = u.upstream.scheme | ||
} | ||
log.Debug("node: ", node.Addr()) | ||
addr := fmt.Sprintf("%s://%s", scheme, node.Addr()) | ||
lastErr = ctx.SendTo(addr, u.timeout) | ||
if lastErr == nil { | ||
return nil | ||
} | ||
log.Error("http upstream send error: ", lastErr) | ||
} | ||
|
||
return lastErr | ||
} |
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,50 @@ | ||
package service_http | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/eolinker/apinto/upstream" | ||
|
||
"github.com/eolinker/eosc" | ||
|
||
"github.com/eolinker/apinto/discovery" | ||
"github.com/eolinker/apinto/upstream/balance" | ||
) | ||
|
||
type Upstream struct { | ||
scheme string | ||
app discovery.IApp | ||
handler balance.IBalanceHandler | ||
|
||
handlers eosc.IUntyped | ||
} | ||
|
||
func (up *Upstream) Create(id string, retry int, timeout time.Duration) (upstream.IUpstreamHandler, error) { | ||
return up.create(id, retry, timeout), nil | ||
} | ||
func (up *Upstream) create(id string, retry int, timeout time.Duration) *UpstreamHandler { | ||
nh := NewUpstreamHandler(id, up, retry, timeout) | ||
up.handlers.Set(id, nh) | ||
return nh | ||
} | ||
|
||
func NewUpstream(scheme string, app discovery.IApp, handler balance.IBalanceHandler) *Upstream { | ||
return &Upstream{scheme: scheme, app: app, handler: handler, handlers: eosc.NewUntyped()} | ||
} | ||
|
||
//Reset reset | ||
func (up *Upstream) Reset(scheme string, app discovery.IApp, handler balance.IBalanceHandler) { | ||
up.scheme = scheme | ||
up.app = app | ||
up.handler = handler | ||
} | ||
|
||
func (up *Upstream) destroy() { | ||
handlers := up.handlers.List() | ||
up.handlers = eosc.NewUntyped() | ||
for _, h := range handlers { | ||
hd := h.(*UpstreamHandler) | ||
hd.Destroy() | ||
} | ||
|
||
} |
Oops, something went wrong.