Skip to content

Commit

Permalink
Initialize single instance of Session.
Browse files Browse the repository at this point in the history
 - Create a single instance of Session instead of a new one with every request
   to prevent high disk I/O or getting throttled by AWS Metaservice.
  • Loading branch information
abutaha committed Nov 1, 2016
1 parent 8ba7b08 commit 49fedd0
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions aws-es-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type proxy struct {
Region string
Service string
Verbose bool
Signer *v4.Signer
}

func copyHeaders(dst, src http.Header) {
Expand Down Expand Up @@ -100,16 +101,9 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// Start AWS session from ENV, Shared Creds or EC2Role
sess, err := session.NewSession()
if err != nil {
log.Fatalln(err)
}

// Sign the request with AWSv4
signer := v4.NewSigner(sess.Config.Credentials)
payload := bytes.NewReader(replaceBody(req))
signer.Sign(req, payload, p.Service, p.Region, time.Now())
p.Signer.Sign(req, payload, p.Service, p.Region, time.Now())

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -157,7 +151,14 @@ func main() {
os.Exit(1)
}

mux := &proxy{Verbose: verbose}
// Start AWS session from ENV, Shared Creds or EC2Role
sess, err := session.NewSession()
if err != nil {
log.Fatalln(err)
}
signer := v4.NewSigner(sess.Config.Credentials)

mux := &proxy{Verbose: verbose, Signer: signer}
parseEndpoint(endpoint, mux)

fmt.Printf("Listening on %s\n", listenAddress)
Expand Down

0 comments on commit 49fedd0

Please sign in to comment.