Best Selenoid code snippet using service.getMemory
imports.go
Source:imports.go
1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17package v218import "mosn.io/proxy-wasm-go-host/proxywasm/common"19func RegisterImports(instance common.WasmInstance) {20 _ = instance.RegisterFunc("env", "proxy_log", ProxyLog)21 _ = instance.RegisterFunc("env", "proxy_set_effective_context", ProxySetEffectiveContext)22 _ = instance.RegisterFunc("env", "proxy_context_finalize", ProxyContextFinalize)23 _ = instance.RegisterFunc("env", "proxy_resume_stream", ProxyResumeStream)24 _ = instance.RegisterFunc("env", "proxy_close_stream", ProxyCloseStream)25 _ = instance.RegisterFunc("env", "proxy_send_http_response", ProxySendHttpResponse)26 _ = instance.RegisterFunc("env", "proxy_resume_http_stream", ProxyResumeHttpStream)27 _ = instance.RegisterFunc("env", "proxy_close_http_stream", ProxyCloseHttpStream)28 _ = instance.RegisterFunc("env", "proxy_get_buffer", ProxyGetBuffer)29 _ = instance.RegisterFunc("env", "proxy_set_buffer", ProxySetBuffer)30 _ = instance.RegisterFunc("env", "proxy_get_map_values", ProxyGetMapValues)31 _ = instance.RegisterFunc("env", "proxy_set_map_values", ProxySetMapValues)32 _ = instance.RegisterFunc("env", "proxy_open_shared_kvstore", ProxyOpenSharedKvstore)33 _ = instance.RegisterFunc("env", "proxy_get_shared_kvstore_key_values", ProxyGetSharedKvstoreKeyValues)34 _ = instance.RegisterFunc("env", "proxy_set_shared_kvstore_key_values", ProxySetSharedKvstoreKeyValues)35 _ = instance.RegisterFunc("env", "proxy_add_shared_kvstore_key_values", ProxyAddSharedKvstoreKeyValues)36 _ = instance.RegisterFunc("env", "proxy_remove_shared_kvstore_key", ProxyRemoveSharedKvstoreKey)37 _ = instance.RegisterFunc("env", "proxy_delete_shared_kvstore", ProxyDeleteSharedKvstore)38 _ = instance.RegisterFunc("env", "proxy_open_shared_queue", ProxyOpenSharedQueue)39 _ = instance.RegisterFunc("env", "proxy_dequeue_shared_queue_item", ProxyDequeueSharedQueueItem)40 _ = instance.RegisterFunc("env", "proxy_enqueue_shared_queue_item", ProxyEnqueueSharedQueueItem)41 _ = instance.RegisterFunc("env", "proxy_delete_shared_queue", ProxyDeleteSharedQueue)42 _ = instance.RegisterFunc("env", "proxy_create_timer", ProxyCreateTimer)43 _ = instance.RegisterFunc("env", "proxy_delete_timer", ProxyDeleteTimer)44 _ = instance.RegisterFunc("env", "proxy_create_metric", ProxyCreateMetric)45 _ = instance.RegisterFunc("env", "proxy_get_metric_value", ProxyGetMetricValue)46 _ = instance.RegisterFunc("env", "proxy_set_metric_value", ProxySetMetricValue)47 _ = instance.RegisterFunc("env", "proxy_increment_metric_value", ProxyIncrementMetricValue)48 _ = instance.RegisterFunc("env", "proxy_delete_metric", ProxyDeleteMetric)49 _ = instance.RegisterFunc("env", "proxy_dispatch_http_call", ProxyDispatchHttpCall)50 _ = instance.RegisterFunc("env", "proxy_dispatch_grpc_call", ProxyDispatchGrpcCall)51 _ = instance.RegisterFunc("env", "proxy_open_grpc_stream", ProxyOpenGrpcStream)52 _ = instance.RegisterFunc("env", "proxy_send_grpc_stream_message", ProxySendGrpcStreamMessage)53 _ = instance.RegisterFunc("env", "proxy_cancel_grpc_call", ProxyCancelGrpcCall)54 _ = instance.RegisterFunc("env", "proxy_close_grpc_call", ProxyCloseGrpcCall)55 _ = instance.RegisterFunc("env", "proxy_call_custom_function", ProxyCallCustomFunction)56}57func ProxyLog(instance common.WasmInstance, logLevel LogLevel, messageData int32, messageSize int32) Result {58 msg, err := instance.GetMemory(uint64(messageData), uint64(messageSize))59 if err != nil {60 return ResultInvalidMemoryAccess61 }62 callback := getImportHandler(instance)63 return callback.Log(logLevel, string(msg))64}65func ProxySetEffectiveContext(instance common.WasmInstance, contextID int32) Result {66 callback := getImportHandler(instance)67 return callback.SetEffectiveContext(contextID)68}69func ProxyContextFinalize(instance common.WasmInstance) Result {70 callback := getImportHandler(instance)71 return callback.ContextFinalize()72}73func ProxyResumeStream(instance common.WasmInstance, streamType StreamType) Result {74 callback := getImportHandler(instance)75 switch streamType {76 case StreamTypeDownstream:77 return callback.ResumeDownStream()78 case StreamTypeUpstream:79 return callback.ResumeUpStream()80 case StreamTypeHttpRequest:81 return callback.ResumeHttpRequest()82 case StreamTypeHttpResponse:83 return callback.ResumeHttpResponse()84 default:85 return callback.ResumeCustomStream(streamType)86 }87}88func ProxyCloseStream(instance common.WasmInstance, streamType StreamType) Result {89 callback := getImportHandler(instance)90 switch streamType {91 case StreamTypeDownstream:92 return callback.CloseDownStream()93 case StreamTypeUpstream:94 return callback.CloseUpStream()95 case StreamTypeHttpRequest:96 return callback.CloseHttpRequest()97 case StreamTypeHttpResponse:98 return callback.CloseHttpResponse()99 default:100 return callback.CloseCustomStream(streamType)101 }102}103func ProxySendHttpResponse(instance common.WasmInstance, responseCode int32, responseCodeDetailsData int32, responseCodeDetailsSize int32,104 responseBodyData int32, responseBodySize int32, additionalHeadersMapData int32, additionalHeadersSize int32,105 grpcStatus int32) Result {106 respCodeDetail, err := instance.GetMemory(uint64(responseCodeDetailsData), uint64(responseCodeDetailsSize))107 if err != nil {108 return ResultInvalidMemoryAccess109 }110 respBody, err := instance.GetMemory(uint64(responseBodyData), uint64(responseBodySize))111 if err != nil {112 return ResultInvalidMemoryAccess113 }114 additionalHeaderMapData, err := instance.GetMemory(uint64(additionalHeadersMapData), uint64(additionalHeadersSize))115 if err != nil {116 return ResultInvalidMemoryAccess117 }118 additionalHeaderMap := common.DecodeMap(additionalHeaderMapData)119 callback := getImportHandler(instance)120 return callback.SendHttpResp(responseCode,121 common.NewIoBufferBytes(respCodeDetail),122 common.NewIoBufferBytes(respBody),123 common.CommonHeader(additionalHeaderMap), grpcStatus)124}125func ProxyResumeHttpStream(instance common.WasmInstance, streamType StreamType) Result {126 callback := getImportHandler(instance)127 switch streamType {128 case StreamTypeHttpRequest:129 return callback.ResumeHttpRequest()130 case StreamTypeHttpResponse:131 return callback.ResumeHttpResponse()132 }133 return ResultBadArgument134}135func ProxyCloseHttpStream(instance common.WasmInstance, streamType StreamType) Result {136 callback := getImportHandler(instance)137 switch streamType {138 case StreamTypeHttpRequest:139 return callback.CloseHttpRequest()140 case StreamTypeHttpResponse:141 return callback.CloseHttpResponse()142 }143 return ResultBadArgument144}145func GetBuffer(instance common.WasmInstance, bufferType BufferType) common.IoBuffer {146 im := getImportHandler(instance)147 switch bufferType {148 case BufferTypeHttpRequestBody:149 return im.GetHttpRequestBody()150 case BufferTypeHttpResponseBody:151 return im.GetHttpResponseBody()152 case BufferTypeDownstreamData:153 return im.GetDownStreamData()154 case BufferTypeUpstreamData:155 return im.GetUpstreamData()156 case BufferTypeHttpCalloutResponseBody:157 return im.GetHttpCalloutResponseBody()158 case BufferTypePluginConfiguration:159 return im.GetPluginConfig()160 case BufferTypeVmConfiguration:161 return im.GetVmConfig()162 default:163 return im.GetCustomBuffer(bufferType)164 }165}166func ProxyGetBuffer(instance common.WasmInstance, bufferType BufferType, offset int32, maxSize int32,167 returnBufferData int32, returnBufferSize int32) Result {168 buf := GetBuffer(instance, bufferType)169 if buf == nil {170 return ResultBadArgument171 }172 if buf.Len() == 0 {173 return ResultEmpty174 }175 if offset > offset+maxSize {176 return ResultBadArgument177 }178 if offset+maxSize > int32(buf.Len()) {179 maxSize = int32(buf.Len()) - offset180 }181 return copyIntoInstance(instance, buf.Bytes()[offset:offset+maxSize], returnBufferData, returnBufferSize)182}183func ProxySetBuffer(instance common.WasmInstance, bufferType BufferType, offset int32, size int32,184 bufferData int32, bufferSize int32) Result {185 buf := GetBuffer(instance, bufferType)186 if buf == nil {187 return ResultBadArgument188 }189 content, err := instance.GetMemory(uint64(bufferData), uint64(bufferSize))190 if err != nil {191 return ResultInvalidMemoryAccess192 }193 if offset == 0 {194 if size == 0 || int(size) >= buf.Len() {195 buf.Drain(buf.Len())196 _, err = buf.Write(content)197 } else {198 return ResultBadArgument199 }200 } else if int(offset) >= buf.Len() {201 _, err = buf.Write(content)202 } else {203 return ResultBadArgument204 }205 if err != nil {206 return ResultInvalidMemoryAccess207 }208 return ResultOk209}210func GetMap(instance common.WasmInstance, mapType MapType) common.HeaderMap {211 ctx := getImportHandler(instance)212 switch mapType {213 case MapTypeHttpRequestHeaders:214 return ctx.GetHttpRequestHeader()215 case MapTypeHttpRequestTrailers:216 return ctx.GetHttpRequestTrailer()217 case MapTypeHttpRequestMetadata:218 return ctx.GetHttpRequestMetadata()219 case MapTypeHttpResponseHeaders:220 return ctx.GetHttpResponseHeader()221 case MapTypeHttpResponseTrailers:222 return ctx.GetHttpResponseTrailer()223 case MapTypeHttpResponseMetadata:224 return ctx.GetHttpResponseMetadata()225 case MapTypeHttpCallResponseHeaders:226 return ctx.GetHttpCallResponseHeaders()227 case MapTypeHttpCallResponseTrailers:228 return ctx.GetHttpCallResponseTrailer()229 case MapTypeHttpCallResponseMetadata:230 return ctx.GetHttpCallResponseMetadata()231 default:232 return ctx.GetCustomMap(mapType)233 }234}235func copyMapIntoInstance(m common.HeaderMap, instance common.WasmInstance, returnMapData int32, returnMapSize int32) Result {236 cloneMap := make(map[string]string)237 totalBytesLen := 4238 m.Range(func(key, value string) bool {239 cloneMap[key] = value240 totalBytesLen += 4 + 4 // keyLen + valueLen241 totalBytesLen += len(key) + 1 + len(value) + 1 // key + \0 + value + \0242 return true243 })244 addr, err := instance.Malloc(int32(totalBytesLen))245 if err != nil {246 return ResultInvalidMemoryAccess247 }248 err = instance.PutUint32(addr, uint32(len(cloneMap)))249 if err != nil {250 return ResultInvalidMemoryAccess251 }252 lenPtr := addr + 4253 dataPtr := lenPtr + uint64(8*len(cloneMap))254 for k, v := range cloneMap {255 _ = instance.PutUint32(lenPtr, uint32(len(k)))256 lenPtr += 4257 _ = instance.PutUint32(lenPtr, uint32(len(v)))258 lenPtr += 4259 _ = instance.PutMemory(dataPtr, uint64(len(k)), []byte(k))260 dataPtr += uint64(len(k))261 _ = instance.PutByte(dataPtr, 0)262 dataPtr++263 _ = instance.PutMemory(dataPtr, uint64(len(v)), []byte(v))264 dataPtr += uint64(len(v))265 _ = instance.PutByte(dataPtr, 0)266 dataPtr++267 }268 err = instance.PutUint32(uint64(returnMapData), uint32(addr))269 if err != nil {270 return ResultInvalidMemoryAccess271 }272 err = instance.PutUint32(uint64(returnMapSize), uint32(totalBytesLen))273 if err != nil {274 return ResultInvalidMemoryAccess275 }276 return ResultOk277}278func ProxyGetMapValues(instance common.WasmInstance, mapType MapType, keysData int32, keysSize int32,279 returnMapData int32, returnMapSize int32) Result {280 m := GetMap(instance, mapType)281 if m == nil {282 return ResultNotFound283 }284 if keysSize == 0 {285 // If the list of keys is empty, then all key-values in the map are returned.286 return copyMapIntoInstance(m, instance, returnMapData, returnMapSize)287 }288 key, err := instance.GetMemory(uint64(keysData), uint64(keysSize))289 if err != nil {290 return ResultInvalidMemoryAccess291 }292 if len(key) == 0 {293 return ResultBadArgument294 }295 value, exists := m.Get(string(key))296 if !exists {297 return ResultNotFound298 }299 return copyIntoInstance(instance, []byte(value), returnMapData, returnMapSize)300}301func ProxySetMapValues(instance common.WasmInstance, mapType MapType, removeKeysData int32, removeKeysSize int32,302 mapData int32, mapSize int32) Result {303 m := GetMap(instance, mapType)304 if m == nil {305 return ResultNotFound306 }307 // add new map data308 newMapContent, err := instance.GetMemory(uint64(mapData), uint64(mapSize))309 if err != nil {310 return ResultInvalidMemoryAccess311 }312 newMap := common.DecodeMap(newMapContent)313 for k, v := range newMap {314 m.Set(k, v)315 }316 // remove unwanted data317 key, err := instance.GetMemory(uint64(removeKeysData), uint64(removeKeysSize))318 if err != nil {319 return ResultInvalidMemoryAccess320 }321 if len(key) != 0 {322 m.Del(string(key))323 }324 return ResultOk325}326func ProxyOpenSharedKvstore(instance common.WasmInstance, kvstoreNameData int32, kvstoreNameSize int32, createIfNotExist int32,327 returnKvstoreID int32) Result {328 kvstoreName, err := instance.GetMemory(uint64(kvstoreNameData), uint64(kvstoreNameSize))329 if err != nil {330 return ResultInvalidMemoryAccess331 }332 if len(kvstoreName) == 0 {333 return ResultBadArgument334 }335 callback := getImportHandler(instance)336 kvStoreID, res := callback.OpenSharedKvstore(string(kvstoreName), intToBool(createIfNotExist))337 if res != ResultOk {338 return res339 }340 err = instance.PutUint32(uint64(returnKvstoreID), kvStoreID)341 if err != nil {342 return ResultInvalidMemoryAccess343 }344 return ResultOk345}346func ProxyGetSharedKvstoreKeyValues(instance common.WasmInstance, kvstoreID int32, keyData int32, keySize int32,347 returnValuesData int32, returnValuesSize int32, returnCas int32) Result {348 callback := getImportHandler(instance)349 kvstore := callback.GetSharedKvstore(uint32(kvstoreID))350 if kvstore == nil {351 return ResultBadArgument352 }353 key, err := instance.GetMemory(uint64(keyData), uint64(keySize))354 if err != nil {355 return ResultInvalidMemoryAccess356 }357 if len(key) == 0 {358 return ResultBadArgument359 }360 value, exists := kvstore.Get(string(key))361 if !exists {362 return ResultNotFound363 }364 return copyIntoInstance(instance, []byte(value), returnValuesData, returnValuesSize)365}366func ProxySetSharedKvstoreKeyValues(instance common.WasmInstance, kvstoreID int32, keyData int32, keySize int32,367 valuesData int32, valuesSize int32, cas int32) Result {368 callback := getImportHandler(instance)369 kvstore := callback.GetSharedKvstore(uint32(kvstoreID))370 if kvstore == nil {371 return ResultBadArgument372 }373 key, err := instance.GetMemory(uint64(keyData), uint64(keySize))374 if err != nil {375 return ResultInvalidMemoryAccess376 }377 if len(key) == 0 {378 return ResultBadArgument379 }380 value, err := instance.GetMemory(uint64(valuesData), uint64(valuesSize))381 if err != nil {382 return ResultInvalidMemoryAccess383 }384 res := kvstore.SetCAS(string(key), string(value), intToBool(cas))385 if !res {386 return ResultCompareAndSwapMismatch387 }388 return ResultOk389}390func ProxyAddSharedKvstoreKeyValues(instance common.WasmInstance, kvstoreID int32, keyData int32, keySize int32,391 valuesData int32, valuesSize int32, cas int32) Result {392 callback := getImportHandler(instance)393 kvstore := callback.GetSharedKvstore(uint32(kvstoreID))394 if kvstore == nil {395 return ResultBadArgument396 }397 key, err := instance.GetMemory(uint64(keyData), uint64(keySize))398 if err != nil {399 return ResultInvalidMemoryAccess400 }401 if len(key) == 0 {402 return ResultBadArgument403 }404 value, err := instance.GetMemory(uint64(valuesData), uint64(valuesSize))405 if err != nil {406 return ResultInvalidMemoryAccess407 }408 res := kvstore.SetCAS(string(key), string(value), intToBool(cas))409 if !res {410 return ResultCompareAndSwapMismatch411 }412 return ResultOk413}414func ProxyRemoveSharedKvstoreKey(instance common.WasmInstance, kvstoreID int32, keyData int32, keySize int32, cas int32) Result {415 callback := getImportHandler(instance)416 kvstore := callback.GetSharedKvstore(uint32(kvstoreID))417 if kvstore == nil {418 return ResultBadArgument419 }420 key, err := instance.GetMemory(uint64(keyData), uint64(keySize))421 if err != nil {422 return ResultInvalidMemoryAccess423 }424 if len(key) == 0 {425 return ResultBadArgument426 }427 res := kvstore.DelCAS(string(key), intToBool(cas))428 if !res {429 return ResultCompareAndSwapMismatch430 }431 return ResultOk432}433func ProxyDeleteSharedKvstore(instance common.WasmInstance, kvstoreID int32) Result {434 callback := getImportHandler(instance)435 return callback.DeleteSharedKvstore(uint32(kvstoreID))436}437func ProxyOpenSharedQueue(instance common.WasmInstance, queueNameData int32, queueNameSize int32, createIfNotExist int32,438 returnQueueID int32) Result {439 queueName, err := instance.GetMemory(uint64(queueNameData), uint64(queueNameSize))440 if err != nil {441 return ResultInvalidMemoryAccess442 }443 if len(queueName) == 0 {444 return ResultBadArgument445 }446 callback := getImportHandler(instance)447 queueID, res := callback.OpenSharedQueue(string(queueName), intToBool(createIfNotExist))448 if res != ResultOk {449 return res450 }451 err = instance.PutUint32(uint64(returnQueueID), queueID)452 if err != nil {453 return ResultInvalidMemoryAccess454 }455 return ResultOk456}457func ProxyDequeueSharedQueueItem(instance common.WasmInstance, queueID int32, returnPayloadData int32, returnPayloadSize int32) Result {458 callback := getImportHandler(instance)459 value, res := callback.DequeueSharedQueueItem(uint32(queueID))460 if res != ResultOk {461 return res462 }463 return copyIntoInstance(instance, []byte(value), returnPayloadData, returnPayloadSize)464}465func ProxyEnqueueSharedQueueItem(instance common.WasmInstance, queueID int32, payloadData int32, payloadSize int32) Result {466 value, err := instance.GetMemory(uint64(payloadData), uint64(payloadSize))467 if err != nil {468 return ResultInvalidMemoryAccess469 }470 callback := getImportHandler(instance)471 return callback.EnqueueSharedQueueItem(uint32(queueID), string(value))472}473func ProxyDeleteSharedQueue(instance common.WasmInstance, queueID int32) Result {474 callback := getImportHandler(instance)475 return callback.DeleteSharedQueue(uint32(queueID))476}477func ProxyCreateTimer(instance common.WasmInstance, period int32, oneTime int32, returnTimerID int32) Result {478 callback := getImportHandler(instance)479 timerID, res := callback.CreateTimer(period, intToBool(oneTime))480 if res != ResultOk {481 return res482 }483 err := instance.PutUint32(uint64(returnTimerID), timerID)484 if err != nil {485 return ResultInvalidMemoryAccess486 }487 return ResultOk488}489func ProxyDeleteTimer(instance common.WasmInstance, timerID int32) Result {490 callback := getImportHandler(instance)491 return callback.DeleteTimer(uint32(timerID))492}493func ProxyCreateMetric(instance common.WasmInstance, metricType MetricType,494 metricNameData int32, metricNameSize int32, returnMetricID int32) Result {495 ctx := getImportHandler(instance)496 name, err := instance.GetMemory(uint64(metricNameData), uint64(metricNameSize))497 if err != nil {498 return ResultInvalidMemoryAccess499 }500 if len(name) == 0 {501 return ResultBadArgument502 }503 mid, res := ctx.CreateMetric(metricType, string(name))504 if res != ResultOk {505 return res506 }507 err = instance.PutUint32(uint64(returnMetricID), mid)508 if err != nil {509 return ResultInvalidMemoryAccess510 }511 return ResultOk512}513func ProxyGetMetricValue(instance common.WasmInstance, metricID int32, returnValue int32) Result {514 ctx := getImportHandler(instance)515 value, res := ctx.GetMetricValue(uint32(metricID))516 if res != ResultOk {517 return res518 }519 err := instance.PutUint32(uint64(returnValue), uint32(value))520 if err != nil {521 return ResultInvalidMemoryAccess522 }523 return ResultOk524}525func ProxySetMetricValue(instance common.WasmInstance, metricID int32, value int64) Result {526 ctx := getImportHandler(instance)527 res := ctx.SetMetricValue(uint32(metricID), value)528 return res529}530func ProxyIncrementMetricValue(instance common.WasmInstance, metricID int32, offset int64) Result {531 ctx := getImportHandler(instance)532 return ctx.IncrementMetricValue(uint32(metricID), offset)533}534func ProxyDeleteMetric(instance common.WasmInstance, metricID int32) Result {535 ctx := getImportHandler(instance)536 return ctx.DeleteMetric(uint32(metricID))537}538func ProxyDispatchHttpCall(instance common.WasmInstance, upstreamNameData int32, upstreamNameSize int32, headersMapData int32, headersMapSize int32,539 bodyData int32, bodySize int32, trailersMapData int32, trailersMapSize int32, timeoutMilliseconds int32,540 returnCalloutID int32) Result {541 upstream, err := instance.GetMemory(uint64(upstreamNameData), uint64(upstreamNameSize))542 if err != nil {543 return ResultInvalidMemoryAccess544 }545 headerMapData, err := instance.GetMemory(uint64(headersMapData), uint64(headersMapSize))546 if err != nil {547 return ResultInvalidMemoryAccess548 }549 headerMap := common.DecodeMap(headerMapData)550 body, err := instance.GetMemory(uint64(bodyData), uint64(bodySize))551 if err != nil {552 return ResultInvalidMemoryAccess553 }554 trailerMapData, err := instance.GetMemory(uint64(trailersMapData), uint64(trailersMapSize))555 if err != nil {556 return ResultInvalidMemoryAccess557 }558 trailerMap := common.DecodeMap(trailerMapData)559 ctx := getImportHandler(instance)560 calloutID, res := ctx.DispatchHttpCall(string(upstream),561 common.CommonHeader(headerMap), common.NewIoBufferBytes(body), common.CommonHeader(trailerMap),562 uint32(timeoutMilliseconds),563 )564 if res != ResultOk {565 return res566 }567 err = instance.PutUint32(uint64(returnCalloutID), calloutID)568 if err != nil {569 return ResultInvalidMemoryAccess570 }571 return ResultOk572}573func ProxyDispatchGrpcCall(instance common.WasmInstance, upstreamNameData int32, upstreamNameSize int32, serviceNameData int32, serviceNameSize int32,574 serviceMethodData int32, serviceMethodSize int32, initialMetadataMapData int32, initialMetadataMapSize int32,575 grpcMessageData int32, grpcMessageSize int32, timeoutMilliseconds int32, returnCalloutID int32) Result {576 upstream, err := instance.GetMemory(uint64(upstreamNameData), uint64(upstreamNameSize))577 if err != nil {578 return ResultInvalidMemoryAccess579 }580 serviceName, err := instance.GetMemory(uint64(serviceNameData), uint64(serviceNameSize))581 if err != nil {582 return ResultInvalidMemoryAccess583 }584 serviceMethod, err := instance.GetMemory(uint64(serviceMethodData), uint64(serviceMethodSize))585 if err != nil {586 return ResultInvalidMemoryAccess587 }588 initialMetaMapdata, err := instance.GetMemory(uint64(initialMetadataMapData), uint64(initialMetadataMapSize))589 if err != nil {590 return ResultInvalidMemoryAccess591 }592 initialMetadataMap := common.DecodeMap(initialMetaMapdata)593 msg, err := instance.GetMemory(uint64(grpcMessageData), uint64(grpcMessageSize))594 if err != nil {595 return ResultInvalidMemoryAccess596 }597 ctx := getImportHandler(instance)598 calloutID, res := ctx.DispatchGrpcCall(string(upstream), string(serviceName), string(serviceMethod),599 common.CommonHeader(initialMetadataMap), common.NewIoBufferBytes(msg), uint32(timeoutMilliseconds))600 if res != ResultOk {601 return res602 }603 err = instance.PutUint32(uint64(returnCalloutID), calloutID)604 if err != nil {605 return ResultInvalidMemoryAccess606 }607 return ResultOk608}609func ProxyOpenGrpcStream(instance common.WasmInstance, upstreamNameData int32, upstreamNameSize int32, serviceNameData int32, serviceNameSize int32,610 serviceMethodData int32, serviceMethodSize int32, initialMetadataMapData int32, initialMetadataMapSize int32,611 returnCalloutID int32) Result {612 upstream, err := instance.GetMemory(uint64(upstreamNameData), uint64(upstreamNameSize))613 if err != nil {614 return ResultInvalidMemoryAccess615 }616 serviceName, err := instance.GetMemory(uint64(serviceNameData), uint64(serviceNameSize))617 if err != nil {618 return ResultInvalidMemoryAccess619 }620 serviceMethod, err := instance.GetMemory(uint64(serviceMethodData), uint64(serviceMethodSize))621 if err != nil {622 return ResultInvalidMemoryAccess623 }624 initialMetaMapdata, err := instance.GetMemory(uint64(initialMetadataMapData), uint64(initialMetadataMapSize))625 if err != nil {626 return ResultInvalidMemoryAccess627 }628 initialMetadataMap := common.DecodeMap(initialMetaMapdata)629 ctx := getImportHandler(instance)630 calloutID, res := ctx.OpenGrpcStream(string(upstream), string(serviceName), string(serviceMethod), common.CommonHeader(initialMetadataMap))631 if res != ResultOk {632 return res633 }634 err = instance.PutUint32(uint64(returnCalloutID), calloutID)635 if err != nil {636 return ResultInvalidMemoryAccess637 }638 return ResultOk639}640func ProxySendGrpcStreamMessage(instance common.WasmInstance, calloutID int32, grpcMessageData int32, grpcMessageSize int32) Result {641 grpcMessage, err := instance.GetMemory(uint64(grpcMessageData), uint64(grpcMessageSize))642 if err != nil {643 return ResultInvalidMemoryAccess644 }645 ctx := getImportHandler(instance)646 return ctx.SendGrpcStreamMessage(uint32(calloutID), common.NewIoBufferBytes(grpcMessage))647}648func ProxyCancelGrpcCall(instance common.WasmInstance, calloutID int32) Result {649 callback := getImportHandler(instance)650 return callback.CancelGrpcCall(uint32(calloutID))651}652func ProxyCloseGrpcCall(instance common.WasmInstance, calloutID int32) Result {653 callback := getImportHandler(instance)654 return callback.CloseGrpcCall(uint32(calloutID))655}656func ProxyCallCustomFunction(instance common.WasmInstance, customFunctionID int32, parametersData int32, parametersSize int32,657 returnResultsData int32, returnResultsSize int32) Result {658 param, err := instance.GetMemory(uint64(parametersData), uint64(parametersSize))659 if err != nil {660 return ResultInvalidMemoryAccess661 }662 ctx := getImportHandler(instance)663 ret, res := ctx.CallCustomFunction(uint32(customFunctionID), string(param))664 if res != ResultOk {665 return res666 }667 return copyIntoInstance(instance, []byte(ret), returnResultsData, returnResultsSize)668}...
grpc.go
Source:grpc.go
1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17package v118import (19 "mosn.io/proxy-wasm-go-host/proxywasm/common"20)21func ProxyOpenGrpcStream(instance common.WasmInstance, grpcServiceData int32, grpcServiceSize int32,22 serviceNameData int32, serviceNameSize int32, methodData int32, methodSize int32, returnCalloutID int32) int32 {23 grpcService, err := instance.GetMemory(uint64(grpcServiceData), uint64(grpcServiceSize))24 if err != nil {25 return WasmResultInvalidMemoryAccess.Int32()26 }27 serviceName, err := instance.GetMemory(uint64(serviceNameData), uint64(serviceNameSize))28 if err != nil {29 return WasmResultInvalidMemoryAccess.Int32()30 }31 method, err := instance.GetMemory(uint64(methodData), uint64(methodSize))32 if err != nil {33 return WasmResultInvalidMemoryAccess.Int32()34 }35 ctx := getImportHandler(instance)36 calloutID, res := ctx.OpenGrpcStream(string(grpcService), string(serviceName), string(method))37 if res != WasmResultOk {38 return res.Int32()39 }40 err = instance.PutUint32(uint64(returnCalloutID), uint32(calloutID))41 if err != nil {42 return WasmResultInvalidMemoryAccess.Int32()43 }44 return WasmResultOk.Int32()45}46func ProxySendGrpcCallMessage(instance common.WasmInstance, calloutID int32, data int32, size int32, endOfStream int32) int32 {47 msg, err := instance.GetMemory(uint64(data), uint64(size))48 if err != nil {49 return WasmResultInvalidMemoryAccess.Int32()50 }51 ctx := getImportHandler(instance)52 return ctx.SendGrpcCallMsg(calloutID, common.NewIoBufferBytes(msg), endOfStream).Int32()53}54func ProxyCancelGrpcCall(instance common.WasmInstance, calloutID int32) int32 {55 ctx := getImportHandler(instance)56 return ctx.CancelGrpcCall(calloutID).Int32()57}58func ProxyCloseGrpcCall(instance common.WasmInstance, calloutID int32) int32 {59 ctx := getImportHandler(instance)60 return ctx.CloseGrpcCall(calloutID).Int32()61}62func ProxyGrpcCall(instance common.WasmInstance, grpcServiceData int32, grpcServiceSize int32,63 serviceNameData int32, serviceNameSize int32,64 methodData int32, methodSize int32,65 grpcMessageData int32, grpcMessageSize int32,66 timeoutMilliseconds int32, returnCalloutID int32) int32 {67 grpcService, err := instance.GetMemory(uint64(grpcServiceData), uint64(grpcServiceSize))68 if err != nil {69 return WasmResultInvalidMemoryAccess.Int32()70 }71 serviceName, err := instance.GetMemory(uint64(serviceNameData), uint64(serviceNameSize))72 if err != nil {73 return WasmResultInvalidMemoryAccess.Int32()74 }75 method, err := instance.GetMemory(uint64(methodData), uint64(methodSize))76 if err != nil {77 return WasmResultInvalidMemoryAccess.Int32()78 }79 msg, err := instance.GetMemory(uint64(grpcMessageData), uint64(grpcMessageSize))80 if err != nil {81 return WasmResultInvalidMemoryAccess.Int32()82 }83 ctx := getImportHandler(instance)84 calloutID, res := ctx.GrpcCall(string(grpcService), string(serviceName), string(method),85 common.NewIoBufferBytes(msg), timeoutMilliseconds)86 if res != WasmResultOk {87 return res.Int32()88 }89 err = instance.PutUint32(uint64(returnCalloutID), uint32(calloutID))90 if err != nil {91 return WasmResultInvalidMemoryAccess.Int32()92 }93 return WasmResultOk.Int32()94}...
sub_provisioning_info.go
Source:sub_provisioning_info.go
1package handler2import (3 "github.com/HailoOSS/platform/errors"4 "github.com/HailoOSS/platform/server"5 "github.com/HailoOSS/protobuf/proto"6 "github.com/HailoOSS/provisioning-manager-service/domain"7 "github.com/HailoOSS/provisioning-manager-service/registry"8 iproto "github.com/HailoOSS/provisioning-service/proto"9 "time"10)11func protoToProvisioner(p *iproto.Info) *domain.Provisioner {12 var processes []*domain.Service13 var containers []*domain.Service14 for _, proc := range p.GetProcesses() {15 processes = append(processes, &domain.Service{16 Name: proc.GetName(),17 Version: proc.GetVersion(),18 Usage: &domain.Resource{19 Cpu: proc.GetUsage().GetCpu(),20 Memory: proc.GetUsage().GetMemory(),21 },22 })23 }24 for _, cont := range p.GetContainers() {25 containers = append(containers, &domain.Service{26 Name: cont.GetName(),27 Version: cont.GetVersion(),28 Usage: &domain.Resource{29 Cpu: cont.GetUsage().GetCpu(),30 Memory: cont.GetUsage().GetMemory(),31 },32 Allocation: &domain.Resource{33 Cpu: cont.GetUsage().GetCpu(),34 Memory: cont.GetUsage().GetMemory(),35 },36 })37 }38 return &domain.Provisioner{39 Id: p.GetId(),40 Version: p.GetVersion(),41 Hostname: p.GetHostname(),42 IpAddress: p.GetIpAddress(),43 AzName: p.GetAzName(),44 MachineClass: p.GetMachineClass(),45 Started: time.Unix(int64(p.GetStarted()), 0),46 LastUpdate: time.Now(),47 Machine: &domain.Machine{48 Cores: p.GetMachine().GetCores(),49 Memory: p.GetMachine().GetMemory(),50 Disk: p.GetMachine().GetDisk(),51 Usage: &domain.Resource{52 Cpu: p.GetMachine().GetUsage().GetCpu(),53 Memory: p.GetMachine().GetUsage().GetMemory(),54 Disk: p.GetMachine().GetUsage().GetDisk(),55 },56 },57 Processes: processes,58 Containers: containers,59 }60}61func SubProvisioningInfo(req *server.Request) (proto.Message, errors.Error) {62 request := &iproto.Info{}63 if err := req.Unmarshal(request); err != nil {64 return nil, errors.BadRequest(server.Name+".subprovisioninginfo", err.Error())65 }66 p := protoToProvisioner(request)67 registry.Insert(p)68 return nil, nil69}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!