simonz-bq commented on a change in pull request #1556: URL: https://github.com/apache/tinkerpop/pull/1556#discussion_r806376918
########## File path: gremlin-go/driver/connection.go ########## @@ -0,0 +1,68 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +type connection struct { Review comment: Added as a part of MS2 ########## File path: gremlin-go/driver/connection.go ########## @@ -0,0 +1,68 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +type connection struct { + host string + port int + transporterType TransporterType + logHandler *logHandler + transporter transporter + protocol protocol + results map[string]ResultSet +} + +func (connection *connection) close() (err error) { + if connection.transporter != nil { + err = connection.transporter.Close() + } + return +} + +func (connection *connection) connect() error { + if connection.transporter != nil { + closeErr := connection.transporter.Close() + connection.logHandler.logf(Warning, transportCloseFailed, closeErr) + } + connection.protocol = newGremlinServerWSProtocol(connection.logHandler) + connection.transporter = getTransportLayer(connection.transporterType, connection.host, connection.port) + err := connection.transporter.Connect() + if err != nil { + return err + } + connection.protocol.connectionMade(connection.transporter) + return nil +} + +func (connection *connection) write(request *request) (ResultSet, error) { + if connection.transporter == nil || connection.transporter.IsClosed() { + err := connection.connect() Review comment: The proper logic for this method is updated as a part of MS2. ########## File path: gremlin-go/driver/protocol.go ########## @@ -0,0 +1,127 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "errors" + "fmt" + "net/http" +) + +type protocol interface { Review comment: Documentation added - more detailed explanation cannot live here however. ########## File path: gremlin-go/driver/protocol.go ########## @@ -0,0 +1,127 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "errors" + "fmt" + "net/http" +) + +type protocol interface { + connectionMade(transport transporter) + read(resultSets map[string]ResultSet) (string, error) + write(request *request, results map[string]ResultSet) (string, error) +} + +type protocolBase struct { + protocol + + transporter transporter +} + +type gremlinServerWSProtocol struct { + *protocolBase + + serializer serializer + logHandler *logHandler + maxContentLength int + username string + password string +} + +func (protocol *protocolBase) connectionMade(transporter transporter) { + protocol.transporter = transporter +} + +func (protocol *gremlinServerWSProtocol) read(resultSets map[string]ResultSet) (string, error) { + // Read data from transport layer. + msg, err := protocol.transporter.Read() + if err != nil || msg == nil { + if err != nil { + return "", err + } + protocol.logHandler.log(Error, malformedURL) + return "", errors.New("malformed ws or wss URL") + } + // Deserialize message and unpack. + response, err := protocol.serializer.deserializeMessage(msg) + if err != nil { + return "", err + } + + responseID, statusCode, metadata, data := response.responseID, response.responseStatus.code, + response.responseResult.meta, response.responseResult.data + + resultSet := resultSets[responseID.String()] + if resultSet == nil { + resultSet = newChannelResultSet(responseID.String()) + } + resultSets[responseID.String()] = resultSet + if aggregateTo, ok := metadata["aggregateTo"]; ok { + resultSet.setAggregateTo(aggregateTo.(string)) + } + + // Handle status codes appropriately. If status code is http.StatusPartialContent, we need to re-read data. + if statusCode == http.StatusProxyAuthRequired { Review comment: Done as a part of MS2. ########## File path: gremlin-go/driver/client.go ########## @@ -0,0 +1,87 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "golang.org/x/text/language" +) + +// ClientSettings is used to modify a Client's settings on initialization. +type ClientSettings struct { + TransporterType TransporterType + LogVerbosity LogVerbosity + Logger Logger + Language language.Tag +} + +// Client is used to connect and interact with a Gremlin-supported server. +type Client struct { + host string + port int + logHandler *logHandler + transporterType TransporterType + connection *connection +} + +// NewClient creates a Client and configures it with the given parameters. +func NewClient(host string, port int, configurations ...func(settings *ClientSettings)) *Client { + settings := &ClientSettings{ + TransporterType: Gorilla, + LogVerbosity: Info, + Logger: &defaultLogger{}, + Language: language.English, + } + for _, configuration := range configurations { + configuration(settings) + } + + logHandler := newLogHandler(settings.Logger, settings.LogVerbosity, settings.Language) + client := &Client{ + host: host, + port: port, + logHandler: logHandler, + transporterType: settings.TransporterType, + connection: nil, + } + return client +} + +// Close closes the client via connection +func (client *Client) Close() error { + return client.connection.close() +} + +// Submit submits a Gremlin script to the server and returns a ResultSet Review comment: As of MS2, it is no longer a synchronous call! ########## File path: gremlin-go/driver/request.go ########## @@ -0,0 +1,42 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import "github.com/google/uuid" + +const op = "eval" +const processor = "" + +// request represents a request to the server +type request struct { + requestID uuid.UUID `json:"requestId"` + op string `json:"op"` + processor string `json:"processor"` + args map[string]interface{} `json:"args"` +} + +func makeStringRequest(requestString string) (req request) { + return request{uuid.New(), op, processor, map[string]interface{}{ + "gremlin": requestString, + "aliases": map[string]interface{}{ + "g": "g", Review comment: TODO added and tracked. ########## File path: gremlin-go/driver/transporterFactory.go ########## @@ -0,0 +1,37 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +// TransporterType is an alias for valid transport protocols. +type TransporterType int + +const ( + // Gorilla transport layer: github.com/gorilla/websocket + Gorilla TransporterType = iota Review comment: Given the way we consume configuration, it wouldn't be an issue in this case. Regardless, this is a good suggestion and has been changed for MS2! ########## File path: gremlin-go/driver/gorillaTransporter.go ########## @@ -0,0 +1,91 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "net/url" + "strconv" + + "github.com/gorilla/websocket" +) + Review comment: Done. ########## File path: gremlin-go/driver/gorillaTransporter.go ########## @@ -0,0 +1,91 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "net/url" + "strconv" + + "github.com/gorilla/websocket" +) + +type gorillaTransporter struct { + host string + port int + connection websocketConn + isClosed bool +} + +func (transporter *gorillaTransporter) Connect() (err error) { + if transporter.connection != nil { + return Review comment: This part of the logic has been refactored as of MS2 to have a more intuitive workflow. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
