@@ -37,17 +37,27 @@ import (
37
37
"time"
38
38
)
39
39
40
+ var (
41
+ ErrUnsupportedTRuntimeType = errors .New ("unsupported runtime type" )
42
+ ErrUnsupportedTubeType = errors .New ("unsupported tube type" )
43
+ )
44
+
40
45
type Server struct {
41
46
options * serverOptions
42
47
httpSvr atomic.Pointer [http.Server ]
43
48
log * slog.Logger
44
49
Manager * fs.FunctionManager
45
50
}
46
51
52
+ type TubeLoaderType func (c * FactoryConfig ) (contube.TubeFactory , error )
53
+ type RuntimeLoaderType func (c * FactoryConfig ) (api.FunctionRuntimeFactory , error )
54
+
47
55
type serverOptions struct {
48
- httpListener net.Listener
49
- managerOpts []fs.ManagerOption
50
- httpTubeFact * contube.HttpTubeFactory
56
+ httpListener net.Listener
57
+ managerOpts []fs.ManagerOption
58
+ httpTubeFact * contube.HttpTubeFactory
59
+ tubeLoader TubeLoaderType
60
+ runtimeLoader RuntimeLoaderType
51
61
}
52
62
53
63
type ServerOption interface {
@@ -86,6 +96,24 @@ func WithHttpTubeFactory(factory *contube.HttpTubeFactory) ServerOption {
86
96
})
87
97
}
88
98
99
+ // WithTubeLoader sets the loader for the tube factory.
100
+ // This must be called before WithConfig.
101
+ func WithTubeLoader (loader TubeLoaderType ) ServerOption {
102
+ return serverOptionFunc (func (o * serverOptions ) (* serverOptions , error ) {
103
+ o .tubeLoader = loader
104
+ return o , nil
105
+ })
106
+ }
107
+
108
+ // WithRuntimeLoader sets the loader for the runtime factory.
109
+ // This must be called before WithConfig.
110
+ func WithRuntimeLoader (loader RuntimeLoaderType ) ServerOption {
111
+ return serverOptionFunc (func (o * serverOptions ) (* serverOptions , error ) {
112
+ o .runtimeLoader = loader
113
+ return o , nil
114
+ })
115
+ }
116
+
89
117
func getRefFactory (m map [string ]* FactoryConfig , name string , visited set.Set [string ]) (string , error ) {
90
118
if visited .Has (name ) {
91
119
return "" , errors .Errorf ("circular reference of factory %s" , name )
@@ -101,7 +129,7 @@ func getRefFactory(m map[string]*FactoryConfig, name string, visited set.Set[str
101
129
return name , nil
102
130
}
103
131
104
- func initFactories [T any ](m map [string ]* FactoryConfig , newFactory func (n string , c * FactoryConfig ) (T , error ), setup func (n string , f T )) error {
132
+ func initFactories [T any ](m map [string ]* FactoryConfig , newFactory func (c * FactoryConfig ) (T , error ), setup func (n string , f T )) error {
105
133
factoryMap := make (map [string ]T )
106
134
107
135
for name := range m {
@@ -114,7 +142,10 @@ func initFactories[T any](m map[string]*FactoryConfig, newFactory func(n string,
114
142
if ! exist {
115
143
return errors .Errorf ("factory %s not found, which the factory %s is pointed to" , refName , name )
116
144
}
117
- f , err := newFactory (refName , fc )
145
+ if fc .Type == nil {
146
+ return errors .Errorf ("factory %s type is not set" , refName )
147
+ }
148
+ f , err := newFactory (fc )
118
149
if err != nil {
119
150
return err
120
151
}
@@ -126,43 +157,40 @@ func initFactories[T any](m map[string]*FactoryConfig, newFactory func(n string,
126
157
return nil
127
158
}
128
159
160
+ func DefaultTubeLoader (c * FactoryConfig ) (contube.TubeFactory , error ) {
161
+ switch strings .ToLower (* c .Type ) {
162
+ case common .PulsarTubeType :
163
+ return contube .NewPulsarEventQueueFactory (context .Background (), contube .ConfigMap (* c .Config ))
164
+ case common .MemoryTubeType :
165
+ return contube .NewMemoryQueueFactory (context .Background ()), nil
166
+ }
167
+ return nil , errors .WithMessagef (ErrUnsupportedTubeType , "unsupported tube type :%s" , * c .Type )
168
+ }
169
+
170
+ func DefaultRuntimeLoader (c * FactoryConfig ) (api.FunctionRuntimeFactory , error ) {
171
+ switch strings .ToLower (* c .Type ) {
172
+ case WASMRuntime :
173
+ return wazero .NewWazeroFunctionRuntimeFactory (), nil
174
+ }
175
+ return nil , errors .WithMessagef (ErrUnsupportedTRuntimeType , "unsupported runtime type: %s" , * c .Type )
176
+ }
177
+
129
178
func WithConfig (config * Config ) ServerOption {
130
179
return serverOptionFunc (func (o * serverOptions ) (* serverOptions , error ) {
131
180
ln , err := net .Listen ("tcp" , config .ListenAddr )
132
181
if err != nil {
133
182
return nil , err
134
183
}
135
184
o .httpListener = ln
136
- err = initFactories [contube.TubeFactory ](config .TubeFactory , func (n string , c * FactoryConfig ) (contube.TubeFactory , error ) {
137
- if c .Type == nil {
138
- return nil , errors .Errorf ("tube factory %s type is not set" , n )
139
- }
140
- switch strings .ToLower (* c .Type ) {
141
- case common .PulsarTubeType :
142
- return contube .NewPulsarEventQueueFactory (context .Background (), contube .ConfigMap (* c .Config ))
143
- case common .MemoryTubeType :
144
- return contube .NewMemoryQueueFactory (context .Background ()), nil
145
- }
146
- return nil , errors .Errorf ("unsupported tube type %s" , * c .Type )
147
- }, func (n string , f contube.TubeFactory ) {
185
+ err = initFactories [contube.TubeFactory ](config .TubeFactory , o .tubeLoader , func (n string , f contube.TubeFactory ) {
148
186
o .managerOpts = append (o .managerOpts , fs .WithTubeFactory (n , f ))
149
187
})
150
188
if err != nil {
151
189
return nil , err
152
190
}
153
- err = initFactories [api.FunctionRuntimeFactory ](config .RuntimeFactory ,
154
- func (n string , c * FactoryConfig ) (api.FunctionRuntimeFactory , error ) {
155
- if c .Type == nil {
156
- return nil , errors .Errorf ("runtime factory %s type is not set" , n )
157
- }
158
- switch strings .ToLower (* c .Type ) {
159
- case WASMRuntime :
160
- return wazero .NewWazeroFunctionRuntimeFactory (), nil
161
- }
162
- return nil , errors .Errorf ("unsupported runtime type %s" , * c .Type )
163
- }, func (n string , f api.FunctionRuntimeFactory ) {
164
- o .managerOpts = append (o .managerOpts , fs .WithRuntimeFactory (n , f ))
165
- })
191
+ err = initFactories [api.FunctionRuntimeFactory ](config .RuntimeFactory , o .runtimeLoader , func (n string , f api.FunctionRuntimeFactory ) {
192
+ o .managerOpts = append (o .managerOpts , fs .WithRuntimeFactory (n , f ))
193
+ })
166
194
if err != nil {
167
195
return nil , err
168
196
}
@@ -178,6 +206,8 @@ func NewServer(opts ...ServerOption) (*Server, error) {
178
206
fs .WithTubeFactory ("http" , httpTubeFact ),
179
207
}
180
208
options .httpTubeFact = httpTubeFact
209
+ options .tubeLoader = DefaultTubeLoader
210
+ options .runtimeLoader = DefaultRuntimeLoader
181
211
for _ , o := range opts {
182
212
if o == nil {
183
213
continue
0 commit comments