Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Feb 15, 2019. It is now read-only.
/oxyPublic archive
forked fromvulcand/oxy

Go middlewares for HTTP servers & proxies

License

NotificationsYou must be signed in to change notification settings

containous/oxy

 
 

Repository files navigation

Oxy is a Go library with HTTP handlers that enhance HTTP standard library:

  • Stream retries and buffers requests and responses
  • Forward forwards requests to remote location and rewrites headers
  • Roundrobin is a round-robin load balancer
  • Circuit Breaker Hystrix-style circuit breaker
  • Connlimit Simultaneous connections limiter
  • Ratelimit Rate limiter (based on tokenbucket algo)
  • Trace Structured request and response logger

It is designed to be fully compatible with http standard library, easy to customize and reuse.

Status

  • Initial design is completed
  • Covered by tests
  • Used as a reverse proxy engine inVulcand

Quickstart

Every handler ishttp.Handler, so writing and plugging in a middleware is easy. Let us write a simple reverse proxy as an example:

Simple reverse proxy

import ("net/http""github.com/vulcand/oxy/forward""github.com/vulcand/oxy/testutils"  )// Forwards incoming requests to whatever location URL points to, adds proper forwarding headersfwd,_:=forward.New()redirect:=http.HandlerFunc(func(w http.ResponseWriter,req*http.Request) {// let us forward this request to another serverreq.URL=testutils.ParseURI("http://localhost:63450")fwd.ServeHTTP(w,req)})// that's it! our reverse proxy is ready!s:=&http.Server{Addr:":8080",Handler:redirect,}s.ListenAndServe()

As a next step, let us add a round robin load-balancer:

import ("net/http""github.com/vulcand/oxy/forward""github.com/vulcand/oxy/roundrobin"  )// Forwards incoming requests to whatever location URL points to, adds proper forwarding headersfwd,_:=forward.New()lb,_:=roundrobin.New(fwd)lb.UpsertServer(url1)lb.UpsertServer(url2)s:=&http.Server{Addr:":8080",Handler:lb,}s.ListenAndServe()

What if we want to handle retries and replay the request in case of errors?stream handler will help:

import ("net/http""github.com/vulcand/oxy/forward""github.com/vulcand/oxy/roundrobin"  )// Forwards incoming requests to whatever location URL points to, adds proper forwarding headersfwd,_:=forward.New()lb,_:=roundrobin.New(fwd)// stream will read the request body and will replay the request again in case if forward returned status// corresponding to nework error (e.g. Gateway Timeout)stream,_:=stream.New(lb,stream.Retry(`IsNetworkError() && Attempts() < 2`))lb.UpsertServer(url1)lb.UpsertServer(url2)// that's it! our reverse proxy is ready!s:=&http.Server{Addr:":8080",Handler:stream,}s.ListenAndServe()

About

Go middlewares for HTTP servers & proxies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go99.8%
  • Makefile0.2%

[8]ページ先頭

©2009-2025 Movatter.jp