|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/TykTechnologies/tyk/apidef/oas" |
| 6 | + "hash/fnv" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + ErrTrafficShapingRejected = errors.New("request rejected by traffic shaping rules") |
| 12 | +) |
| 13 | + |
| 14 | +// TrafficShapingMiddleware controls traffic based on configured rules |
| 15 | +type TrafficShapingMiddleware struct { |
| 16 | + *BaseMiddleware |
| 17 | +} |
| 18 | + |
| 19 | +func (t *TrafficShapingMiddleware) Name() string { |
| 20 | + return "TrafficShapingMiddleware" |
| 21 | +} |
| 22 | + |
| 23 | +// EnabledForSpec checks if any operation has traffic shaping enabled |
| 24 | +func (t *TrafficShapingMiddleware) EnabledForSpec() bool { |
| 25 | + if ext := t.Spec.GetTykExtension(); ext != nil && ext.Middleware != nil { |
| 26 | + for _, op := range ext.Middleware.Operations { |
| 27 | + if op.TrafficShaping != nil && op.TrafficShaping.Enabled { |
| 28 | + return true |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return false |
| 33 | +} |
| 34 | + |
| 35 | +// ProcessRequest implements the traffic shaping logic |
| 36 | +func (t *TrafficShapingMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) { |
| 37 | + // Find operation config for this request |
| 38 | + op := t.Spec.findOperation(r) |
| 39 | + if op == nil || op.TrafficShaping == nil || !op.TrafficShaping.Enabled { |
| 40 | + return nil, http.StatusOK |
| 41 | + } |
| 42 | + |
| 43 | + // Get routing value based on config |
| 44 | + routingKey := t.getRoutingValue(r, op.TrafficShaping.ConsistentRouting) |
| 45 | + |
| 46 | + // Check if request should be allowed |
| 47 | + if !t.isAllowed(routingKey, op.TrafficShaping.Percentage) { |
| 48 | + if altEndpoint := op.TrafficShaping.AlternativeEndpoint; altEndpoint != "" { |
| 49 | + http.Redirect(w, r, altEndpoint, http.StatusTemporaryRedirect) |
| 50 | + return nil, http.StatusTemporaryRedirect |
| 51 | + } |
| 52 | + return ErrTrafficShapingRejected, http.StatusTooManyRequests |
| 53 | + } |
| 54 | + |
| 55 | + return nil, http.StatusOK |
| 56 | +} |
| 57 | + |
| 58 | +// getRoutingValue extracts a consistent routing value from the request |
| 59 | +func (t *TrafficShapingMiddleware) getRoutingValue(r *http.Request, routing *oas.ConsistentRouting) string { |
| 60 | + if routing == nil { |
| 61 | + return r.RemoteAddr // Use IP as default |
| 62 | + } |
| 63 | + |
| 64 | + // Try header first |
| 65 | + if routing.HeaderName != "" { |
| 66 | + if val := r.Header.Get(routing.HeaderName); val != "" { |
| 67 | + return val |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Try query param |
| 72 | + if routing.QueryName != "" { |
| 73 | + if val := r.URL.Query().Get(routing.QueryName); val != "" { |
| 74 | + return val |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return r.RemoteAddr // Fallback to IP |
| 79 | +} |
| 80 | + |
| 81 | +// isAllowed determines if the request should be allowed through |
| 82 | +func (t *TrafficShapingMiddleware) isAllowed(routingKey string, percentage int) bool { |
| 83 | + if percentage >= 100 { |
| 84 | + return true |
| 85 | + } |
| 86 | + if percentage <= 0 { |
| 87 | + return false |
| 88 | + } |
| 89 | + |
| 90 | + // Hash the routing key for consistent distribution |
| 91 | + h := fnv.New32a() |
| 92 | + h.Write([]byte(routingKey)) |
| 93 | + hash := h.Sum32() |
| 94 | + |
| 95 | + // Map to 0-99 range |
| 96 | + bucket := hash % 100 |
| 97 | + return int(bucket) < percentage |
| 98 | +} |
0 commit comments