How to Set Up the InProxy Component of FastProxy: A Complete Guide
To set up the InProxy component of FastProxy, create a YAML configuration file, load it using inconfig.LoadYamlConfig, and start the server with inproxy.NewServer.
The InProxy component serves as the entry point for the FastProxy open-source project (kingson4wu/fast_proxy), handling inbound traffic, enforcing security policies, and forwarding requests to downstream services. This guide walks through the complete setup process using the actual source code structure, from configuration to running the binary.
Understanding the InProxy Architecture
Before diving into setup steps, it helps to understand how InProxy is structured. The component is organized into three distinct layers:
- Server bootstrap (
inproxy/inproxy.go): Creates the HTTP server, loads configuration, and registers the request handler viainproxy.NewServer. - Configuration (
inproxy/inconfig/yaml_config.go): Parses YAML configuration including service-call policies, encryption settings, and HTTP client limits viainconfig.LoadYamlConfig. - Proxy engine (
inproxy/internal/proxy/httpclientProxy.go): Validates requests, applies rate limits, builds outbound requests (using standardnet/httpor FastHTTP), and streams responses back viaproxy.DoProxy.
Prerequisites and Configuration Setup
Creating the YAML Configuration File
The InProxy component requires a YAML configuration file that defines routing, security policies, and performance tuning parameters. Create a config.yaml file with the following structure:
proxy:
forwardAddress: http://127.0.0.1:9833/inProxy
application:
name: in_proxy
port: 8033 # Server listens on this port
contextPath: /inProxy # Request path prefix
rpc:
serviceHeaderName: C_ServiceName
serviceConfig:
song_service:
encryptKeyName: encrypt.key.room.v2
signKeyName: sign.key.room.v1
encryptEnable: true
signEnable: true
compressEnable: true
signKeyConfig:
sign.key.room.v1: abcd
sign.key.room.v2: abcd
encryptKeyConfig:
encrypt.key.room.v1: ABCDABCDABCDABCDW
encrypt.key.room.v2: ABCDABCDABCDABCD
serviceCallTypeConfig:
song_service:
/token_service/api/service:
callType: 1
qps: 10
chat_service:
/search_service/api/service:
callType: 1
qps: 20000000000
httpClient:
MaxIdleConns: 5000
MaxIdleConnsPerHost: 3000
fastHttp:
enable: true # Toggles FastHTTP fast path
This configuration file, located at examples/inproxy/config.yaml in the repository, defines the forwarding address, port, encryption keys, rate limits (QPS), and HTTP client pooling settings.
Loading Configuration and Initializing Components
Loading the YAML Config
Use the inconfig package to parse the YAML file into a configuration object that implements the inconfig.Config interface. This interface exposes methods required by the proxy core, such as ServerContextPath() and ContainsCallPrivilege().
package main
import (
"embed"
"fmt"
"github.com/kingson4wu/fast_proxy/inproxy"
"github.com/kingson4wu/fast_proxy/inproxy/inconfig"
"os"
)
//go:embed *
var ConfigFs embed.FS
func main() {
// Load raw YAML bytes from the embedded file system
data, err := ConfigFs.ReadFile("config.yaml")
if err != nil {
fmt.Printf("cannot read config: %v\n", err)
os.Exit(1)
}
// Parse into an inconfig.Config implementation
cfg := inconfig.LoadYamlConfig(data)
// cfg now satisfies the methods used by the proxy core
}
The LoadYamlConfig function, defined in inproxy/inconfig/yaml_config.go, handles the unmarshaling of all configuration sections including service policies and HTTP client limits.
Optional Service Center Setup
For dynamic service discovery, FastProxy provides a center package that supplies a service registry. While optional, this enables runtime service location rather than static forwarding addresses.
import "github.com/kingson4wu/fast_proxy/examples/center"
// The center reads the RPC header name from the config
sc := center.GetSC(func() string { return cfg.ServiceRpcHeaderName() })
This creates a service center instance that inproxy.NewServer can use to resolve service names to addresses dynamically.
Starting the InProxy Server
Initialize and start the server using inproxy.NewServer, which wires together the configuration, HTTP client, and request handler. The function accepts optional server options, such as the service center.
import (
"github.com/kingson4wu/fast_proxy/common/server"
"github.com/kingson4wu/fast_proxy/inproxy"
)
func main() {
// ...load cfg and sc as shown above...
// NewServer wires everything together and starts listening.
// The optional WithServiceCenter option injects the dynamic registry.
inproxy.NewServer(cfg, server.WithServiceCenter(sc))
}
Internally, inproxy.NewServer (located in inproxy/inproxy.go) performs the following initialization sequence:
- Registers configuration: Calls
inconfig.Read(c)to set the config globally. - Builds HTTP client: Invokes
proxy.BuildClient(c)using limits fromc.HttpClientMaxIdleConns()andc.HttpClientMaxIdleConnsPerHost(). - Creates server instance: Instantiates
common/server.Serverwith therequestProxyhandler. - Routes requests: The
requestProxyhandler delegates toproxy.DoProxy, where validation, rate-limiting, timeout handling, and request forwarding occur.
Building and Running the Binary
Compile and execute the InProxy component using standard Go build commands. The example in examples/inproxy provides a ready-to-run implementation.
# Build the example
go build -o in-proxy ./examples/inproxy
# Run the binary
./in-proxy
Upon startup, the server prints the banner (from banner.txt) and logs the listening address. By default, it accepts requests on 0.0.0.0:8033 (as configured in application.port) and forwards them to the proxy.forwardAddress after applying the configured security policies and rate limits.
Summary
- InProxy is the inbound traffic gateway of FastProxy that handles request validation, encryption, rate limiting, and forwarding.
- Configuration is YAML-based and loaded via
inconfig.LoadYamlConfig, supporting service-specific policies, QPS limits, and HTTP client tuning. - Server initialization uses
inproxy.NewServer, which internally callsproxy.BuildClientand registers therequestProxyhandler that delegates toproxy.DoProxy. - Optional service discovery can be enabled via
server.WithServiceCenterfor dynamic service resolution. - Complete working example is available in
examples/inproxy, demonstrating embedded config loading and server startup.
Frequently Asked Questions
What is the primary role of the InProxy component in FastProxy?
The InProxy component acts as the entry point for inbound traffic in the FastProxy architecture. It receives incoming HTTP requests, enforces security policies such as encryption and signing, applies traffic governance rules including rate limiting and timeouts, and forwards validated requests to downstream services.
How does InProxy handle configuration loading?
InProxy uses the inconfig package to parse YAML configuration files. The inconfig.LoadYamlConfig function unmarshals the YAML into a configuration object that implements the inconfig.Config interface. This interface provides methods like ServerContextPath() and ContainsCallPrivilege() that the proxy core uses to determine routing and access control.
Can I use FastHTTP instead of standard net/http with InProxy?
Yes, InProxy supports FastHTTP as an alternative to the standard net/http client. You can enable this by setting fastHttp.enable: true in your YAML configuration file. When enabled, the proxy engine uses the FastHTTP implementation for request forwarding, which can offer performance benefits in high-throughput scenarios.
Where can I find a complete working example of InProxy setup?
A complete, runnable example is provided in the examples/inproxy directory of the kingson4wu/fast_proxy repository. This example includes a server.go file that demonstrates embedded configuration loading, optional service center integration, and server startup, along with a sample config.yaml file showing all available configuration options.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →