compile.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package billingexpr
  2. import (
  3. "fmt"
  4. "math"
  5. "sync"
  6. "github.com/expr-lang/expr"
  7. "github.com/expr-lang/expr/vm"
  8. )
  9. const maxCacheSize = 256
  10. var (
  11. cacheMu sync.RWMutex
  12. cache = make(map[string]*vm.Program, 64)
  13. )
  14. // compileEnvPrototype is the type-checking prototype used at compile time.
  15. // It declares the shape of the environment that RunExpr will provide.
  16. // The tier() function is a no-op placeholder here; the real one with
  17. // side-channel tracing is injected at runtime.
  18. var compileEnvPrototype = map[string]interface{}{
  19. "p": float64(0),
  20. "c": float64(0),
  21. "cr": float64(0),
  22. "cc": float64(0),
  23. "cc1h": float64(0),
  24. "prompt_tokens": float64(0),
  25. "completion_tokens": float64(0),
  26. "cache_read_tokens": float64(0),
  27. "cache_create_tokens": float64(0),
  28. "cache_create_1h_tokens": float64(0),
  29. "img": float64(0),
  30. "ai": float64(0),
  31. "ao": float64(0),
  32. "image_tokens": float64(0),
  33. "audio_input_tokens": float64(0),
  34. "audio_output_tokens": float64(0),
  35. "tier": func(string, float64) float64 { return 0 },
  36. "header": func(string) string { return "" },
  37. "param": func(string) interface{} { return nil },
  38. "has": func(interface{}, string) bool { return false },
  39. "hour": func(string) int { return 0 },
  40. "minute": func(string) int { return 0 },
  41. "weekday": func(string) int { return 0 },
  42. "month": func(string) int { return 0 },
  43. "day": func(string) int { return 0 },
  44. "max": math.Max,
  45. "min": math.Min,
  46. "abs": math.Abs,
  47. "ceil": math.Ceil,
  48. "floor": math.Floor,
  49. }
  50. // CompileFromCache compiles an expression string, using a cached program when
  51. // available. The cache is keyed by the SHA-256 hex digest of the expression.
  52. func CompileFromCache(exprStr string) (*vm.Program, error) {
  53. return compileFromCacheByHash(exprStr, ExprHashString(exprStr))
  54. }
  55. // CompileFromCacheByHash is like CompileFromCache but accepts a pre-computed
  56. // hash, useful when the caller already has the BillingSnapshot.ExprHash.
  57. func CompileFromCacheByHash(exprStr, hash string) (*vm.Program, error) {
  58. return compileFromCacheByHash(exprStr, hash)
  59. }
  60. func compileFromCacheByHash(exprStr, hash string) (*vm.Program, error) {
  61. cacheMu.RLock()
  62. if prog, ok := cache[hash]; ok {
  63. cacheMu.RUnlock()
  64. return prog, nil
  65. }
  66. cacheMu.RUnlock()
  67. prog, err := expr.Compile(exprStr, expr.Env(compileEnvPrototype), expr.AsFloat64())
  68. if err != nil {
  69. return nil, fmt.Errorf("expr compile error: %w", err)
  70. }
  71. cacheMu.Lock()
  72. if len(cache) >= maxCacheSize {
  73. cache = make(map[string]*vm.Program, 64)
  74. }
  75. cache[hash] = prog
  76. cacheMu.Unlock()
  77. return prog, nil
  78. }
  79. // InvalidateCache clears the compiled-expression cache.
  80. // Called when billing rules are updated.
  81. func InvalidateCache() {
  82. cacheMu.Lock()
  83. cache = make(map[string]*vm.Program, 64)
  84. cacheMu.Unlock()
  85. }