Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gomicro-base
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lizifeng
gomicro-base
Commits
67e5d558
Commit
67e5d558
authored
Jun 10, 2022
by
kzkzzzz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 增加超时
parent
1ae63cd0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
2 deletions
+19
-2
error.go
common/errorm/error.go
+1
-1
gateway.go
tool/gateway/gateway.go
+18
-1
No files found.
common/error
x
/error.go
→
common/error
m
/error.go
View file @
67e5d558
package
error
x
package
error
m
import
(
"fmt"
...
...
tool/gateway/gateway.go
View file @
67e5d558
...
...
@@ -15,6 +15,7 @@ import (
"net/http"
"os"
"sync"
"time"
)
var
(
...
...
@@ -31,6 +32,22 @@ type response struct {
Result
interface
{}
`json:"result"`
}
// 超时控制
type
timeoutWrapper
struct
{
client
.
Client
}
func
NewTimeoutWrapper
(
c
client
.
Client
)
client
.
Client
{
return
&
timeoutWrapper
{
c
}
}
func
(
t
*
timeoutWrapper
)
Call
(
ctx
context
.
Context
,
req
client
.
Request
,
rsp
interface
{},
opts
...
client
.
CallOption
)
error
{
// 2秒超时
timeoutCtx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
2
)
defer
cancel
()
return
t
.
Client
.
Call
(
timeoutCtx
,
req
,
rsp
,
opts
...
)
}
func
main
()
{
pool
,
_
=
ants
.
NewPool
(
16
)
// 协程池, 并发查询GetService用
defer
pool
.
Release
()
...
...
@@ -64,7 +81,7 @@ func start() {
service
:=
micro
.
NewService
(
micro
.
Client
(
grpc
.
NewClient
()),
micro
.
Selector
(
newSelector
),
//micro.WrapClient(NewLog
Wrapper),
micro
.
WrapClient
(
NewTimeout
Wrapper
),
)
microClient
=
service
.
Client
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment