Skip to content

Commit 7aaec5e

Browse files
committed
add example for timeout settings in API file
1 parent 516c49a commit 7aaec5e

File tree

14 files changed

+1028
-2
lines changed

14 files changed

+1028
-2
lines changed

http/timeout/etc/timeout-api.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name: timeout-api
2+
Host: 0.0.0.0
3+
Port: 8888

http/timeout/go.mod

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module timeout
2+
3+
go 1.18
4+
5+
require github.com/zeromicro/go-zero v1.3.2
6+
7+
require (
8+
github.com/beorn7/perks v1.0.1 // indirect
9+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
10+
github.com/go-logr/logr v1.2.2 // indirect
11+
github.com/go-logr/stdr v1.2.2 // indirect
12+
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
13+
github.com/golang/protobuf v1.5.2 // indirect
14+
github.com/google/uuid v1.3.0 // indirect
15+
github.com/justinas/alice v1.2.0 // indirect
16+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
17+
github.com/openzipkin/zipkin-go v0.4.0 // indirect
18+
github.com/prometheus/client_golang v1.11.0 // indirect
19+
github.com/prometheus/client_model v0.2.0 // indirect
20+
github.com/prometheus/common v0.30.0 // indirect
21+
github.com/prometheus/procfs v0.7.3 // indirect
22+
github.com/spaolacci/murmur3 v1.1.0 // indirect
23+
go.opentelemetry.io/otel v1.3.0 // indirect
24+
go.opentelemetry.io/otel/exporters/jaeger v1.3.0 // indirect
25+
go.opentelemetry.io/otel/exporters/zipkin v1.3.0 // indirect
26+
go.opentelemetry.io/otel/sdk v1.3.0 // indirect
27+
go.opentelemetry.io/otel/trace v1.3.0 // indirect
28+
go.uber.org/automaxprocs v1.4.0 // indirect
29+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
30+
google.golang.org/grpc v1.44.0 // indirect
31+
google.golang.org/protobuf v1.27.1 // indirect
32+
gopkg.in/yaml.v2 v2.4.0 // indirect
33+
)

http/timeout/go.sum

+755
Large diffs are not rendered by default.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package config
2+
3+
import "github.com/zeromicro/go-zero/rest"
4+
5+
type Config struct {
6+
rest.RestConf
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package handler
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/zeromicro/go-zero/rest/httpx"
7+
"timeout/internal/logic"
8+
"timeout/internal/svc"
9+
"timeout/internal/types"
10+
)
11+
12+
func FastHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
13+
return func(w http.ResponseWriter, r *http.Request) {
14+
var req types.FastRequest
15+
if err := httpx.Parse(r, &req); err != nil {
16+
httpx.Error(w, err)
17+
return
18+
}
19+
20+
l := logic.NewFastLogic(r.Context(), svcCtx)
21+
err := l.Fast(&req)
22+
if err != nil {
23+
httpx.Error(w, err)
24+
} else {
25+
httpx.Ok(w)
26+
}
27+
}
28+
}

http/timeout/internal/handler/routes.go

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package handler
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/zeromicro/go-zero/rest/httpx"
7+
"timeout/internal/logic"
8+
"timeout/internal/svc"
9+
"timeout/internal/types"
10+
)
11+
12+
func SlowHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
13+
return func(w http.ResponseWriter, r *http.Request) {
14+
var req types.SlowRequest
15+
if err := httpx.Parse(r, &req); err != nil {
16+
httpx.Error(w, err)
17+
return
18+
}
19+
20+
l := logic.NewSlowLogic(r.Context(), svcCtx)
21+
err := l.Slow(&req)
22+
if err != nil {
23+
httpx.Error(w, err)
24+
} else {
25+
httpx.Ok(w)
26+
}
27+
}
28+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package logic
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"timeout/internal/svc"
8+
"timeout/internal/types"
9+
10+
"github.com/zeromicro/go-zero/core/logx"
11+
)
12+
13+
type FastLogic struct {
14+
logx.Logger
15+
ctx context.Context
16+
svcCtx *svc.ServiceContext
17+
}
18+
19+
func NewFastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FastLogic {
20+
return &FastLogic{
21+
Logger: logx.WithContext(ctx),
22+
ctx: ctx,
23+
svcCtx: svcCtx,
24+
}
25+
}
26+
27+
func (l *FastLogic) Fast(req *types.FastRequest) error {
28+
// should timeout
29+
time.Sleep(time.Second * 4)
30+
31+
return nil
32+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package logic
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"timeout/internal/svc"
8+
"timeout/internal/types"
9+
10+
"github.com/zeromicro/go-zero/core/logx"
11+
)
12+
13+
type SlowLogic struct {
14+
logx.Logger
15+
ctx context.Context
16+
svcCtx *svc.ServiceContext
17+
}
18+
19+
func NewSlowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SlowLogic {
20+
return &SlowLogic{
21+
Logger: logx.WithContext(ctx),
22+
ctx: ctx,
23+
svcCtx: svcCtx,
24+
}
25+
}
26+
27+
func (l *SlowLogic) Slow(req *types.SlowRequest) error {
28+
time.Sleep(time.Second * 4)
29+
30+
return nil
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package svc
2+
3+
import (
4+
"timeout/internal/config"
5+
)
6+
7+
type ServiceContext struct {
8+
Config config.Config
9+
}
10+
11+
func NewServiceContext(c config.Config) *ServiceContext {
12+
return &ServiceContext{
13+
Config: c,
14+
}
15+
}

http/timeout/internal/types/types.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

http/timeout/timeout.api

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type SlowRequest {
2+
Name string `path:"name"`
3+
}
4+
5+
type FastRequest {
6+
Name string `path:"name"`
7+
}
8+
9+
@server(
10+
timeout: 5s
11+
)
12+
service timeout-api {
13+
@handler SlowHandler
14+
get /slow/:name(SlowRequest)
15+
}
16+
17+
service timeout-api {
18+
@handler FastHandler
19+
get /fast/:name(FastRequest)
20+
}

http/timeout/timeout.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"timeout/internal/config"
8+
"timeout/internal/handler"
9+
"timeout/internal/svc"
10+
11+
"github.com/zeromicro/go-zero/core/conf"
12+
"github.com/zeromicro/go-zero/rest"
13+
)
14+
15+
var configFile = flag.String("f", "etc/timeout-api.yaml", "the config file")
16+
17+
func main() {
18+
flag.Parse()
19+
20+
var c config.Config
21+
conf.MustLoad(*configFile, &c)
22+
23+
ctx := svc.NewServiceContext(c)
24+
server := rest.MustNewServer(c.RestConf)
25+
defer server.Stop()
26+
27+
handler.RegisterHandlers(server, ctx)
28+
29+
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
30+
server.Start()
31+
}

timingwheel/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func timingWheelMode() {
4949
defer tw.Stop()
5050
for i := 0; ; i++ {
5151
tw.SetTimer(i, i, interval)
52-
time.Sleep(time.Millisecond)
52+
time.Sleep(time.Microsecond)
5353
}
5454
}
5555

@@ -72,7 +72,7 @@ func traditionalMode() {
7272

7373
func job(count *uint64) {
7474
v := atomic.AddUint64(count, 1)
75-
if v%1000 == 0 {
75+
if v%10000 == 0 {
7676
fmt.Println(v)
7777
}
7878
}

0 commit comments

Comments
 (0)