Skip to content

Commit bfd812d

Browse files
authored
feat: support fasthttp server (#288)
1 parent 5f44272 commit bfd812d

File tree

6 files changed

+928
-0
lines changed

6 files changed

+928
-0
lines changed

pkg/server/xfasthttp/config.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2022 Douyu
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package xfasthttp
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/douyu/jupiter/pkg/conf"
21+
"github.com/douyu/jupiter/pkg/constant"
22+
"github.com/douyu/jupiter/pkg/ecode"
23+
"github.com/douyu/jupiter/pkg/flag"
24+
"github.com/douyu/jupiter/pkg/xlog"
25+
"github.com/pkg/errors"
26+
)
27+
28+
//ModName named a mod
29+
const ModName = "server.fasthttp"
30+
31+
//Config HTTP config
32+
type Config struct {
33+
Host string
34+
Port int
35+
Deployment string
36+
Debug bool
37+
DisableMetric bool
38+
DisableTrace bool
39+
DisablePrintStack bool
40+
// ServiceAddress service address in registry info, default to 'Host:Port'
41+
ServiceAddress string
42+
CertFile string
43+
PrivateFile string
44+
EnableTLS bool
45+
46+
SlowQueryThresholdInMilli int64
47+
ReadBufferSize int
48+
WriteBufferSize int
49+
ReduceMemoryUsage bool
50+
51+
logger *xlog.Logger
52+
}
53+
54+
// DefaultConfig ...
55+
func DefaultConfig() *Config {
56+
return &Config{
57+
Host: flag.String("host"),
58+
Port: 9091,
59+
Debug: false,
60+
Deployment: constant.DefaultDeployment,
61+
SlowQueryThresholdInMilli: 500, // 500ms
62+
ReadBufferSize: 1024, // 1KB
63+
WriteBufferSize: 1024, // 1KB
64+
ReduceMemoryUsage: true,
65+
logger: xlog.JupiterLogger.With(xlog.FieldMod(ModName)),
66+
DisablePrintStack: false,
67+
EnableTLS: false,
68+
CertFile: "cert.pem",
69+
PrivateFile: "private.pem",
70+
}
71+
}
72+
73+
// StdConfig Jupiter Standard HTTP Server config
74+
func StdConfig(name string) *Config {
75+
return RawConfig("jupiter.server." + name)
76+
}
77+
78+
// RawConfig ...
79+
func RawConfig(key string) *Config {
80+
var config = DefaultConfig()
81+
if err := conf.UnmarshalKey(key, &config); err != nil &&
82+
errors.Cause(err) != conf.ErrInvalidKey {
83+
config.logger.Panic("http server parse config panic", xlog.FieldErrKind(ecode.ErrKindUnmarshalConfigErr), xlog.FieldErr(err), xlog.FieldKey(key), xlog.FieldValueAny(config))
84+
}
85+
return config
86+
}
87+
88+
// WithLogger ...
89+
func (config *Config) WithLogger(logger *xlog.Logger) *Config {
90+
config.logger = logger
91+
return config
92+
}
93+
94+
// WithHost ...
95+
func (config *Config) WithHost(host string) *Config {
96+
config.Host = host
97+
return config
98+
}
99+
100+
// WithPort ...
101+
func (config *Config) WithPort(port int) *Config {
102+
config.Port = port
103+
return config
104+
}
105+
106+
func (config *Config) MustBuild() *Server {
107+
server, err := config.Build()
108+
if err != nil {
109+
xlog.Panicf("build echo server failed: %v", err)
110+
}
111+
return server
112+
}
113+
114+
// Build create server instance, then initialize it with necessary interceptor
115+
func (config *Config) Build() (*Server, error) {
116+
server, err := newServer(config)
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
return server, nil
122+
}
123+
124+
// Address ...
125+
func (config *Config) Address() string {
126+
return fmt.Sprintf("%s:%d", config.Host, config.Port)
127+
}

pkg/server/xfasthttp/const.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2022 Douyu
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package xfasthttp
16+
17+
// Headers
18+
const (
19+
// HeaderAcceptEncoding ...
20+
HeaderAcceptEncoding = "Accept-Encoding"
21+
// HeaderContentType ...
22+
HeaderContentType = "Content-Type"
23+
// HRPC Errord
24+
HeaderHRPCErr = "HRPC-Errord"
25+
)
26+
27+
// MIME types
28+
const (
29+
// MIMEApplicationJSON ...
30+
MIMEApplicationJSON = "application/json"
31+
// MIMEApplicationJSONCharsetUTF8 ...
32+
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
33+
// MIMEApplicationProtobuf ...
34+
MIMEApplicationProtobuf = "application/protobuf"
35+
)
36+
const (
37+
charsetUTF8 = "charset=utf-8"
38+
)

pkg/server/xfasthttp/go.mod

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module github.com/douyu/jupiter/pkg/server/xfasthttp
2+
3+
go 1.17
4+
5+
replace github.com/douyu/jupiter => ../../../
6+
7+
require (
8+
github.com/douyu/jupiter v0.3.5
9+
github.com/pkg/errors v0.9.1
10+
github.com/valyala/fasthttp v1.33.0
11+
go.uber.org/zap v1.19.1
12+
)
13+
14+
require (
15+
github.com/BurntSushi/toml v0.3.1 // indirect
16+
github.com/andybalholm/brotli v1.0.4 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/golang/protobuf v1.5.2 // indirect
19+
github.com/json-iterator/go v1.1.11 // indirect
20+
github.com/klauspost/compress v1.14.1 // indirect
21+
github.com/mitchellh/mapstructure v1.3.2 // indirect
22+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
23+
github.com/modern-go/reflect2 v1.0.1 // indirect
24+
github.com/valyala/bytebufferpool v1.0.0 // indirect
25+
go.uber.org/atomic v1.7.0 // indirect
26+
go.uber.org/multierr v1.6.0 // indirect
27+
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
28+
google.golang.org/grpc v1.43.0 // indirect
29+
google.golang.org/protobuf v1.26.0 // indirect
30+
)

0 commit comments

Comments
 (0)