Commit 45477526 authored by Elvis Pranskevichus's avatar Elvis Pranskevichus

Fix gohttp performance

parent 26ae4b8f
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
) )
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
var cache *lru.Cache var cache *lru.Cache
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
var resp string var resp []byte
respSize := r.URL.Path[1:] respSize := r.URL.Path[1:]
s, err := strconv.Atoi(respSize) s, err := strconv.Atoi(respSize)
...@@ -22,13 +22,13 @@ func handler(w http.ResponseWriter, r *http.Request) { ...@@ -22,13 +22,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
val, ok := cache.Get(s) val, ok := cache.Get(s)
if ok { if ok {
resp = val.(string) resp = val.([]byte)
} else { } else {
resp = strings.Repeat("X", s) resp = bytes.Repeat([]byte{'X'}, s)
cache.Add(s, resp) cache.Add(s, resp)
} }
fmt.Fprintf(w, resp) w.Write(resp)
} }
func main() { func main() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment