Skip to content

Commit

Permalink
feat: add lock for interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Dec 14, 2023
1 parent 7c22753 commit b693660
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -40,6 +41,9 @@ type (
RequestInterceptors []RequestInterceptor
ResponseInterceptors []ResponseInterceptor

requestInterceptorsMu sync.RWMutex
responseInterceptorsMu sync.RWMutex

MaxBodyLength int
MaxRedirects int

Expand All @@ -65,6 +69,9 @@ type (
RequestInterceptors []RequestInterceptor
ResponseInterceptors []ResponseInterceptor

requestInterceptorsMu sync.Mutex
responseInterceptorsMu sync.Mutex

Body interface{}

MaxBodyLength int
Expand Down Expand Up @@ -277,6 +284,9 @@ func (rc *RequestConfig) mergeConfig(config *Config) *RequestConfig {

// AppendRequestInterceptors appends request interceptors to the interceptor list.
func (rc *RequestConfig) AppendRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig {
rc.requestInterceptorsMu.Lock()
defer rc.requestInterceptorsMu.Unlock()

if rc.RequestInterceptors == nil {
rc.RequestInterceptors = make([]RequestInterceptor, 0)
}
Expand All @@ -286,6 +296,9 @@ func (rc *RequestConfig) AppendRequestInterceptors(interceptors ...RequestInterc

// PrependRequestInterceptors prepends request interceptors to the interceptor list.
func (rc *RequestConfig) PrependRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig {
rc.requestInterceptorsMu.Lock()
defer rc.requestInterceptorsMu.Unlock()

if rc.RequestInterceptors == nil {
rc.RequestInterceptors = make([]RequestInterceptor, 0)
}
Expand All @@ -295,6 +308,9 @@ func (rc *RequestConfig) PrependRequestInterceptors(interceptors ...RequestInter

// invokeRequestInterceptors invokes all request interceptors with the provided configuration.
func (rc *RequestConfig) invokeRequestInterceptors(config *RequestConfig) (err error) {
rc.requestInterceptorsMu.Lock()
defer rc.requestInterceptorsMu.Unlock()

for _, fn := range rc.RequestInterceptors {
err = fn(config)
if err != nil {
Expand All @@ -306,6 +322,9 @@ func (rc *RequestConfig) invokeRequestInterceptors(config *RequestConfig) (err e

// AppendResponseInterceptors appends response interceptors to the interceptor list.
func (rc *RequestConfig) AppendResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig {
rc.responseInterceptorsMu.Lock()
defer rc.responseInterceptorsMu.Unlock()

if rc.ResponseInterceptors == nil {
rc.ResponseInterceptors = make([]ResponseInterceptor, 0)
}
Expand All @@ -315,6 +334,9 @@ func (rc *RequestConfig) AppendResponseInterceptors(interceptors ...ResponseInte

// PrependResponseInterceptors prepends response interceptors to the interceptor list.
func (rc *RequestConfig) PrependResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig {
rc.responseInterceptorsMu.Lock()
defer rc.responseInterceptorsMu.Unlock()

if rc.ResponseInterceptors == nil {
rc.ResponseInterceptors = make([]ResponseInterceptor, 0)
}
Expand All @@ -324,6 +346,9 @@ func (rc *RequestConfig) PrependResponseInterceptors(interceptors ...ResponseInt

// invokeResponseInterceptors invokes all response interceptors with the provided response.
func (rc *RequestConfig) invokeResponseInterceptors(resp *Response) (err error) {
rc.responseInterceptorsMu.Lock()
defer rc.responseInterceptorsMu.Unlock()

for _, fn := range rc.ResponseInterceptors {
err = fn(resp)
if err != nil {
Expand All @@ -335,6 +360,9 @@ func (rc *RequestConfig) invokeResponseInterceptors(resp *Response) (err error)

// invokeRequestInterceptors invokes all request interceptors with the provided configuration.
func (c *Config) invokeRequestInterceptors(config *RequestConfig) (err error) {
c.requestInterceptorsMu.Lock()
defer c.requestInterceptorsMu.Unlock()

for _, fn := range c.RequestInterceptors {
err = fn(config)
if err != nil {
Expand All @@ -346,6 +374,9 @@ func (c *Config) invokeRequestInterceptors(config *RequestConfig) (err error) {

// invokeResponseInterceptors invokes all response interceptors with the provided response.
func (c *Config) invokeResponseInterceptors(resp *Response) (err error) {
c.responseInterceptorsMu.Lock()
defer c.responseInterceptorsMu.Unlock()

for _, fn := range c.ResponseInterceptors {
err = fn(resp)
if err != nil {
Expand Down

0 comments on commit b693660

Please sign in to comment.