This may seem stupid but I am totally new to this library and am starting to get frustrated after trying to properly reconfigure the base "helloworld" API example to increase the max buffer sizes:
package main
import (
"flag"
"fmt"
"log"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)
func main() {
flag.Parse()
h := requestHandler
if *compress {
h = fasthttp.CompressHandler(h)
}
if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!\n\n")
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
ctx.SetContentType("text/plain; charset=utf8")
// Set arbitrary headers
ctx.Response.Header.Set("X-My-Header", "my-header-value")
// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
}
I know I should be able to add a sort of
Server{
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
ReadBufferSize: 16000,
WriteBufferSize: 16000,
}
But every location and way I have tried doing so is wrong. Can anyone that has played with this library before help me out and give me the snippit I need to accomplish this within this example? Default buffer for header is 4096 and thats just not big enough.
Would appreciate the help!
-Jeremy
checkout the code like this
listener, err = reuseport.Listen("tcp4", addr)
// for windows
//
if err != nil {
log.Info().Str("working in windows", addr)
listener, err1 = net.Listen("tcp", addr)
if err1 != nil {
log.Fatal().Err(err1)
}
}
go func() {
// fasthttp server setting here
s := &fasthttp.Server{
Handler: router.ServeFastHTTP,
Name: ServerName,
ReadBufferSize: BufferSize,
MaxConnsPerIP: 10,
MaxRequestsPerConn: 10,
MaxRequestBodySize: 1024 * 4, // MaxRequestBodySize: 100<<20, // 100MB
Concurrency: MaxFttpConnect,
Logger: NewFastHTTPLoggerAdapter(log),
}
if err = s.Serve(listener); err != nil {
log.Panic().Err(err)
}
}()
select{}
inside
router.ServeFastHTTP,
is func(ctx *fasthttp.RequestCtx)
.
@tsingson the syntax there makes sense and I have been trying that with the helloworld one. Will post back if I get it cracked.. I tried another attempted based on a gzip one I saw by @erikdubbelboer like so too but it did not work:
package main
import (
"fmt"
"log"
"github.com/valyala/fasthttp"
)
func server() {
server := fasthttp.Server{
Name: "Fasthttp server",
Handler: handler,
ReduceMemoryUsage: true,
ReadBufferSize: 16000,
WriteBufferSize: 16000,
}
log.Fatal(server.ListenAndServe(":8080"))
}
func handler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!\n\n")
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
ctx.SetContentType("text/plain; charset=utf8")
}
func main() {
go server()
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
req.Header.Add("Accept-Encoding", "gzip")
req.SetRequestURI("http://localhost:8080")
err := fasthttp.Do(req, res)
if err != nil {
panic(err)
}
}
Compiles and ./helloworldserver on this ran but it instantly closes like nothing works.
package main
import (
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/valyala/fasthttp"
)
type (
// FastHTTPLoggerAdapter for zerolog
FastHTTPLoggerAdapter struct {
ZeroLogger zerolog.Logger
fasthttp.Logger
}
)
var (
log zerolog.Logger
err error
)
func init() {
log = zerolog.New(os.Stderr).With().Timestamp().Logger()
}
func main() {
/**
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
req.Header.Add("Accept-Encoding", "gzip")
req.SetRequestURI("http://localhost:8080")
err := fasthttp.HostClient{}(req, res)
if err != nil {
panic(err)
}
*/
server := fasthttp.Server{
Name: "Fasthttp server",
Handler: handler,
ReduceMemoryUsage: true,
ReadBufferSize: 16000,
WriteBufferSize: 16000,
MaxConnsPerIP: 10,
MaxRequestsPerConn: 10,
MaxRequestBodySize: 1024 * 4, // MaxRequestBodySize: 100<<20, // 100MB
Logger: NewFastHTTPLoggerAdapter(log),
}
err = server.ListenAndServe(":8080")
if err != nil {
log.Panic().Err(err)
}
}
func handler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!\n\n")
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
ctx.SetContentType("text/plain; charset=utf8")
}
// NewFastHTTPLoggerAdapter create new *FastHTTPLoggerAdapter
func NewFastHTTPLoggerAdapter(logger zerolog.Logger) (fasthttplogger *FastHTTPLoggerAdapter) {
fasthttplogger = &FastHTTPLoggerAdapter{
ZeroLogger: logger,
}
return fasthttplogger
}
a working sample
good luck.
.
@tsingson Thank you so much. It is great people like you that make the open source community awesome
to work with. I appreciate the help, and enjoy your weekend buddy!
@jeremyjpj0916 you r welcome.
i love this fasthttp , it's great.
thanks @valyala
Most helpful comment
@jeremyjpj0916 you r welcome.
i love this fasthttp , it's great.
thanks @valyala